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