v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
internal-index.h
Go to the documentation of this file.
1// Copyright 2019 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_OBJECTS_INTERNAL_INDEX_H_
6#define V8_OBJECTS_INTERNAL_INDEX_H_
7
8#include <stdint.h>
9
10#include <limits>
11
12#include "src/base/logging.h"
13
14namespace v8 {
15namespace internal {
16
17// Simple wrapper around an entry (which is notably different from "index" for
18// dictionary backing stores). Most code should treat this as an opaque
19// wrapper: get it via GetEntryForIndex, pass it on to consumers.
21 public:
22 explicit constexpr InternalIndex(size_t raw) : entry_(raw) {}
24
26 DCHECK_GE(entry_, subtract);
27 return InternalIndex(entry_ - subtract);
28 }
30 DCHECK_LT(entry_, std::numeric_limits<size_t>::max() - add);
31 return InternalIndex(entry_ + add);
32 }
33
34 bool is_found() const { return entry_ != kNotFound; }
35 bool is_not_found() const { return entry_ == kNotFound; }
36
37 size_t raw_value() const { return entry_; }
38 uint32_t as_uint32() const {
39 DCHECK_LE(entry_, std::numeric_limits<uint32_t>::max());
40 return static_cast<uint32_t>(entry_);
41 }
42 constexpr int as_int() const {
43 DCHECK_GE(std::numeric_limits<int>::max(), entry_);
44 return static_cast<int>(entry_);
45 }
46
47 bool operator==(const InternalIndex& other) const {
48 return entry_ == other.entry_;
49 }
50
51 // Iteration support.
52 InternalIndex operator*() { return *this; }
53 bool operator!=(const InternalIndex& other) const {
54 return entry_ != other.entry_;
55 }
57 entry_++;
58 return *this;
59 }
60
61 bool operator<(const InternalIndex& other) const {
62 return entry_ < other.entry_;
63 }
64
65 class Range {
66 public:
67 explicit Range(size_t max) : min_(0), max_(max) {}
68 Range(size_t min, size_t max) : min_(min), max_(max) {}
69
72
73 private:
74 size_t min_;
75 size_t max_;
76 };
77
78 private:
79 static const size_t kNotFound = std::numeric_limits<size_t>::max();
80
81 size_t entry_;
82};
83
84} // namespace internal
85} // namespace v8
86
87#endif // V8_OBJECTS_INTERNAL_INDEX_H_
Range(size_t min, size_t max)
InternalIndex & operator++()
static const size_t kNotFound
bool operator!=(const InternalIndex &other) const
bool operator==(const InternalIndex &other) const
static InternalIndex NotFound()
constexpr InternalIndex(size_t raw)
constexpr int as_int() const
V8_WARN_UNUSED_RESULT InternalIndex adjust_down(size_t subtract) const
V8_WARN_UNUSED_RESULT InternalIndex adjust_up(size_t add) const
bool operator<(const InternalIndex &other) const
#define DCHECK_LE(v1, v2)
Definition logging.h:490
#define DCHECK_GE(v1, v2)
Definition logging.h:488
#define DCHECK_LT(v1, v2)
Definition logging.h:489
#define V8_WARN_UNUSED_RESULT
Definition v8config.h:671