Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
range.h
Go to the documentation of this file.
1// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#ifndef RUNTIME_VM_COMPILER_FFI_RANGE_H_
6#define RUNTIME_VM_COMPILER_FFI_RANGE_H_
7
8#include "platform/assert.h"
9#include "platform/utils.h"
10#include "vm/allocation.h"
11
12namespace dart {
13
14namespace compiler {
15
16namespace ffi {
17
18// A non-empty range.
19//
20// Ranges are positive and non-empty.
21//
22// The end is exclusive.
23class Range {
24 public:
25 // Constructs a Range from start (inclusive) and length.
26 //
27 // The resulting range is `[start_inclusive, start_inclusive + length)`.
28 static Range StartAndLength(intptr_t start_inclusive, intptr_t length) {
29 return Range(start_inclusive, start_inclusive + length);
30 }
31
32 // Constructs a Range from start (inclusive) and end (exclusive).
33 //
34 // The resulting range is `[start_inclusive, end_exclusive)`.
35 static Range StartAndEnd(intptr_t start_inclusive, intptr_t end_exclusive) {
36 return Range(start_inclusive, end_exclusive);
37 }
38
39 intptr_t start() const { return start_; }
40 intptr_t end_exclusive() const { return end_exclusive_; }
41 intptr_t end_inclusive() const { return end_exclusive_ - 1; }
42
43 intptr_t Length() const { return end_exclusive_ - start_; }
44
45 // Returns true iff number is in this range.
46 bool Contains(intptr_t number) const {
47 return start_ <= number && number < end_exclusive_;
48 }
49
50 // Returns true iff [this] contains [other] completely.
51 bool Contains(const Range& other) const {
52 return Contains(other.start_) && Contains(other.end_inclusive());
53 }
54
55 // Returns true iff [this] is completely after [other].
56 bool After(const Range& other) const {
57 return other.end_exclusive_ <= start_;
58 }
59
60 // Returns true iff [this] contains some numbers of [other].
61 bool Overlaps(const Range& other) const {
62 return !this->After(other) && !other.After(*this);
63 }
64
65 // Returns the intersection of [this] with [other].
66 //
67 // Requires [this] and [other] to overlap.
68 const Range Intersect(const Range& other) const {
69 ASSERT(Overlaps(other));
70 return Range(Utils::Maximum(start_, other.start_),
71 Utils::Minimum(end_exclusive_, other.end_exclusive_));
72 }
73
74 // Returns a range moved by [delta].
75 //
76 // `this.start() - delta` must be positive.
77 const Range Translate(intptr_t delta) const {
78 return Range(start_ + delta, end_exclusive_ + delta);
79 }
80
81 private:
82 Range(intptr_t start_inclusive, intptr_t end_exclusive)
83 : start_(start_inclusive), end_exclusive_(end_exclusive) {
84 ASSERT(start_ < end_exclusive_);
85 }
86
87 const intptr_t start_;
88 const intptr_t end_exclusive_;
89};
90
91} // namespace ffi
92
93} // namespace compiler
94
95} // namespace dart
96
97#endif // RUNTIME_VM_COMPILER_FFI_RANGE_H_
static constexpr T Maximum(T x, T y)
Definition utils.h:26
static T Minimum(T x, T y)
Definition utils.h:21
bool Contains(intptr_t number) const
Definition range.h:46
static Range StartAndLength(intptr_t start_inclusive, intptr_t length)
Definition range.h:28
intptr_t start() const
Definition range.h:39
const Range Intersect(const Range &other) const
Definition range.h:68
bool Overlaps(const Range &other) const
Definition range.h:61
static Range StartAndEnd(intptr_t start_inclusive, intptr_t end_exclusive)
Definition range.h:35
bool Contains(const Range &other) const
Definition range.h:51
bool After(const Range &other) const
Definition range.h:56
intptr_t end_inclusive() const
Definition range.h:41
const Range Translate(intptr_t delta) const
Definition range.h:77
intptr_t Length() const
Definition range.h:43
intptr_t end_exclusive() const
Definition range.h:40
#define ASSERT(E)
size_t length