Consistency in namespace-closing comments
[folly.git] / folly / experimental / EliasFanoCoding.h
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * @author Philip Pronin (philipp@fb.com)
19  *
20  * Based on the paper by Sebastiano Vigna,
21  * "Quasi-succinct indices" (arxiv:1206.4300).
22  */
23
24 #pragma once
25
26 #include <algorithm>
27 #include <cstdlib>
28 #include <limits>
29 #include <type_traits>
30
31 #include <folly/Assume.h>
32 #include <folly/Bits.h>
33 #include <folly/Likely.h>
34 #include <folly/Portability.h>
35 #include <folly/Range.h>
36 #include <folly/experimental/CodingDetail.h>
37 #include <folly/experimental/Instructions.h>
38 #include <folly/experimental/Select64.h>
39 #include <glog/logging.h>
40
41 #if !FOLLY_X64
42 #error EliasFanoCoding.h requires x86_64
43 #endif
44
45 namespace folly { namespace compression {
46
47 static_assert(kIsLittleEndian, "EliasFanoCoding.h requires little endianness");
48
49 template <class Pointer>
50 struct EliasFanoCompressedListBase {
51   EliasFanoCompressedListBase() = default;
52
53   template <class OtherPointer>
54   EliasFanoCompressedListBase(
55       const EliasFanoCompressedListBase<OtherPointer>& other)
56       : size(other.size),
57         numLowerBits(other.numLowerBits),
58         data(other.data),
59         skipPointers(reinterpret_cast<Pointer>(other.skipPointers)),
60         forwardPointers(reinterpret_cast<Pointer>(other.forwardPointers)),
61         lower(reinterpret_cast<Pointer>(other.lower)),
62         upper(reinterpret_cast<Pointer>(other.upper)) { }
63
64   template <class T = Pointer>
65   auto free() -> decltype(::free(T(nullptr))) {
66     return ::free(data.data());
67   }
68
69   size_t upperSize() const {
70     return size_t(data.end() - upper);
71   }
72
73   size_t size = 0;
74   uint8_t numLowerBits = 0;
75
76   // WARNING: EliasFanoCompressedList has no ownership of data. The 7
77   // bytes following the last byte should be readable.
78   folly::Range<Pointer> data;
79
80   Pointer skipPointers = nullptr;
81   Pointer forwardPointers = nullptr;
82   Pointer lower = nullptr;
83   Pointer upper = nullptr;
84 };
85
86 typedef EliasFanoCompressedListBase<const uint8_t*> EliasFanoCompressedList;
87 typedef EliasFanoCompressedListBase<uint8_t*> MutableEliasFanoCompressedList;
88
89 template <
90     class Value,
91     class SkipValue = size_t,
92     size_t kSkipQuantum = 0, // 0 = disabled
93     size_t kForwardQuantum = 0> // 0 = disabled
94 struct EliasFanoEncoderV2 {
95   static_assert(std::is_integral<Value>::value &&
96                     std::is_unsigned<Value>::value,
97                 "Value should be unsigned integral");
98
99   typedef EliasFanoCompressedList CompressedList;
100   typedef MutableEliasFanoCompressedList MutableCompressedList;
101
102   typedef Value ValueType;
103   typedef SkipValue SkipValueType;
104   struct Layout;
105
106   static constexpr size_t skipQuantum = kSkipQuantum;
107   static constexpr size_t forwardQuantum = kForwardQuantum;
108
109   static uint8_t defaultNumLowerBits(size_t upperBound, size_t size) {
110     if (UNLIKELY(size == 0 || upperBound < size)) {
111       return 0;
112     }
113     // Result that should be returned is "floor(log(upperBound / size))".
114     // In order to avoid expensive division, we rely on
115     // "floor(a) - floor(b) - 1 <= floor(a - b) <= floor(a) - floor(b)".
116     // Assuming "candidate = floor(log(upperBound)) - floor(log(upperBound))",
117     // then result is either "candidate - 1" or "candidate".
118     auto candidate = folly::findLastSet(upperBound) - folly::findLastSet(size);
119     // NOTE: As size != 0, "candidate" is always < 64.
120     return (size > (upperBound >> candidate)) ? candidate - 1 : candidate;
121   }
122
123   // Requires: input range (begin, end) is sorted (encoding
124   // crashes if it's not).
125   // WARNING: encode() mallocates EliasFanoCompressedList::data. As
126   // EliasFanoCompressedList has no ownership of it, you need to call
127   // free() explicitly.
128   template <class RandomAccessIterator>
129   static MutableCompressedList encode(RandomAccessIterator begin,
130                                       RandomAccessIterator end) {
131     if (begin == end) {
132       return MutableCompressedList();
133     }
134     EliasFanoEncoderV2 encoder(size_t(end - begin), *(end - 1));
135     for (; begin != end; ++begin) {
136       encoder.add(*begin);
137     }
138     return encoder.finish();
139   }
140
141   explicit EliasFanoEncoderV2(const MutableCompressedList& result)
142       : lower_(result.lower),
143         upper_(result.upper),
144         skipPointers_(reinterpret_cast<SkipValueType*>(
145               result.skipPointers)),
146         forwardPointers_(reinterpret_cast<SkipValueType*>(
147               result.forwardPointers)),
148         result_(result) {
149     std::fill(result.data.begin(), result.data.end(), '\0');
150   }
151
152   EliasFanoEncoderV2(size_t size, ValueType upperBound)
153       : EliasFanoEncoderV2(
154             Layout::fromUpperBoundAndSize(upperBound, size).allocList()) { }
155
156   void add(ValueType value) {
157     CHECK_LT(value, std::numeric_limits<ValueType>::max());
158     CHECK_GE(value, lastValue_);
159
160     const auto numLowerBits = result_.numLowerBits;
161     const ValueType upperBits = value >> numLowerBits;
162
163     // Upper sequence consists of upperBits 0-bits and (size_ + 1) 1-bits.
164     const size_t pos = upperBits + size_;
165     upper_[pos / 8] |= 1U << (pos % 8);
166     // Append numLowerBits bits to lower sequence.
167     if (numLowerBits != 0) {
168       const ValueType lowerBits = value & ((ValueType(1) << numLowerBits) - 1);
169       writeBits56(lower_, size_ * numLowerBits, numLowerBits, lowerBits);
170     }
171
172     /* static */ if (skipQuantum != 0) {
173       while ((skipPointersSize_ + 1) * skipQuantum <= upperBits) {
174         // Store the number of preceding 1-bits.
175         skipPointers_[skipPointersSize_++] = SkipValue(size_);
176       }
177     }
178
179     /* static */ if (forwardQuantum != 0) {
180       if ((size_ + 1) % forwardQuantum == 0) {
181         const auto k = size_ / forwardQuantum;
182         // Store the number of preceding 0-bits.
183         forwardPointers_[k] = upperBits;
184       }
185     }
186
187     lastValue_ = value;
188     ++size_;
189   }
190
191   const MutableCompressedList& finish() const {
192     CHECK_EQ(size_, result_.size);
193     return result_;
194   }
195
196  private:
197   // Writes value (with len up to 56 bits) to data starting at pos-th bit.
198   static void writeBits56(unsigned char* data, size_t pos,
199                           uint8_t len, uint64_t value) {
200     DCHECK_LE(uint32_t(len), 56);
201     DCHECK_EQ(0, value & ~((uint64_t(1) << len) - 1));
202     unsigned char* const ptr = data + (pos / 8);
203     uint64_t ptrv = folly::loadUnaligned<uint64_t>(ptr);
204     ptrv |= value << (pos % 8);
205     folly::storeUnaligned<uint64_t>(ptr, ptrv);
206   }
207
208   unsigned char* lower_ = nullptr;
209   unsigned char* upper_ = nullptr;
210   SkipValueType* skipPointers_ = nullptr;
211   SkipValueType* forwardPointers_ = nullptr;
212
213   ValueType lastValue_ = 0;
214   size_t size_ = 0;
215   size_t skipPointersSize_ = 0;
216
217   MutableCompressedList result_;
218 };
219
220 template <
221     class Value,
222     class SkipValue,
223     size_t kSkipQuantum,
224     size_t kForwardQuantum>
225 struct EliasFanoEncoderV2<Value,
226                           SkipValue,
227                           kSkipQuantum,
228                           kForwardQuantum>::Layout {
229   static Layout fromUpperBoundAndSize(size_t upperBound, size_t size) {
230     // numLowerBits can be at most 56 because of detail::writeBits56.
231     const uint8_t numLowerBits = std::min(defaultNumLowerBits(upperBound,
232                                                               size),
233                                           uint8_t(56));
234     // *** Upper bits.
235     // Upper bits are stored using unary delta encoding.
236     // For example, (3 5 5 9) will be encoded as 1000011001000_2.
237     const size_t upperSizeBits =
238       (upperBound >> numLowerBits) +  // Number of 0-bits to be stored.
239       size;                           // 1-bits.
240     const size_t upper = (upperSizeBits + 7) / 8;
241
242     // *** Validity checks.
243     // Shift by numLowerBits must be valid.
244     CHECK_LT(numLowerBits, 8 * sizeof(Value));
245     CHECK_LT(size, std::numeric_limits<SkipValueType>::max());
246     CHECK_LT(upperBound >> numLowerBits,
247              std::numeric_limits<SkipValueType>::max());
248
249     return fromInternalSizes(numLowerBits, upper, size);
250   }
251
252   static Layout fromInternalSizes(uint8_t numLowerBits,
253                                   size_t upper,
254                                   size_t size) {
255     Layout layout;
256     layout.size = size;
257     layout.numLowerBits = numLowerBits;
258
259     layout.lower = (numLowerBits * size + 7) / 8;
260     layout.upper = upper;
261
262     // *** Skip pointers.
263     // Store (1-indexed) position of every skipQuantum-th
264     // 0-bit in upper bits sequence.
265     /* static */ if (skipQuantum != 0) {
266       // 8 * upper is used here instead of upperSizeBits, as that is
267       // more serialization-friendly way (upperSizeBits doesn't need
268       // to be known by this function, unlike upper).
269
270       size_t numSkipPointers = (8 * upper - size) / skipQuantum;
271       layout.skipPointers = numSkipPointers * sizeof(SkipValueType);
272     }
273
274     // *** Forward pointers.
275     // Store (1-indexed) position of every forwardQuantum-th
276     // 1-bit in upper bits sequence.
277     /* static */ if (forwardQuantum != 0) {
278       size_t numForwardPointers = size / forwardQuantum;
279       layout.forwardPointers = numForwardPointers * sizeof(SkipValueType);
280     }
281
282     return layout;
283   }
284
285   size_t bytes() const {
286     return lower + upper + skipPointers + forwardPointers;
287   }
288
289   template <class Range>
290   EliasFanoCompressedListBase<typename Range::iterator>
291   openList(Range& buf) const {
292     EliasFanoCompressedListBase<typename Range::iterator> result;
293     result.size = size;
294     result.numLowerBits = numLowerBits;
295     result.data = buf.subpiece(0, bytes());
296
297     auto advance = [&] (size_t n) {
298       auto begin = buf.data();
299       buf.advance(n);
300       return begin;
301     };
302
303     result.skipPointers = advance(skipPointers);
304     result.forwardPointers = advance(forwardPointers);
305     result.lower = advance(lower);
306     result.upper = advance(upper);
307
308     return result;
309   }
310
311   MutableCompressedList allocList() const {
312     uint8_t* buf = nullptr;
313     // WARNING: Current read/write logic assumes that the 7 bytes
314     // following the last byte of lower and upper sequences are
315     // readable (stored value doesn't matter and won't be changed), so
316     // we allocate additional 7 bytes, but do not include them in size
317     // of returned value.
318     if (size > 0) {
319       buf = static_cast<uint8_t*>(malloc(bytes() + 7));
320     }
321     folly::MutableByteRange bufRange(buf, bytes());
322     return openList(bufRange);
323   }
324
325   size_t size = 0;
326   uint8_t numLowerBits = 0;
327
328   // Sizes in bytes.
329   size_t lower = 0;
330   size_t upper = 0;
331   size_t skipPointers = 0;
332   size_t forwardPointers = 0;
333 };
334
335 namespace detail {
336
337 template <class Encoder, class Instructions, class SizeType>
338 class UpperBitsReader : ForwardPointers<Encoder::forwardQuantum>,
339                         SkipPointers<Encoder::skipQuantum> {
340   typedef typename Encoder::SkipValueType SkipValueType;
341  public:
342   typedef typename Encoder::ValueType ValueType;
343
344   explicit UpperBitsReader(const typename Encoder::CompressedList& list)
345       : ForwardPointers<Encoder::forwardQuantum>(list.forwardPointers),
346         SkipPointers<Encoder::skipQuantum>(list.skipPointers),
347         start_(list.upper) {
348     reset();
349   }
350
351   void reset() {
352     block_ = start_ != nullptr ? folly::loadUnaligned<block_t>(start_) : 0;
353     position_ = std::numeric_limits<SizeType>::max();
354     outer_ = 0;
355     value_ = 0;
356   }
357
358   SizeType position() const {
359     return position_;
360   }
361   ValueType value() const {
362     return value_;
363   }
364
365   ValueType next() {
366     // Skip to the first non-zero block.
367     while (block_ == 0) {
368       outer_ += sizeof(block_t);
369       block_ = folly::loadUnaligned<block_t>(start_ + outer_);
370     }
371
372     ++position_;
373     size_t inner = Instructions::ctz(block_);
374     block_ = Instructions::blsr(block_);
375
376     return setValue(inner);
377   }
378
379   ValueType skip(SizeType n) {
380     DCHECK_GT(n, 0);
381
382     position_ += n;  // n 1-bits will be read.
383
384     // Use forward pointer.
385     if (Encoder::forwardQuantum > 0 && n > Encoder::forwardQuantum) {
386       const size_t steps = position_ / Encoder::forwardQuantum;
387       const size_t dest = folly::loadUnaligned<SkipValueType>(
388           this->forwardPointers_ + (steps - 1) * sizeof(SkipValueType));
389
390       reposition(dest + steps * Encoder::forwardQuantum);
391       n = position_ + 1 - steps * Encoder::forwardQuantum; // n is > 0.
392     }
393
394     size_t cnt;
395     // Find necessary block.
396     while ((cnt = Instructions::popcount(block_)) < n) {
397       n -= cnt;
398       outer_ += sizeof(block_t);
399       block_ = folly::loadUnaligned<block_t>(start_ + outer_);
400     }
401
402     // Skip to the n-th one in the block.
403     DCHECK_GT(n, 0);
404     size_t inner = select64<Instructions>(block_, n - 1);
405     block_ &= (block_t(-1) << inner) << 1;
406
407     return setValue(inner);
408   }
409
410   // Skip to the first element that is >= v and located *after* the current
411   // one (so even if current value equals v, position will be increased by 1).
412   ValueType skipToNext(ValueType v) {
413     DCHECK_GE(v, value_);
414
415     // Use skip pointer.
416     if (Encoder::skipQuantum > 0 && v >= value_ + Encoder::skipQuantum) {
417       const size_t steps = v / Encoder::skipQuantum;
418       const size_t dest = folly::loadUnaligned<SkipValueType>(
419           this->skipPointers_ + (steps - 1) * sizeof(SkipValueType));
420
421       reposition(dest + Encoder::skipQuantum * steps);
422       position_ = dest - 1;
423
424       // Correct value_ will be set during the next() call at the end.
425
426       // NOTE: Corresponding block of lower bits sequence may be
427       // prefetched here (via __builtin_prefetch), but experiments
428       // didn't show any significant improvements.
429     }
430
431     // Skip by blocks.
432     size_t cnt;
433     size_t skip = v - (8 * outer_ - position_ - 1);
434
435     constexpr size_t kBitsPerBlock = 8 * sizeof(block_t);
436     while ((cnt = Instructions::popcount(~block_)) < skip) {
437       skip -= cnt;
438       position_ += kBitsPerBlock - cnt;
439       outer_ += sizeof(block_t);
440       block_ = folly::loadUnaligned<block_t>(start_ + outer_);
441     }
442
443     if (LIKELY(skip)) {
444       auto inner = select64<Instructions>(~block_, skip - 1);
445       position_ += inner - skip + 1;
446       block_ &= block_t(-1) << inner;
447     }
448
449     next();
450     return value_;
451   }
452
453   ValueType jump(size_t n) {
454     if (Encoder::forwardQuantum == 0 || n <= Encoder::forwardQuantum) {
455       reset();
456     } else {
457       // Avoid reading the head, skip() will reposition.
458       position_ = std::numeric_limits<SizeType>::max();
459     }
460     return skip(n);
461   }
462
463   ValueType jumpToNext(ValueType v) {
464     if (Encoder::skipQuantum == 0 || v < Encoder::skipQuantum) {
465       reset();
466     } else {
467       value_ = 0;  // Avoid reading the head, skipToNext() will reposition.
468     }
469     return skipToNext(v);
470   }
471
472   ValueType previousValue() const {
473     DCHECK_NE(position(), std::numeric_limits<SizeType>::max());
474     DCHECK_GT(position(), 0);
475
476     auto outer = outer_;
477     auto inner = size_t(value_) - 8 * outer_ + position_;
478     block_t block = folly::loadUnaligned<block_t>(start_ + outer);
479     block &= (block_t(1) << inner) - 1;
480
481     while (UNLIKELY(block == 0)) {
482       DCHECK_GT(outer, 0);
483       outer -= std::min<OuterType>(sizeof(block_t), outer);
484       block = folly::loadUnaligned<block_t>(start_ + outer);
485     }
486
487     inner = 8 * sizeof(block_t) - 1 - Instructions::clz(block);
488     return static_cast<ValueType>(8 * outer + inner - (position_ - 1));
489   }
490
491   void setDone(SizeType endPos) {
492     position_ = endPos;
493   }
494
495  private:
496   ValueType setValue(size_t inner) {
497     value_ = static_cast<ValueType>(8 * outer_ + inner - position_);
498     return value_;
499   }
500
501   void reposition(SizeType dest) {
502     outer_ = dest / 8;
503     block_ = folly::loadUnaligned<block_t>(start_ + outer_);
504     block_ &= ~((block_t(1) << (dest % 8)) - 1);
505   }
506
507   using block_t = uint64_t;
508   // The size in bytes of the upper bits is limited by n + universe / 8,
509   // so a type that can hold either sizes or values is sufficient.
510   using OuterType = typename std::common_type<ValueType, SizeType>::type;
511
512   const unsigned char* const start_;
513   block_t block_;
514   SizeType position_; // Index of current value (= #reads - 1).
515   OuterType outer_; // Outer offset: number of consumed bytes in upper.
516   ValueType value_;
517 };
518
519 } // namespace detail
520
521 // If kUnchecked = true the caller must guarantee that all the
522 // operations return valid elements, i.e., they would never return
523 // false if checked.
524 template <
525     class Encoder,
526     class Instructions = instructions::Default,
527     bool kUnchecked = false,
528     class SizeType = size_t>
529 class EliasFanoReader {
530  public:
531   typedef Encoder EncoderType;
532   typedef typename Encoder::ValueType ValueType;
533
534   explicit EliasFanoReader(const typename Encoder::CompressedList& list)
535       : upper_(list),
536         lower_(list.lower),
537         size_(list.size),
538         numLowerBits_(list.numLowerBits) {
539     DCHECK(Instructions::supported());
540     // To avoid extra branching during skipTo() while reading
541     // upper sequence we need to know the last element.
542     // If kUnchecked == true, we do not check that skipTo() is called
543     // within the bounds, so we can avoid initializing lastValue_.
544     if (kUnchecked || UNLIKELY(list.size == 0)) {
545       lastValue_ = 0;
546       return;
547     }
548     ValueType lastUpperValue = ValueType(8 * list.upperSize() - size_);
549     auto it = list.upper + list.upperSize() - 1;
550     DCHECK_NE(*it, 0);
551     lastUpperValue -= 8 - folly::findLastSet(*it);
552     lastValue_ = readLowerPart(size_ - 1) | (lastUpperValue << numLowerBits_);
553   }
554
555   void reset() {
556     upper_.reset();
557     value_ = kInvalidValue;
558   }
559
560   bool next() {
561     if (!kUnchecked && UNLIKELY(position() + 1 >= size_)) {
562       return setDone();
563     }
564     upper_.next();
565     value_ = readLowerPart(upper_.position()) |
566              (upper_.value() << numLowerBits_);
567     return true;
568   }
569
570   bool skip(SizeType n) {
571     CHECK_GT(n, 0);
572
573     if (kUnchecked || LIKELY(position() + n < size_)) {
574       if (LIKELY(n < kLinearScanThreshold)) {
575         for (SizeType i = 0; i < n; ++i)
576           upper_.next();
577       } else {
578         upper_.skip(n);
579       }
580       value_ = readLowerPart(upper_.position()) |
581         (upper_.value() << numLowerBits_);
582       return true;
583     }
584
585     return setDone();
586   }
587
588   bool skipTo(ValueType value) {
589     // Also works when value_ == kInvalidValue.
590     if (value != kInvalidValue) { DCHECK_GE(value + 1, value_ + 1); }
591
592     if (!kUnchecked && value > lastValue_) {
593       return setDone();
594     } else if (value == value_) {
595       return true;
596     }
597
598     ValueType upperValue = (value >> numLowerBits_);
599     ValueType upperSkip = upperValue - upper_.value();
600     // The average density of ones in upper bits is 1/2.
601     // LIKELY here seems to make things worse, even for small skips.
602     if (upperSkip < 2 * kLinearScanThreshold) {
603       do {
604         upper_.next();
605       } while (UNLIKELY(upper_.value() < upperValue));
606     } else {
607       upper_.skipToNext(upperValue);
608     }
609
610     iterateTo(value);
611     return true;
612   }
613
614   bool jump(SizeType n) {
615     if (LIKELY(n < size_)) {  // Also checks that n != -1.
616       value_ = readLowerPart(n) | (upper_.jump(n + 1) << numLowerBits_);
617       return true;
618     }
619     return setDone();
620   }
621
622   bool jumpTo(ValueType value) {
623     if (!kUnchecked && value > lastValue_) {
624       return setDone();
625     }
626
627     upper_.jumpToNext(value >> numLowerBits_);
628     iterateTo(value);
629     return true;
630   }
631
632   ValueType previousValue() const {
633     DCHECK_GT(position(), 0);
634     DCHECK_LT(position(), size());
635     return readLowerPart(upper_.position() - 1) |
636       (upper_.previousValue() << numLowerBits_);
637   }
638
639   SizeType size() const {
640     return size_;
641   }
642
643   bool valid() const {
644     return position() < size(); // Also checks that position() != -1.
645   }
646
647   SizeType position() const {
648     return upper_.position();
649   }
650   ValueType value() const {
651     DCHECK(valid());
652     return value_;
653   }
654
655  private:
656   // Must hold kInvalidValue + 1 == 0.
657   constexpr static ValueType kInvalidValue =
658       std::numeric_limits<ValueType>::max();
659
660   bool setDone() {
661     value_ = kInvalidValue;
662     upper_.setDone(size_);
663     return false;
664   }
665
666   ValueType readLowerPart(SizeType i) const {
667     DCHECK_LT(i, size_);
668     const size_t pos = i * numLowerBits_;
669     const unsigned char* ptr = lower_ + (pos / 8);
670     const uint64_t ptrv = folly::loadUnaligned<uint64_t>(ptr);
671     // This removes the branch in the fallback implementation of
672     // bzhi. The condition is verified at encoding time.
673     assume(numLowerBits_ < sizeof(ValueType) * 8);
674     return Instructions::bzhi(ptrv >> (pos % 8), numLowerBits_);
675   }
676
677   void iterateTo(ValueType value) {
678     while (true) {
679       value_ = readLowerPart(upper_.position()) |
680         (upper_.value() << numLowerBits_);
681       if (LIKELY(value_ >= value)) break;
682       upper_.next();
683     }
684   }
685
686   constexpr static size_t kLinearScanThreshold = 8;
687
688   detail::UpperBitsReader<Encoder, Instructions, SizeType> upper_;
689   const uint8_t* lower_;
690   SizeType size_;
691   ValueType value_ = kInvalidValue;
692   ValueType lastValue_;
693   uint8_t numLowerBits_;
694 };
695
696 }}  // namespaces