-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Range.h
679 lines (588 loc) · 18.9 KB
/
Range.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
/*
* Copyright 2013 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// @author Mark Rabkin (mrabkin@fb.com)
// @author Andrei Alexandrescu (andrei.alexandrescu@fb.com)
#ifndef FOLLY_RANGE_H_
#define FOLLY_RANGE_H_
#include "folly/FBString.h"
#include <glog/logging.h>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <stdexcept>
#include <type_traits>
#include <boost/operators.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits.hpp>
#include <bits/c++config.h>
#include "folly/Traits.h"
namespace folly {
template <class T> class Range;
/**
* Finds the first occurrence of needle in haystack. The algorithm is on
* average faster than O(haystack.size() * needle.size()) but not as fast
* as Boyer-Moore. On the upside, it does not do any upfront
* preprocessing and does not allocate memory.
*/
template <class T>
inline size_t qfind(const Range<T> & haystack,
const Range<T> & needle);
/**
* Finds the first occurrence of needle in haystack. The result is the
* offset reported to the beginning of haystack, or string::npos if
* needle wasn't found.
*/
template <class T>
size_t qfind(const Range<T> & haystack,
const typename Range<T>::value_type& needle);
/**
* Finds the first occurrence of any element of needle in
* haystack. The algorithm is O(haystack.size() * needle.size()).
*/
template <class T>
inline size_t qfind_first_of(const Range<T> & haystack,
const Range<T> & needle);
/**
* Small internal helper - returns the value just before an iterator.
*/
namespace detail {
/**
* For random-access iterators, the value before is simply i[-1].
*/
template <class Iter>
typename boost::enable_if_c<
boost::is_same<typename std::iterator_traits<Iter>::iterator_category,
std::random_access_iterator_tag>::value,
typename std::iterator_traits<Iter>::reference>::type
value_before(Iter i) {
return i[-1];
}
/**
* For all other iterators, we need to use the decrement operator.
*/
template <class Iter>
typename boost::enable_if_c<
!boost::is_same<typename std::iterator_traits<Iter>::iterator_category,
std::random_access_iterator_tag>::value,
typename std::iterator_traits<Iter>::reference>::type
value_before(Iter i) {
return *--i;
}
} // namespace detail
/**
* Range abstraction keeping a pair of iterators. We couldn't use
* boost's similar range abstraction because we need an API identical
* with the former StringPiece class, which is used by a lot of other
* code. This abstraction does fulfill the needs of boost's
* range-oriented algorithms though.
*
* (Keep memory lifetime in mind when using this class, since it
* doesn't manage the data it refers to - just like an iterator
* wouldn't.)
*/
template <class Iter>
class Range : private boost::totally_ordered<Range<Iter> > {
public:
typedef std::size_t size_type;
typedef Iter iterator;
typedef Iter const_iterator;
typedef typename boost::remove_reference<
typename std::iterator_traits<Iter>::reference>::type
value_type;
typedef typename std::iterator_traits<Iter>::reference reference;
typedef std::char_traits<value_type> traits_type;
static const size_type npos;
// Works for all iterators
Range() : b_(), e_() {
}
private:
static bool reachable(Iter b, Iter e, std::forward_iterator_tag) {
for (; b != e; ++b) {
LOG_EVERY_N(INFO, 100000) << __FILE__ ":" << __LINE__
<< " running reachability test ("
<< google::COUNTER << " iterations)...";
}
return true;
}
static bool reachable(Iter b, Iter e, std::random_access_iterator_tag) {
return b <= e;
}
public:
// Works for all iterators
Range(Iter start, Iter end)
: b_(start), e_(end) {
assert(reachable(b_, e_,
typename std::iterator_traits<Iter>::iterator_category()));
}
// Works only for random-access iterators
Range(Iter start, size_t size)
: b_(start), e_(start + size) { }
// Works only for Range<const char*>
/* implicit */ Range(Iter str)
: b_(str), e_(b_ + strlen(str)) {}
// Works only for Range<const char*>
/* implicit */ Range(const std::string& str)
: b_(str.data()), e_(b_ + str.size()) {}
// Works only for Range<const char*>
Range(const std::string& str, std::string::size_type startFrom) {
CHECK_LE(startFrom, str.size());
b_ = str.data() + startFrom;
e_ = str.data() + str.size();
}
// Works only for Range<const char*>
Range(const std::string& str,
std::string::size_type startFrom,
std::string::size_type size) {
CHECK_LE(startFrom + size, str.size());
b_ = str.data() + startFrom;
e_ = b_ + size;
}
Range(const Range<Iter>& str,
size_t startFrom,
size_t size) {
CHECK_LE(startFrom + size, str.size());
b_ = str.b_ + startFrom;
e_ = b_ + size;
}
// Works only for Range<const char*>
/* implicit */ Range(const fbstring& str)
: b_(str.data()), e_(b_ + str.size()) { }
// Works only for Range<const char*>
Range(const fbstring& str, fbstring::size_type startFrom) {
CHECK_LE(startFrom, str.size());
b_ = str.data() + startFrom;
e_ = str.data() + str.size();
}
// Works only for Range<const char*>
Range(const fbstring& str, fbstring::size_type startFrom,
fbstring::size_type size) {
CHECK_LE(startFrom + size, str.size());
b_ = str.data() + startFrom;
e_ = b_ + size;
}
// Allow implicit conversion from Range<const char*> (aka StringPiece) to
// Range<const unsigned char*> (aka ByteRange), as they're both frequently
// used to represent ranges of bytes. Allow explicit conversion in the other
// direction.
template <class OtherIter, typename std::enable_if<
(std::is_same<Iter, const unsigned char*>::value &&
std::is_same<OtherIter, const char*>::value), int>::type = 0>
/* implicit */ Range(const Range<OtherIter>& other)
: b_(reinterpret_cast<const unsigned char*>(other.begin())),
e_(reinterpret_cast<const unsigned char*>(other.end())) {
}
template <class OtherIter, typename std::enable_if<
(std::is_same<Iter, const char*>::value &&
std::is_same<OtherIter, const unsigned char*>::value), int>::type = 0>
explicit Range(const Range<OtherIter>& other)
: b_(reinterpret_cast<const char*>(other.begin())),
e_(reinterpret_cast<const char*>(other.end())) {
}
void clear() {
b_ = Iter();
e_ = Iter();
}
void assign(Iter start, Iter end) {
b_ = start;
e_ = end;
}
void reset(Iter start, size_type size) {
b_ = start;
e_ = start + size;
}
// Works only for Range<const char*>
void reset(const std::string& str) {
reset(str.data(), str.size());
}
size_type size() const {
assert(b_ <= e_);
return e_ - b_;
}
size_type walk_size() const {
assert(b_ <= e_);
return std::distance(b_, e_);
}
bool empty() const { return b_ == e_; }
Iter data() const { return b_; }
Iter start() const { return b_; }
Iter begin() const { return b_; }
Iter end() const { return e_; }
Iter cbegin() const { return b_; }
Iter cend() const { return e_; }
value_type& front() {
assert(b_ < e_);
return *b_;
}
value_type& back() {
assert(b_ < e_);
return detail::value_before(e_);
}
const value_type& front() const {
assert(b_ < e_);
return *b_;
}
const value_type& back() const {
assert(b_ < e_);
return detail::value_before(e_);
}
// Works only for Range<const char*>
std::string str() const { return std::string(b_, size()); }
std::string toString() const { return str(); }
// Works only for Range<const char*>
fbstring fbstr() const { return fbstring(b_, size()); }
fbstring toFbstring() const { return fbstr(); }
// Works only for Range<const char*>
int compare(const Range& o) const {
const size_type tsize = this->size();
const size_type osize = o.size();
const size_type msize = std::min(tsize, osize);
int r = traits_type::compare(data(), o.data(), msize);
if (r == 0) r = tsize - osize;
return r;
}
value_type& operator[](size_t i) {
CHECK_GT(size(), i);
return b_[i];
}
const value_type& operator[](size_t i) const {
CHECK_GT(size(), i);
return b_[i];
}
value_type& at(size_t i) {
if (i >= size()) throw std::out_of_range("index out of range");
return b_[i];
}
const value_type& at(size_t i) const {
if (i >= size()) throw std::out_of_range("index out of range");
return b_[i];
}
// Works only for Range<const char*>
uint32_t hash() const {
// Taken from fbi/nstring.h:
// Quick and dirty bernstein hash...fine for short ascii strings
uint32_t hash = 5381;
for (size_t ix = 0; ix < size(); ix++) {
hash = ((hash << 5) + hash) + b_[ix];
}
return hash;
}
void advance(size_type n) {
CHECK_LE(n, size());
b_ += n;
}
void subtract(size_type n) {
CHECK_LE(n, size());
e_ -= n;
}
void pop_front() {
assert(b_ < e_);
++b_;
}
void pop_back() {
assert(b_ < e_);
--e_;
}
Range subpiece(size_type first,
size_type length = std::string::npos) const {
CHECK_LE(first, size());
return Range(b_ + first,
std::min<std::string::size_type>(length, size() - first));
}
// string work-alike functions
size_type find(Range str) const {
return qfind(*this, str);
}
size_type find(Range str, size_t pos) const {
if (pos > size()) return std::string::npos;
size_t ret = qfind(subpiece(pos), str);
return ret == npos ? ret : ret + pos;
}
size_type find(Iter s, size_t pos, size_t n) const {
if (pos > size()) return std::string::npos;
size_t ret = qfind(pos ? subpiece(pos) : *this, Range(s, n));
return ret == npos ? ret : ret + pos;
}
// Works only for Range<const (unsigned) char*> which have Range(Iter) ctor
size_type find(const Iter s) const {
return qfind(*this, Range(s));
}
// Works only for Range<const (unsigned) char*> which have Range(Iter) ctor
size_type find(const Iter s, size_t pos) const {
if (pos > size()) return std::string::npos;
size_type ret = qfind(subpiece(pos), Range(s));
return ret == npos ? ret : ret + pos;
}
size_type find(value_type c) const {
return qfind(*this, c);
}
size_type find(value_type c, size_t pos) const {
if (pos > size()) return std::string::npos;
size_type ret = qfind(subpiece(pos), c);
return ret == npos ? ret : ret + pos;
}
size_type find_first_of(Range needles) const {
return qfind_first_of(*this, needles);
}
size_type find_first_of(Range needles, size_t pos) const {
if (pos > size()) return std::string::npos;
size_type ret = qfind_first_of(subpiece(pos), needles);
return ret == npos ? ret : ret + pos;
}
// Works only for Range<const (unsigned) char*> which have Range(Iter) ctor
size_type find_first_of(Iter needles) const {
return find_first_of(Range(needles));
}
// Works only for Range<const (unsigned) char*> which have Range(Iter) ctor
size_type find_first_of(Iter needles, size_t pos) const {
return find_first_of(Range(needles), pos);
}
size_type find_first_of(Iter needles, size_t pos, size_t n) const {
return find_first_of(Range(needles, n), pos);
}
size_type find_first_of(value_type c) const {
return find(c);
}
size_type find_first_of(value_type c, size_t pos) const {
return find(c, pos);
}
void swap(Range& rhs) {
std::swap(b_, rhs.b_);
std::swap(e_, rhs.e_);
}
private:
Iter b_, e_;
};
template <class Iter>
const typename Range<Iter>::size_type Range<Iter>::npos = std::string::npos;
template <class T>
void swap(Range<T>& lhs, Range<T>& rhs) {
lhs.swap(rhs);
}
/**
* Create a range from two iterators, with type deduction.
*/
template <class Iter>
Range<Iter> makeRange(Iter first, Iter last) {
return Range<Iter>(first, last);
}
typedef Range<const char*> StringPiece;
typedef Range<const unsigned char*> ByteRange;
std::ostream& operator<<(std::ostream& os, const StringPiece& piece);
/**
* Templated comparison operators
*/
template <class T>
inline bool operator==(const Range<T>& lhs, const Range<T>& rhs) {
return lhs.size() == rhs.size() && lhs.compare(rhs) == 0;
}
template <class T>
inline bool operator<(const Range<T>& lhs, const Range<T>& rhs) {
return lhs.compare(rhs) < 0;
}
/**
* Specializations of comparison operators for StringPiece
*/
namespace detail {
template <class A, class B>
struct ComparableAsStringPiece {
enum {
value =
(boost::is_convertible<A, StringPiece>::value
&& boost::is_same<B, StringPiece>::value)
||
(boost::is_convertible<B, StringPiece>::value
&& boost::is_same<A, StringPiece>::value)
};
};
} // namespace detail
/**
* operator== through conversion for Range<const char*>
*/
template <class T, class U>
typename
boost::enable_if_c<detail::ComparableAsStringPiece<T, U>::value, bool>::type
operator==(const T& lhs, const U& rhs) {
return StringPiece(lhs) == StringPiece(rhs);
}
/**
* operator< through conversion for Range<const char*>
*/
template <class T, class U>
typename
boost::enable_if_c<detail::ComparableAsStringPiece<T, U>::value, bool>::type
operator<(const T& lhs, const U& rhs) {
return StringPiece(lhs) < StringPiece(rhs);
}
/**
* operator> through conversion for Range<const char*>
*/
template <class T, class U>
typename
boost::enable_if_c<detail::ComparableAsStringPiece<T, U>::value, bool>::type
operator>(const T& lhs, const U& rhs) {
return StringPiece(lhs) > StringPiece(rhs);
}
/**
* operator< through conversion for Range<const char*>
*/
template <class T, class U>
typename
boost::enable_if_c<detail::ComparableAsStringPiece<T, U>::value, bool>::type
operator<=(const T& lhs, const U& rhs) {
return StringPiece(lhs) <= StringPiece(rhs);
}
/**
* operator> through conversion for Range<const char*>
*/
template <class T, class U>
typename
boost::enable_if_c<detail::ComparableAsStringPiece<T, U>::value, bool>::type
operator>=(const T& lhs, const U& rhs) {
return StringPiece(lhs) >= StringPiece(rhs);
}
struct StringPieceHash {
std::size_t operator()(const StringPiece& str) const {
return static_cast<std::size_t>(str.hash());
}
};
/**
* Finds substrings faster than brute force by borrowing from Boyer-Moore
*/
template <class T, class Comp>
size_t qfind(const Range<T>& haystack,
const Range<T>& needle,
Comp eq) {
// Don't use std::search, use a Boyer-Moore-like trick by comparing
// the last characters first
auto const nsize = needle.size();
if (haystack.size() < nsize) {
return std::string::npos;
}
if (!nsize) return 0;
auto const nsize_1 = nsize - 1;
auto const lastNeedle = needle[nsize_1];
// Boyer-Moore skip value for the last char in the needle. Zero is
// not a valid value; skip will be computed the first time it's
// needed.
std::string::size_type skip = 0;
auto i = haystack.begin();
auto iEnd = haystack.end() - nsize_1;
while (i < iEnd) {
// Boyer-Moore: match the last element in the needle
while (!eq(i[nsize_1], lastNeedle)) {
if (++i == iEnd) {
// not found
return std::string::npos;
}
}
// Here we know that the last char matches
// Continue in pedestrian mode
for (size_t j = 0; ; ) {
assert(j < nsize);
if (!eq(i[j], needle[j])) {
// Not found, we can skip
// Compute the skip value lazily
if (skip == 0) {
skip = 1;
while (skip <= nsize_1 && !eq(needle[nsize_1 - skip], lastNeedle)) {
++skip;
}
}
i += skip;
break;
}
// Check if done searching
if (++j == nsize) {
// Yay
return i - haystack.begin();
}
}
}
return std::string::npos;
}
namespace detail {
size_t qfind_first_byte_of(const StringPiece& haystack,
const StringPiece& needles);
} // namespace detail
template <class T, class Comp>
size_t qfind_first_of(const Range<T> & haystack,
const Range<T> & needles,
Comp eq) {
auto ret = std::find_first_of(haystack.begin(), haystack.end(),
needles.begin(), needles.end(),
eq);
return ret == haystack.end() ? std::string::npos : ret - haystack.begin();
}
struct AsciiCaseSensitive {
bool operator()(char lhs, char rhs) const {
return lhs == rhs;
}
};
struct AsciiCaseInsensitive {
bool operator()(char lhs, char rhs) const {
return toupper(lhs) == toupper(rhs);
}
};
extern const AsciiCaseSensitive asciiCaseSensitive;
extern const AsciiCaseInsensitive asciiCaseInsensitive;
template <class T>
size_t qfind(const Range<T>& haystack,
const Range<T>& needle) {
return qfind(haystack, needle, asciiCaseSensitive);
}
template <class T>
size_t qfind(const Range<T>& haystack,
const typename Range<T>::value_type& needle) {
auto pos = std::find(haystack.begin(), haystack.end(), needle);
return pos == haystack.end() ? std::string::npos : pos - haystack.data();
}
// specialization for StringPiece
template <>
inline size_t qfind(const Range<const char*>& haystack, const char& needle) {
auto pos = static_cast<const char*>(
::memchr(haystack.data(), needle, haystack.size()));
return pos == nullptr ? std::string::npos : pos - haystack.data();
}
// specialization for ByteRange
template <>
inline size_t qfind(const Range<const unsigned char*>& haystack,
const unsigned char& needle) {
auto pos = static_cast<const unsigned char*>(
::memchr(haystack.data(), needle, haystack.size()));
return pos == nullptr ? std::string::npos : pos - haystack.data();
}
template <class T>
size_t qfind_first_of(const Range<T>& haystack,
const Range<T>& needles) {
return qfind_first_of(haystack, needles, asciiCaseSensitive);
}
// specialization for StringPiece
template <>
inline size_t qfind_first_of(const Range<const char*>& haystack,
const Range<const char*>& needles) {
return detail::qfind_first_byte_of(haystack, needles);
}
// specialization for ByteRange
template <>
inline size_t qfind_first_of(const Range<const unsigned char*>& haystack,
const Range<const unsigned char*>& needles) {
return detail::qfind_first_byte_of(StringPiece(haystack),
StringPiece(needles));
}
} // !namespace folly
FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(folly::Range);
#endif // FOLLY_RANGE_H_