]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/misc/Interval.h
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / misc / Interval.h
1 /* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
2  * Use of this file is governed by the BSD 3-clause license that
3  * can be found in the LICENSE.txt file in the project root.
4  */
5
6 #pragma once
7
8 #include "antlr4-common.h"
9
10 namespace antlr4 {
11 namespace misc {
12
13   // Helpers to convert certain unsigned symbols (e.g. Token::EOF) to their original numeric value (e.g. -1)
14   // and vice versa. This is needed mostly for intervals to keep their original order and for toString()
15   // methods to print the original numeric value (e.g. for tests).
16   size_t numericToSymbol(ssize_t v);
17   ssize_t symbolToNumeric(size_t v);
18
19   /// An immutable inclusive interval a..b
20   class ANTLR4CPP_PUBLIC Interval {
21   public:
22     static const Interval INVALID;
23
24     // Must stay signed to guarantee the correct sort order.
25     ssize_t a;
26     ssize_t b;
27
28     Interval();
29     explicit Interval(size_t a_, size_t b_); // For unsigned -> signed mappings.
30     Interval(ssize_t a_, ssize_t b_);
31
32     /// return number of elements between a and b inclusively. x..x is length 1.
33     ///  if b < a, then length is 0.  9..10 has length 2.
34     size_t length() const;
35
36     bool operator == (const Interval &other) const;
37
38     size_t hashCode() const;
39
40     /// <summary>
41     /// Does this start completely before other? Disjoint </summary>
42     bool startsBeforeDisjoint(const Interval &other) const;
43
44     /// <summary>
45     /// Does this start at or before other? Nondisjoint </summary>
46     bool startsBeforeNonDisjoint(const Interval &other) const;
47
48     /// <summary>
49     /// Does this.a start after other.b? May or may not be disjoint </summary>
50     bool startsAfter(const Interval &other) const;
51
52     /// <summary>
53     /// Does this start completely after other? Disjoint </summary>
54     bool startsAfterDisjoint(const Interval &other) const;
55
56     /// <summary>
57     /// Does this start after other? NonDisjoint </summary>
58     bool startsAfterNonDisjoint(const Interval &other) const;
59
60     /// <summary>
61     /// Are both ranges disjoint? I.e., no overlap? </summary>
62     bool disjoint(const Interval &other) const;
63
64     /// <summary>
65     /// Are two intervals adjacent such as 0..41 and 42..42? </summary>
66     bool adjacent(const Interval &other) const;
67
68     bool properlyContains(const Interval &other) const;
69
70     /// <summary>
71     /// Return the interval computed from combining this and other </summary>
72     Interval Union(const Interval &other) const;
73
74     /// <summary>
75     /// Return the interval in common between this and o </summary>
76     Interval intersection(const Interval &other) const;
77
78     std::string toString() const;
79
80   private:
81   };
82
83 } // namespace atn
84 } // namespace antlr4