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_str.h" // len()
|
9 |
|
10 | template <typename L, typename R>
|
11 | class Tuple2;
|
12 |
|
13 | bool str_equals(BigStr* left, BigStr* right);
|
14 | bool maybe_str_equals(BigStr* left, BigStr* right);
|
15 |
|
16 | bool are_equal(BigStr* left, BigStr* right);
|
17 |
|
18 | bool are_equal(BigStr* left, BigStr* right);
|
19 | bool are_equal(int left, int right);
|
20 | bool are_equal(Tuple2<BigStr*, int>* t1, Tuple2<BigStr*, int>* t2);
|
21 | bool are_equal(Tuple2<int, int>* t1, Tuple2<int, int>* t2);
|
22 |
|
23 | bool keys_equal(int left, int right);
|
24 | bool keys_equal(BigStr* left, BigStr* right);
|
25 | bool keys_equal(Tuple2<int, int>* t1, Tuple2<int, int>* t2);
|
26 | bool keys_equal(Tuple2<BigStr*, int>* t1, Tuple2<BigStr*, int>* t2);
|
27 | bool keys_equal(void* left, void* right);
|
28 |
|
29 | namespace id_kind_asdl {
|
30 | enum class Kind;
|
31 | };
|
32 |
|
33 | bool are_equal(id_kind_asdl::Kind left, id_kind_asdl::Kind right);
|
34 |
|
35 | inline int int_cmp(int a, int b) {
|
36 | if (a == b) {
|
37 | return 0;
|
38 | }
|
39 | return a < b ? -1 : 1;
|
40 | }
|
41 |
|
42 | // mylib::str_cmp is in this common header to avoid gc_list.h -> gc_mylib.h
|
43 | // dependency
|
44 | //
|
45 | // It's also used for _cmp(BigStr*) in gc_list.
|
46 | namespace mylib {
|
47 |
|
48 | // Used by [[ a > b ]] and so forth
|
49 | inline int str_cmp(BigStr* a, BigStr* b) {
|
50 | int len_a = len(a);
|
51 | int len_b = len(b);
|
52 |
|
53 | int min = std::min(len_a, len_b);
|
54 | if (min == 0) {
|
55 | return int_cmp(len_a, len_b);
|
56 | }
|
57 | int comp = memcmp(a->data_, b->data_, min);
|
58 | if (comp == 0) {
|
59 | return int_cmp(len_a, len_b); // tiebreaker
|
60 | }
|
61 | return comp;
|
62 | }
|
63 |
|
64 | } // namespace mylib
|
65 |
|
66 | #endif // MYCPP_COMPARATORS_H
|