/*
- * Copyright 2013 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* limitations under the License.
*/
-#ifndef FOLLY_PADDED_H_
-#define FOLLY_PADDED_H_
+#pragma once
+#include <algorithm>
#include <cassert>
#include <cstdint>
#include <cstring>
#include <boost/iterator/iterator_adaptor.hpp>
-#include "folly/Portability.h"
+#include <folly/Portability.h>
+#include <folly/ContainerTraits.h>
/**
* Code that aids in storing data aligned on block (possibly cache-line)
: c_(std::move(c)),
lastCount_(lastCount) {
}
+ explicit Adaptor(size_t n, const value_type& value = value_type())
+ : c_(Node::nodeCount(n), fullNode(value)) {
+ const auto count = n % Node::kElementCount;
+ lastCount_ = count != 0 ? count : Node::kElementCount;
+ }
+
Adaptor(const Adaptor&) = default;
Adaptor& operator=(const Adaptor&) = default;
- Adaptor(Adaptor&& other)
+ Adaptor(Adaptor&& other) noexcept
: c_(std::move(other.c_)),
lastCount_(other.lastCount_) {
other.lastCount_ = Node::kElementCount;
return c_.back().data()[lastCount_ - 1];
}
+ template <typename... Args>
+ void emplace_back(Args&&... args) {
+ new (allocate_back()) value_type(std::forward<Args>(args)...);
+ }
+
void push_back(value_type x) {
- if (lastCount_ == Node::kElementCount) {
- c_.push_back(Node());
- lastCount_ = 0;
- }
- c_.back().data()[lastCount_++] = std::move(x);
+ emplace_back(std::move(x));
}
void pop_back() {
assert(n >= 0);
c_.reserve(Node::nodeCount(n));
}
+
size_type capacity() const {
return c_.capacity() * Node::kElementCount;
}
}
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:
+ value_type* allocate_back() {
+ if (lastCount_ == Node::kElementCount) {
+ container_emplace_back_or_push_back(c_);
+ lastCount_ = 0;
+ }
+ return &c_.back().data()[lastCount_++];
+ }
+
+ 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
};
} // namespace padded
} // namespace folly
-
-#endif /* FOLLY_PADDED_H_ */
-