add support for whenAll to waitWithSemaphore
[folly.git] / folly / Padded.h
index f4d3b9c7af57d72deb2f644d015bf2e4fbb61ccf..3bf87a6c9e3b726697b23f78cae5de1f0c3d0009 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2013 Facebook, Inc.
+ * Copyright 2014 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -343,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)
@@ -444,6 +449,7 @@ class Adaptor {
     assert(n >= 0);
     c_.reserve(Node::nodeCount(n));
   }
+
   size_type capacity() const {
     return c_.capacity() * Node::kElementCount;
   }
@@ -475,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
 };