SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
Comparators_basic.h
Go to the documentation of this file.
1 /*
2  * SWC-DB© Copyright since 2019 Alex Kashirin <kashirin.alex@gmail.com>
3  * License details at <https://github.com/kashirin-alex/swc-db/#license>
4  */
5 
6 
7 #ifndef swcdb_core_Comparators_basic_h
8 #define swcdb_core_Comparators_basic_h
9 
10 
11 /*
12 #if defined(__GNUC__ ) && __GNUC__ >= 10
13 #define SWC_IMPL_COMPARATORS_BASIC ON
14 #endif
15 */
16 
17 
18 namespace SWC {
19 
27 namespace Condition {
28 
29 
30 extern int
31 mem_cmp(const uint8_t* b1, const uint8_t* b2, size_t count) noexcept
32  SWC_ATTRIBS((SWC_ATTRIB_O3));
33 
34 extern bool
35 mem_eq(const uint8_t* b1, const uint8_t* b2, size_t count) noexcept
36  SWC_ATTRIBS((SWC_ATTRIB_O3));
37 
38 
39 extern int
40 str_cmp(const char* s1, const char* s2) noexcept
41  SWC_ATTRIBS((SWC_ATTRIB_O3));
42 
43 extern int
44 str_cmp(const char* s1, const char* s2, size_t count) noexcept
45  SWC_ATTRIBS((SWC_ATTRIB_O3));
46 
47 
48 extern bool
49 str_eq(const char* s1, const char* s2) noexcept
50  SWC_ATTRIBS((SWC_ATTRIB_O3));
51 
52 extern bool
53 str_eq(const char* s1, const char* s2, size_t count) noexcept
54  SWC_ATTRIBS((SWC_ATTRIB_O3));
55 
56 extern bool
57 str_case_eq(const char* s1, const char* s2, size_t count) noexcept
58  SWC_ATTRIBS((SWC_ATTRIB_O3));
59 
60 
61 extern bool
62 str_eq(const std::string& s1, const std::string& s2) noexcept
63  SWC_ATTRIBS((SWC_ATTRIB_O3));
64 
65 extern bool
66 mem_eq(const std::string& s1, const std::string& s2) noexcept
67  SWC_ATTRIBS((SWC_ATTRIB_O3));
68 
69 
70 
71 namespace { // local namespace
72 
73 
74 #if defined(SWC_IMPL_COMPARATORS_BASIC)
75 
76 static int
77 _mem_cmp(const uint8_t* b1, const uint8_t* b2, size_t count) noexcept
78  SWC_ATTRIBS((SWC_ATTRIB_O3));
79 
80 static bool
81 _mem_eq(const uint8_t* b1, const uint8_t* b2, size_t count) noexcept
82  SWC_ATTRIBS((SWC_ATTRIB_O3));
83 
84 
85 
86 SWC_SHOULD_NOT_INLINE
87 static int
88 _mem_cmp(const uint8_t* b1, const uint8_t* b2, size_t count) noexcept {
89  for(; count; --count, ++b1, ++b2)
90  if(*b1 != *b2)
91  return *b1 < *b2 ? -1 : 1;
92  return 0;
93 }
94 
95 SWC_SHOULD_NOT_INLINE
96 static bool
97 _mem_eq(const uint8_t* b1, const uint8_t* b2, size_t count) noexcept {
98  for(; count; --count, ++b1, ++b2) {
99  if(*b1 != *b2)
100  return false;
101  }
102  return true;
103 }
104 
105 static int
106 _str_cmp(const char* s1, const char* s2) noexcept
107  SWC_ATTRIBS((SWC_ATTRIB_O3));
108 
109 static int
110 _str_cmp(const char* s1, const char* s2, size_t count) noexcept
111  SWC_ATTRIBS((SWC_ATTRIB_O3));
112 
113 static bool
114 _str_eq(const char* s1, const char* s2) noexcept
115  SWC_ATTRIBS((SWC_ATTRIB_O3));
116 
117 static bool
118 _str_eq(const char* s1, const char* s2, size_t count) noexcept
119  SWC_ATTRIBS((SWC_ATTRIB_O3));
120 
121 static bool
122 _str_case_eq(const char* s1, const char* s2, size_t count) noexcept
123  SWC_ATTRIBS((SWC_ATTRIB_O3));
124 
125 
126 
127 SWC_SHOULD_NOT_INLINE
128 static int
129 _str_cmp(const char* s1, const char* s2) noexcept {
130  for(; ; ++s1, ++s2) {
131  if(*s1 != *s2)
132  return *s1 < *s2 ? -1 : 1;
133  if(!*s1)
134  break;
135  }
136  return 0;
137 }
138 
139 SWC_SHOULD_NOT_INLINE
140 static int
141 _str_cmp(const char* s1, const char* s2, size_t count) noexcept {
142  for(; count; --count, ++s1, ++s2) {
143  if(*s1 != *s2)
144  return *s1 < *s2 ? -1 : 1;
145  if(!*s1)
146  break;
147  }
148  return 0;
149 }
150 
151 SWC_SHOULD_NOT_INLINE
152 static bool
153 _str_eq(const char* s1, const char* s2) noexcept {
154  for(; ; ++s1, ++s2) {
155  if(*s1 != *s2)
156  return false;
157  if(!*s1)
158  return true;
159  }
160 }
161 
162 SWC_SHOULD_NOT_INLINE
163 static bool
164 _str_eq(const char* s1, const char* s2, size_t count) noexcept {
165  for(; count; --count, ++s1, ++s2) {
166  if(*s1 != *s2)
167  return false;
168  if(!*s1)
169  break;
170  }
171  return true;
172 }
173 
174 SWC_SHOULD_NOT_INLINE
175 static bool
176 _str_case_eq(const char* s1, const char* s2, size_t count) noexcept {
177  for(; count; --count, ++s1, ++s2) {
178  if(*s1 - (*s1 > 96 && *s1 < 123 ? 32 : 0) !=
179  *s2 - (*s2 > 96 && *s2 < 123 ? 32 : 0))
180  return false;
181  if(!*s1)
182  break;
183  }
184  return true;
185 }
186 
187 #endif // defined(SWC_IMPL_COMPARATORS_BASIC)
188 
189 } // local namespace
190 
191 
192 
193 
194 
195 extern SWC_CAN_INLINE
196 int
197 mem_cmp(const uint8_t* b1, const uint8_t* b2, size_t count) noexcept {
198  #if defined(SWC_IMPL_COMPARATORS_BASIC)
199  return _mem_cmp(b1, b2, count);
200  #else
201  return memcmp(b1, b2, count);
202  #endif
203 }
204 
205 extern SWC_CAN_INLINE
206 bool
207 mem_eq(const uint8_t* b1, const uint8_t* b2, size_t count) noexcept {
208  #if defined(SWC_IMPL_COMPARATORS_BASIC)
209  return _mem_eq(b1, b2, count);
210  #else
211  return !memcmp(b1, b2, count);
212  #endif
213 }
214 
215 extern SWC_CAN_INLINE
216 int
217 str_cmp(const char* s1, const char* s2) noexcept {
218  #if defined(SWC_IMPL_COMPARATORS_BASIC)
219  return _str_cmp(s1, s2);
220  #else
221  return strcmp(s1, s2);
222  #endif
223 }
224 
225 extern SWC_CAN_INLINE
226 int
227 str_cmp(const char* s1, const char* s2, size_t count) noexcept {
228  #if defined(SWC_IMPL_COMPARATORS_BASIC)
229  return _str_cmp(s1, s2, count);
230  #else
231  return strncmp(s1, s2, count);
232  #endif
233 }
234 
235 extern SWC_CAN_INLINE
236 bool
237 str_eq(const char* s1, const char* s2) noexcept {
238  #if defined(SWC_IMPL_COMPARATORS_BASIC)
239  return _str_eq(s1, s2);
240  #else
241  return !strcmp(s1, s2);
242  #endif
243 }
244 
245 extern SWC_CAN_INLINE
246 bool
247 str_eq(const char* s1, const char* s2, size_t count) noexcept {
248  #if defined(SWC_IMPL_COMPARATORS_BASIC)
249  return _str_eq(s1, s2, count);
250  #else
251  return !strncmp(s1, s2, count);
252  #endif
253 }
254 
255 extern SWC_CAN_INLINE
256 bool
257 str_case_eq(const char* s1, const char* s2, size_t count) noexcept {
258  #if defined(SWC_IMPL_COMPARATORS_BASIC)
259  return _str_case_eq(s1, s2, count);
260  #else
261  return !strncasecmp(s1, s2, count);
262  #endif
263 }
264 
265 
266 extern SWC_CAN_INLINE
267 bool
268 str_eq(const std::string& s1, const std::string& s2) noexcept {
269  return str_eq(s1.c_str(), s2.c_str());
270 }
271 
272 extern SWC_CAN_INLINE
273 bool
274 mem_eq(const std::string& s1, const std::string& s2) noexcept {
275  return s1.length() == s2.length() &&
276  mem_eq(reinterpret_cast<const uint8_t*>(s1.c_str()),
277  reinterpret_cast<const uint8_t*>(s2.c_str()),
278  s1.length());
279 }
280 
281 
282 } } // namespace SWC::Condition
283 
284 
285 
286 #endif // swcdb_core_Comparators_basic_h
SWC_ATTRIBS
#define SWC_ATTRIBS(attrs)
Definition: Compat.h:53
SWC::Condition::str_cmp
int str_cmp(const char *s1, const char *s2) noexcept SWC_ATTRIBS((SWC_ATTRIB_O3))
Definition: Comparators_basic.h:217
SWC::Condition::mem_eq
bool mem_eq(const uint8_t *b1, const uint8_t *b2, size_t count) noexcept SWC_ATTRIBS((SWC_ATTRIB_O3))
Definition: Comparators_basic.h:207
SWC::Condition::str_eq
bool str_eq(const char *s1, const char *s2) noexcept SWC_ATTRIBS((SWC_ATTRIB_O3))
Definition: Comparators_basic.h:237
SWC::Condition::mem_cmp
int mem_cmp(const uint8_t *b1, const uint8_t *b2, size_t count) noexcept SWC_ATTRIBS((SWC_ATTRIB_O3))
Definition: Comparators_basic.h:197
SWC_CAN_INLINE
#define SWC_CAN_INLINE
Definition: Compat.h:102
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::Condition::str_case_eq
bool str_case_eq(const char *s1, const char *s2, size_t count) noexcept SWC_ATTRIBS((SWC_ATTRIB_O3))
Definition: Comparators_basic.h:257