/home/andy/git/oilshell/oil/mycpp/comparators.h
Line | Count | Source (jump to first uncovered line) |
1 | | #ifndef MYCPP_COMPARATORS_H |
2 | | #define MYCPP_COMPARATORS_H |
3 | | |
4 | | #include <string.h> // memcmp |
5 | | |
6 | | #include <algorithm> // std::min() |
7 | | |
8 | | #include "mycpp/gc_mops.h" // mops::BigInt |
9 | | #include "mycpp/gc_str.h" // len() |
10 | | |
11 | | template <typename L, typename R> |
12 | | class Tuple2; |
13 | | |
14 | | bool str_equals(BigStr* left, BigStr* right); |
15 | | bool maybe_str_equals(BigStr* left, BigStr* right); |
16 | | |
17 | | bool items_equal(BigStr* left, BigStr* right); |
18 | | bool keys_equal(BigStr* left, BigStr* right); |
19 | | |
20 | | // No List<T> comparison by pointer |
21 | 0 | inline bool items_equal(void* left, void* right) { |
22 | 0 | assert(0); |
23 | 0 | } |
24 | | |
25 | | // e.g. for Dict<Token*, int>, use object IDENTITY, not value |
26 | 0 | inline bool keys_equal(void* left, void* right) { |
27 | 0 | return left == right; |
28 | 0 | } |
29 | | |
30 | 32.5k | inline bool items_equal(int left, int right) { |
31 | 32.5k | return left == right; |
32 | 32.5k | } |
33 | | |
34 | 32.4k | inline bool keys_equal(int left, int right) { |
35 | 32.4k | return items_equal(left, right); |
36 | 32.4k | } |
37 | | |
38 | 40.1k | inline bool items_equal(mops::BigInt left, mops::BigInt right) { |
39 | 40.1k | return left == right; |
40 | 40.1k | } |
41 | | |
42 | 40.1k | inline bool keys_equal(mops::BigInt left, mops::BigInt right) { |
43 | 40.1k | return items_equal(left, right); |
44 | 40.1k | } |
45 | | |
46 | | bool items_equal(Tuple2<int, int>* t1, Tuple2<int, int>* t2); |
47 | | bool keys_equal(Tuple2<int, int>* t1, Tuple2<int, int>* t2); |
48 | | |
49 | | bool items_equal(Tuple2<BigStr*, int>* t1, Tuple2<BigStr*, int>* t2); |
50 | | bool keys_equal(Tuple2<BigStr*, int>* t1, Tuple2<BigStr*, int>* t2); |
51 | | |
52 | | namespace id_kind_asdl { |
53 | | enum class Kind; |
54 | | }; |
55 | | |
56 | | // Defined in cpp/translation_stubs.h |
57 | | bool items_equal(id_kind_asdl::Kind left, id_kind_asdl::Kind right); |
58 | | |
59 | 36 | inline int int_cmp(int a, int b) { |
60 | 36 | if (a == b) { |
61 | 8 | return 0; |
62 | 8 | } |
63 | 28 | return a < b ? -1 : 1; |
64 | 36 | } |
65 | | |
66 | | // mylib::str_cmp is in this common header to avoid gc_list.h -> gc_mylib.h |
67 | | // dependency |
68 | | // |
69 | | // It's also used for _cmp(BigStr*) in gc_list. |
70 | | namespace mylib { |
71 | | |
72 | | // Used by [[ a > b ]] and so forth |
73 | 58 | inline int str_cmp(BigStr* a, BigStr* b) { |
74 | 58 | int len_a = len(a); |
75 | 58 | int len_b = len(b); |
76 | | |
77 | 58 | int min = std::min(len_a, len_b); |
78 | 58 | if (min == 0) { |
79 | 16 | return int_cmp(len_a, len_b); |
80 | 16 | } |
81 | 42 | int comp = memcmp(a->data_, b->data_, min); |
82 | 42 | if (comp == 0) { |
83 | 8 | return int_cmp(len_a, len_b); // tiebreaker |
84 | 8 | } |
85 | 34 | return comp; |
86 | 42 | } |
87 | | |
88 | | } // namespace mylib |
89 | | |
90 | | #endif // MYCPP_COMPARATORS_H |