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

66 lines, 39 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_str.h" // len()
9
10template <typename L, typename R>
11class Tuple2;
12
13bool str_equals(BigStr* left, BigStr* right);
14bool maybe_str_equals(BigStr* left, BigStr* right);
15
16bool are_equal(BigStr* left, BigStr* right);
17
18bool are_equal(BigStr* left, BigStr* right);
19bool are_equal(int left, int right);
20bool are_equal(Tuple2<BigStr*, int>* t1, Tuple2<BigStr*, int>* t2);
21bool are_equal(Tuple2<int, int>* t1, Tuple2<int, int>* t2);
22
23bool keys_equal(int left, int right);
24bool keys_equal(BigStr* left, BigStr* right);
25bool keys_equal(Tuple2<int, int>* t1, Tuple2<int, int>* t2);
26bool keys_equal(Tuple2<BigStr*, int>* t1, Tuple2<BigStr*, int>* t2);
27bool keys_equal(void* left, void* right);
28
29namespace id_kind_asdl {
30enum class Kind;
31};
32
33bool are_equal(id_kind_asdl::Kind left, id_kind_asdl::Kind right);
34
35inline 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.
46namespace mylib {
47
48// Used by [[ a > b ]] and so forth
49inline 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