cleanup CompressedList / MutableCompressedList interfaces
authorPhilip Pronin <philipp@fb.com>
Mon, 6 Jul 2015 00:29:39 +0000 (17:29 -0700)
committerSara Golemon <sgolemon@fb.com>
Tue, 7 Jul 2015 17:25:09 +0000 (10:25 -0700)
Summary: Drop those `const_cast<>` hacks.

Reviewed By: @ot

Differential Revision: D2217629

folly/experimental/BitVectorCoding.h
folly/experimental/EliasFanoCoding.h
folly/experimental/test/BitVectorCodingTest.cpp
folly/experimental/test/EliasFanoCodingTest.cpp

index edaea7a59a260f9751386e8b378971a198888b2a..4f6603788fd1452a2db9f932bf5407edfee7aa02 100644 (file)
@@ -58,9 +58,10 @@ struct BitVectorCompressedListBase {
         skipPointers(reinterpret_cast<Pointer>(other.skipPointers)),
         forwardPointers(reinterpret_cast<Pointer>(other.forwardPointers)) {}
 
-  void free() { ::free(const_cast<unsigned char*>(data.data())); }
-
-  size_t getUpperBound() const { return upperBound; }
+  template <class T = Pointer>
+  auto free() -> decltype(::free(T(nullptr))) {
+    return ::free(data.data());
+  }
 
   size_t size = 0;
   size_t upperBound = 0;
@@ -85,6 +86,7 @@ struct BitVectorEncoder {
                 "Value should be unsigned integral");
 
   typedef BitVectorCompressedList CompressedList;
+  typedef MutableBitVectorCompressedList MutableCompressedList;
 
   typedef Value ValueType;
   typedef SkipValue SkipValueType;
@@ -94,10 +96,10 @@ struct BitVectorEncoder {
   static constexpr size_t forwardQuantum = kForwardQuantum;
 
   template <class RandomAccessIterator>
-  static BitVectorCompressedList encode(RandomAccessIterator begin,
-                                        RandomAccessIterator end) {
+  static MutableCompressedList encode(RandomAccessIterator begin,
+                                      RandomAccessIterator end) {
     if (begin == end) {
-      return BitVectorCompressedList();
+      return MutableCompressedList();
     }
     BitVectorEncoder encoder(end - begin, *(end - 1));
     for (; begin != end; ++begin) {
@@ -106,7 +108,7 @@ struct BitVectorEncoder {
     return encoder.finish();
   }
 
-  explicit BitVectorEncoder(const MutableBitVectorCompressedList& result)
+  explicit BitVectorEncoder(const MutableCompressedList& result)
       : bits_(result.bits),
         skipPointers_(result.skipPointers),
         forwardPointers_(result.forwardPointers),
@@ -130,15 +132,15 @@ struct BitVectorEncoder {
       while (skipPointersSize_ < nextSkipPointerSize) {
         auto pos = skipPointersSize_++;
         folly::storeUnaligned<SkipValueType>(
-          skipPointers_ + pos * sizeof(SkipValueType), size_);
+            skipPointers_ + pos * sizeof(SkipValueType), size_);
       }
     }
 
     if (forwardQuantum != 0) {
-      if ( size_ != 0  && (size_ % (forwardQuantum ?: 1) == 0)) {
+      if (size_ != 0 && (size_ % (forwardQuantum ?: 1) == 0)) {
         const auto pos = size_ / (forwardQuantum ?: 1) - 1;
         folly::storeUnaligned<SkipValueType>(
-          forwardPointers_ + pos * sizeof(SkipValueType), value);
+            forwardPointers_ + pos * sizeof(SkipValueType), value);
       }
     }
 
@@ -146,10 +148,10 @@ struct BitVectorEncoder {
     ++size_;
   }
 
-  const BitVectorCompressedList& finish() const {
+  const MutableCompressedList& finish() const {
     CHECK_EQ(size_, result_.size);
     // TODO(ott): Relax this assumption.
-    CHECK_EQ(result_.getUpperBound(), lastValue_);
+    CHECK_EQ(result_.upperBound, lastValue_);
     return result_;
   }
 
@@ -162,7 +164,7 @@ struct BitVectorEncoder {
   size_t size_ = 0;
   size_t skipPointersSize_ = 0;
 
-  BitVectorCompressedList result_;
+  MutableCompressedList result_;
 };
 
 template <class Value,
@@ -195,7 +197,7 @@ struct BitVectorEncoder<Value, SkipValue, kSkipQuantum, kForwardQuantum>::
 
   size_t bytes() const { return bits + skipPointers + forwardPointers; }
 
-  template <typename Range>
+  template <class Range>
   BitVectorCompressedListBase<typename Range::iterator> openList(
       Range& buf) const {
     BitVectorCompressedListBase<typename Range::iterator> result;
@@ -216,7 +218,7 @@ struct BitVectorEncoder<Value, SkipValue, kSkipQuantum, kForwardQuantum>::
     return result;
   }
 
-  MutableBitVectorCompressedList allocList() const {
+  MutableCompressedList allocList() const {
     uint8_t* buf = nullptr;
     if (size > 0) {
       buf = static_cast<uint8_t*>(malloc(bytes() + 7));
@@ -243,7 +245,7 @@ class BitVectorReader {
   typedef typename Encoder::ValueType ValueType;
   typedef typename Encoder::SkipValueType SkipValueType;
 
-  explicit BitVectorReader(const BitVectorCompressedList& list)
+  explicit BitVectorReader(const typename Encoder::CompressedList& list)
       : size_(list.size),
         bits_(list.bits),
         skipPointers_(list.skipPointers),
@@ -255,7 +257,7 @@ class BitVectorReader {
       return;
     }
 
-    upperBound_ = list.getUpperBound();
+    upperBound_ = list.upperBound;
   }
 
   void reset() {
@@ -383,7 +385,6 @@ class BitVectorReader {
   size_t size() const { return size_; }
 
   size_t position() const { return position_; }
-
   ValueType value() const { return value_; }
 
   bool jump(size_t n) {
@@ -434,6 +435,6 @@ class BitVectorReader {
   const uint8_t* const forwardPointers_;
 };
 
-}} // namespaces
+}}  // namespaces
 
-#endif // FOLLY_EXPERIMENTAL_BIT_VECTOR_CODING_H
+#endif  // FOLLY_EXPERIMENTAL_BIT_VECTOR_CODING_H
index c0f121d709c9ad749888be3d0e3c334954222f2d..5d1dcdb308a05998234086c182afffcac78be7fc 100644 (file)
@@ -56,23 +56,21 @@ struct EliasFanoCompressedListBase {
 
   template <class OtherPointer>
   EliasFanoCompressedListBase(
-    const EliasFanoCompressedListBase<OtherPointer>& other)
+      const EliasFanoCompressedListBase<OtherPointer>& other)
       : size(other.size),
         numLowerBits(other.numLowerBits),
         data(other.data),
         skipPointers(reinterpret_cast<Pointer>(other.skipPointers)),
         forwardPointers(reinterpret_cast<Pointer>(other.forwardPointers)),
         lower(reinterpret_cast<Pointer>(other.lower)),
-        upper(reinterpret_cast<Pointer>(other.upper)) {
-  }
+        upper(reinterpret_cast<Pointer>(other.upper)) { }
 
-  void free() {
-    ::free(const_cast<unsigned char*>(data.data()));
+  template <class T = Pointer>
+  auto free() -> decltype(::free(T(nullptr))) {
+    return ::free(data.data());
   }
 
-  size_t upperSize() const {
-    return data.end() - upper;
-  }
+  size_t upperSize() const { return data.end() - upper; }
 
   size_t size = 0;
   uint8_t numLowerBits = 0;
@@ -96,10 +94,11 @@ template <class Value,
           size_t kForwardQuantum = 0>  // 0 = disabled
 struct EliasFanoEncoderV2 {
   static_assert(std::is_integral<Value>::value &&
-                std::is_unsigned<Value>::value,
+                    std::is_unsigned<Value>::value,
                 "Value should be unsigned integral");
 
   typedef EliasFanoCompressedList CompressedList;
+  typedef MutableEliasFanoCompressedList MutableCompressedList;
 
   typedef Value ValueType;
   typedef SkipValue SkipValueType;
@@ -122,10 +121,10 @@ struct EliasFanoEncoderV2 {
   // EliasFanoCompressedList has no ownership of it, you need to call
   // free() explicitly.
   template <class RandomAccessIterator>
-  static EliasFanoCompressedList encode(RandomAccessIterator begin,
-                                        RandomAccessIterator end) {
+  static MutableCompressedList encode(RandomAccessIterator begin,
+                                      RandomAccessIterator end) {
     if (begin == end) {
-      return EliasFanoCompressedList();
+      return MutableCompressedList();
     }
     EliasFanoEncoderV2 encoder(end - begin, *(end - 1));
     for (; begin != end; ++begin) {
@@ -134,21 +133,20 @@ struct EliasFanoEncoderV2 {
     return encoder.finish();
   }
 
-  explicit EliasFanoEncoderV2(const MutableEliasFanoCompressedList& result)
+  explicit EliasFanoEncoderV2(const MutableCompressedList& result)
       : lower_(result.lower),
         upper_(result.upper),
         skipPointers_(reinterpret_cast<SkipValueType*>(
-                        result.skipPointers)),
+              result.skipPointers)),
         forwardPointers_(reinterpret_cast<SkipValueType*>(
-                           result.forwardPointers)),
+              result.forwardPointers)),
         result_(result) {
     memset(result.data.data(), 0, result.data.size());
   }
 
   EliasFanoEncoderV2(size_t size, ValueType upperBound)
       : EliasFanoEncoderV2(
-            Layout::fromUpperBoundAndSize(upperBound, size).allocList()) {
-  }
+            Layout::fromUpperBoundAndSize(upperBound, size).allocList()) { }
 
   void add(ValueType value) {
     CHECK_GE(value, lastValue_);
@@ -184,7 +182,7 @@ struct EliasFanoEncoderV2 {
     ++size_;
   }
 
-  const EliasFanoCompressedList& finish() const {
+  const MutableCompressedList& finish() const {
     CHECK_EQ(size_, result_.size);
     return result_;
   }
@@ -210,7 +208,7 @@ struct EliasFanoEncoderV2 {
   size_t size_ = 0;
   size_t skipPointersSize_ = 0;
 
-  EliasFanoCompressedList result_;
+  MutableCompressedList result_;
 };
 
 template <class Value,
@@ -283,7 +281,7 @@ struct EliasFanoEncoderV2<Value,
     return lower + upper + skipPointers + forwardPointers;
   }
 
-  template <typename Range>
+  template <class Range>
   EliasFanoCompressedListBase<typename Range::iterator>
   openList(Range& buf) const {
     EliasFanoCompressedListBase<typename Range::iterator> result;
@@ -305,7 +303,7 @@ struct EliasFanoEncoderV2<Value,
     return result;
   }
 
-  MutableEliasFanoCompressedList allocList() const {
+  MutableCompressedList allocList() const {
     uint8_t* buf = nullptr;
     // WARNING: Current read/write logic assumes that the 7 bytes
     // following the last byte of lower and upper sequences are
@@ -337,10 +335,10 @@ class UpperBitsReader {
  public:
   typedef typename Encoder::ValueType ValueType;
 
-  explicit UpperBitsReader(const EliasFanoCompressedList& list)
-    : forwardPointers_(list.forwardPointers),
-      skipPointers_(list.skipPointers),
-      start_(list.upper) {
+  explicit UpperBitsReader(const typename Encoder::CompressedList& list)
+      : forwardPointers_(list.forwardPointers),
+        skipPointers_(list.skipPointers),
+        start_(list.upper) {
     reset();
   }
 
@@ -529,7 +527,7 @@ class EliasFanoReader {
   typedef Encoder EncoderType;
   typedef typename Encoder::ValueType ValueType;
 
-  explicit EliasFanoReader(const EliasFanoCompressedList& list)
+  explicit EliasFanoReader(const typename Encoder::CompressedList& list)
       : size_(list.size),
         lower_(list.lower),
         upper_(list),
index 1bbc3b7c2739dcc5784100075c13ee9928c94d1a..ff8e0fd6284c9e3479a915cd775b32dd221b5ce4 100644 (file)
@@ -81,7 +81,7 @@ std::vector<size_t> order;
 std::vector<uint32_t> encodeSmallData;
 std::vector<uint32_t> encodeLargeData;
 
-typename Encoder::CompressedList list;
+typename Encoder::MutableCompressedList list;
 
 void init() {
   std::mt19937 gen;
index 2e53ea425e8bf49bbc1f7c404bf65b62fc2e6fae..3a5b83d4cda655fdef312212960e96f4ddf5a66b 100644 (file)
@@ -81,7 +81,7 @@ std::vector<size_t> order;
 std::vector<uint32_t> encodeSmallData;
 std::vector<uint32_t> encodeLargeData;
 
-typename Encoder::CompressedList list;
+typename Encoder::MutableCompressedList list;
 
 void init() {
   std::mt19937 gen;