cpp

Coverage Report

Created: 2024-07-28 06:14

/home/uke/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
0
inline bool items_equal(int left, int right) {
31
0
  return left == right;
32
0
}
33
34
0
inline bool keys_equal(int left, int right) {
35
0
  return items_equal(left, right);
36
0
}
37
38
0
inline bool items_equal(mops::BigInt left, mops::BigInt right) {
39
0
  return left == right;
40
0
}
41
42
0
inline bool keys_equal(mops::BigInt left, mops::BigInt right) {
43
0
  return items_equal(left, right);
44
0
}
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
0
inline int int_cmp(int a, int b) {
60
0
  if (a == b) {
61
0
    return 0;
62
0
  }
63
0
  return a < b ? -1 : 1;
64
0
}
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
0
inline int str_cmp(BigStr* a, BigStr* b) {
74
0
  int len_a = len(a);
75
0
  int len_b = len(b);
76
0
77
0
  int min = std::min(len_a, len_b);
78
0
  if (min == 0) {
79
0
    return int_cmp(len_a, len_b);
80
0
  }
81
0
  int comp = memcmp(a->data_, b->data_, min);
82
0
  if (comp == 0) {
83
0
    return int_cmp(len_a, len_b);  // tiebreaker
84
0
  }
85
0
  return comp;
86
0
}
87
88
}  // namespace mylib
89
90
#endif  // MYCPP_COMPARATORS_H