CodeMod: dynamic-init-list-ctor
[folly.git] / folly / experimental / EliasFanoCoding.h
index b428d54384da3d2d0d16772e19f62e25a2cc0ebc..8508f3298a88b77fc34cff9b5a14901605d2de41 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2015 Facebook, Inc.
+ * Copyright 2016 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -21,9 +21,9 @@
  * "Quasi-succinct indices" (arxiv:1206.4300).
  */
 
-#ifndef FOLLY_EXPERIMENTAL_ELIAS_FANO_CODING_H
-#define FOLLY_EXPERIMENTAL_ELIAS_FANO_CODING_H
+#pragma once
 
+#include <algorithm>
 #include <cstdlib>
 #include <limits>
 #include <type_traits>
 #include <folly/experimental/Select64.h>
 #include <glog/logging.h>
 
-#ifndef __GNUC__
-#error EliasFanoCoding.h requires GCC
-#endif
-
 #if !FOLLY_X64
 #error EliasFanoCoding.h requires x86_64
 #endif
 
-#if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
-#error EliasFanoCoding.h requires little endianness
-#endif
-
 namespace folly { namespace compression {
 
+static_assert(kIsLittleEndian, "EliasFanoCoding.h requires little endianness");
+
 template <class Pointer>
 struct EliasFanoCompressedListBase {
   EliasFanoCompressedListBase() = default;
@@ -141,7 +135,7 @@ struct EliasFanoEncoderV2 {
         forwardPointers_(reinterpret_cast<SkipValueType*>(
               result.forwardPointers)),
         result_(result) {
-    memset(result.data.data(), 0, result.data.size());
+    std::fill(result.data.begin(), result.data.end(), 0);
   }
 
   EliasFanoEncoderV2(size_t size, ValueType upperBound)
@@ -261,9 +255,7 @@ struct EliasFanoEncoderV2<Value,
       // more serialization-friendly way (upperSizeBits doesn't need
       // to be known by this function, unlike upper).
 
-      // '?: 1' is a workaround for false 'division by zero'
-      // compile-time error.
-      size_t numSkipPointers = (8 * upper - size) / (skipQuantum ?: 1);
+      size_t numSkipPointers = (8 * upper - size) / skipQuantum;
       layout.skipPointers = numSkipPointers * sizeof(SkipValueType);
     }
 
@@ -271,7 +263,7 @@ struct EliasFanoEncoderV2<Value,
     // Store (1-indexed) position of every forwardQuantum-th
     // 1-bit in upper bits sequence.
     /* static */ if (forwardQuantum != 0) {
-      size_t numForwardPointers = size / (forwardQuantum ?: 1);
+      size_t numForwardPointers = size / forwardQuantum;
       layout.forwardPointers = numForwardPointers * sizeof(SkipValueType);
     }
 
@@ -375,16 +367,13 @@ class UpperBitsReader {
 
     // Use forward pointer.
     if (Encoder::forwardQuantum > 0 && n > Encoder::forwardQuantum) {
-      // Workaround to avoid 'division by zero' compile-time error.
-      constexpr size_t q = Encoder::forwardQuantum ?: 1;
-
-      const size_t steps = position_ / q;
+      const size_t steps = position_ / Encoder::forwardQuantum;
       const size_t dest =
         folly::loadUnaligned<SkipValueType>(
             forwardPointers_ + (steps - 1) * sizeof(SkipValueType));
 
-      reposition(dest + steps * q);
-      n = position_ + 1 - steps * q;  // n is > 0.
+      reposition(dest + steps * Encoder::forwardQuantum);
+      n = position_ + 1 - steps * Encoder::forwardQuantum; // n is > 0.
       // Correct inner_ will be set at the end.
     }
 
@@ -411,15 +400,12 @@ class UpperBitsReader {
 
     // Use skip pointer.
     if (Encoder::skipQuantum > 0 && v >= value_ + Encoder::skipQuantum) {
-      // Workaround to avoid 'division by zero' compile-time error.
-      constexpr size_t q = Encoder::skipQuantum ?: 1;
-
-      const size_t steps = v / q;
+      const size_t steps = v / Encoder::skipQuantum;
       const size_t dest =
         folly::loadUnaligned<SkipValueType>(
             skipPointers_ + (steps - 1) * sizeof(SkipValueType));
 
-      reposition(dest + q * steps);
+      reposition(dest + Encoder::skipQuantum * steps);
       position_ = dest - 1;
 
       // Correct inner_ and value_ will be set during the next()
@@ -684,5 +670,3 @@ class EliasFanoReader {
 };
 
 }}  // namespaces
-
-#endif  // FOLLY_EXPERIMENTAL_ELIAS_FANO_CODING_H