mycpp

Coverage Report

Created: 2024-06-09 04:56

/home/uke/oil/mycpp/comparators.h
Line
Count
Source
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
18
inline int int_cmp(int a, int b) {
36
18
  if (a == b) {
37
4
    return 0;
38
4
  }
39
14
  return a < b ? -1 : 1;
40
18
}
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
29
inline int str_cmp(BigStr* a, BigStr* b) {
50
29
  int len_a = len(a);
51
29
  int len_b = len(b);
52
53
29
  int min = std::min(len_a, len_b);
54
29
  if (min == 0) {
55
8
    return int_cmp(len_a, len_b);
56
8
  }
57
21
  int comp = memcmp(a->data_, b->data_, min);
58
21
  if (comp == 0) {
59
4
    return int_cmp(len_a, len_b);  // tiebreaker
60
4
  }
61
17
  return comp;
62
21
}
63
64
}  // namespace mylib
65
66
#endif  // MYCPP_COMPARATORS_H