OILS / mycpp / comparators.h View on Github | oilshell.org

90 lines, 48 significant
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
11template <typename L, typename R>
12class Tuple2;
13
14bool str_equals(BigStr* left, BigStr* right);
15bool maybe_str_equals(BigStr* left, BigStr* right);
16
17bool items_equal(BigStr* left, BigStr* right);
18bool keys_equal(BigStr* left, BigStr* right);
19
20// No List<T> comparison by pointer
21inline bool items_equal(void* left, void* right) {
22 assert(0);
23}
24
25// e.g. for Dict<Token*, int>, use object IDENTITY, not value
26inline bool keys_equal(void* left, void* right) {
27 return left == right;
28}
29
30inline bool items_equal(int left, int right) {
31 return left == right;
32}
33
34inline bool keys_equal(int left, int right) {
35 return items_equal(left, right);
36}
37
38inline bool items_equal(mops::BigInt left, mops::BigInt right) {
39 return left == right;
40}
41
42inline bool keys_equal(mops::BigInt left, mops::BigInt right) {
43 return items_equal(left, right);
44}
45
46bool items_equal(Tuple2<int, int>* t1, Tuple2<int, int>* t2);
47bool keys_equal(Tuple2<int, int>* t1, Tuple2<int, int>* t2);
48
49bool items_equal(Tuple2<BigStr*, int>* t1, Tuple2<BigStr*, int>* t2);
50bool keys_equal(Tuple2<BigStr*, int>* t1, Tuple2<BigStr*, int>* t2);
51
52namespace id_kind_asdl {
53enum class Kind;
54};
55
56// Defined in cpp/translation_stubs.h
57bool items_equal(id_kind_asdl::Kind left, id_kind_asdl::Kind right);
58
59inline int int_cmp(int a, int b) {
60 if (a == b) {
61 return 0;
62 }
63 return a < b ? -1 : 1;
64}
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.
70namespace mylib {
71
72// Used by [[ a > b ]] and so forth
73inline int str_cmp(BigStr* a, BigStr* b) {
74 int len_a = len(a);
75 int len_b = len(b);
76
77 int min = std::min(len_a, len_b);
78 if (min == 0) {
79 return int_cmp(len_a, len_b);
80 }
81 int comp = memcmp(a->data_, b->data_, min);
82 if (comp == 0) {
83 return int_cmp(len_a, len_b); // tiebreaker
84 }
85 return comp;
86}
87
88} // namespace mylib
89
90#endif // MYCPP_COMPARATORS_H