Fix typo - ac defined are prefixed with FOLLY_
[folly.git] / folly / Padded.h
index 4c0af2afbf64edcc5020fd379dccdbe2091f6f25..c5b53a7c94ad88c6aef460e29ffd6193dd18046d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2012 Facebook, Inc.
+ * Copyright 2015 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
 
 #include <cassert>
 #include <cstdint>
+#include <cstring>
 #include <functional>
 #include <iterator>
 #include <limits>
@@ -26,7 +27,7 @@
 
 #include <boost/iterator/iterator_adaptor.hpp>
 
-#include "folly/Portability.h"
+#include <folly/Portability.h>
 
 /**
  * Code that aids in storing data aligned on block (possibly cache-line)
@@ -342,6 +343,11 @@ class Adaptor {
     : c_(std::move(c)),
       lastCount_(lastCount) {
   }
+  explicit Adaptor(size_t n, const value_type& value = value_type())
+    : c_(Node::nodeCount(n), fullNode(value)),
+      lastCount_(n % Node::kElementCount ?: Node::kElementCount) {
+  }
+
   Adaptor(const Adaptor&) = default;
   Adaptor& operator=(const Adaptor&) = default;
   Adaptor(Adaptor&& other)
@@ -443,6 +449,7 @@ class Adaptor {
     assert(n >= 0);
     c_.reserve(Node::nodeCount(n));
   }
+
   size_type capacity() const {
     return c_.capacity() * Node::kElementCount;
   }
@@ -474,12 +481,20 @@ class Adaptor {
   }
 
   void padToFullNode(const value_type& padValue) {
-    while (lastCount_ != Node::kElementCount) {
-      push_back(padValue);
+    // the if is necessary because c_ may be empty so we can't call c_.back()
+    if (lastCount_ != Node::kElementCount) {
+      auto last = c_.back().data();
+      std::fill(last + lastCount_, last + Node::kElementCount, padValue);
+      lastCount_ = Node::kElementCount;
     }
   }
 
  private:
+  static Node fullNode(const value_type& value) {
+    Node n;
+    std::fill(n.data(), n.data() + kElementsPerNode, value);
+    return n;
+  }
   Container c_;  // container of Nodes
   size_t lastCount_;  // number of elements in last Node
 };
@@ -488,4 +503,3 @@ class Adaptor {
 }  // namespace folly
 
 #endif /* FOLLY_PADDED_H_ */
-