FlatHashMap: use same code for const and non-const methods
This commit is contained in:
parent
c88955a8b7
commit
e3a5b29d20
@ -1876,6 +1876,6 @@ void LanguagePackManager::hangup() {
|
|||||||
|
|
||||||
int32 LanguagePackManager::manager_count_ = 0;
|
int32 LanguagePackManager::manager_count_ = 0;
|
||||||
std::mutex LanguagePackManager::language_database_mutex_;
|
std::mutex LanguagePackManager::language_database_mutex_;
|
||||||
FlatHashMap<string, unique_ptr<LanguagePackManager::LanguageDatabase>> LanguagePackManager::language_databases_;
|
std::unordered_map<string, unique_ptr<LanguagePackManager::LanguageDatabase>> LanguagePackManager::language_databases_;
|
||||||
|
|
||||||
} // namespace td
|
} // namespace td
|
||||||
|
@ -105,7 +105,7 @@ class LanguagePackManager final : public NetQueryCallback {
|
|||||||
static int32 manager_count_;
|
static int32 manager_count_;
|
||||||
|
|
||||||
static std::mutex language_database_mutex_;
|
static std::mutex language_database_mutex_;
|
||||||
static FlatHashMap<string, unique_ptr<LanguageDatabase>> language_databases_;
|
static std::unordered_map<string, unique_ptr<LanguageDatabase>> language_databases_;
|
||||||
|
|
||||||
static LanguageDatabase *add_language_database(string path);
|
static LanguageDatabase *add_language_database(string path);
|
||||||
|
|
||||||
|
@ -40,7 +40,6 @@ class FlatHashMapImpl {
|
|||||||
}
|
}
|
||||||
Node(Node &&other) noexcept {
|
Node(Node &&other) noexcept {
|
||||||
*this = std::move(other);
|
*this = std::move(other);
|
||||||
other.first = KeyT{};
|
|
||||||
}
|
}
|
||||||
Node &operator=(Node &&other) noexcept {
|
Node &operator=(Node &&other) noexcept {
|
||||||
DCHECK(empty());
|
DCHECK(empty());
|
||||||
@ -118,6 +117,7 @@ class FlatHashMapImpl {
|
|||||||
NodeIterator it_;
|
NodeIterator it_;
|
||||||
Self *map_;
|
Self *map_;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ConstIterator {
|
struct ConstIterator {
|
||||||
using iterator_category = std::bidirectional_iterator_tag;
|
using iterator_category = std::bidirectional_iterator_tag;
|
||||||
using difference_type = std::ptrdiff_t;
|
using difference_type = std::ptrdiff_t;
|
||||||
@ -127,15 +127,11 @@ class FlatHashMapImpl {
|
|||||||
|
|
||||||
friend class FlatHashMapImpl;
|
friend class FlatHashMapImpl;
|
||||||
ConstIterator &operator++() {
|
ConstIterator &operator++() {
|
||||||
do {
|
++it_;
|
||||||
++it_;
|
|
||||||
} while (it_ != map_->nodes_.end() && it_->empty());
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
ConstIterator &operator--() {
|
ConstIterator &operator--() {
|
||||||
do {
|
--it_;
|
||||||
--it_;
|
|
||||||
} while (it_->empty());
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
const Node &operator*() {
|
const Node &operator*() {
|
||||||
@ -145,20 +141,17 @@ class FlatHashMapImpl {
|
|||||||
return &*it_;
|
return &*it_;
|
||||||
}
|
}
|
||||||
bool operator==(const ConstIterator &other) const {
|
bool operator==(const ConstIterator &other) const {
|
||||||
DCHECK(map_ == other.map_);
|
|
||||||
return it_ == other.it_;
|
return it_ == other.it_;
|
||||||
}
|
}
|
||||||
bool operator!=(const ConstIterator &other) const {
|
bool operator!=(const ConstIterator &other) const {
|
||||||
DCHECK(map_ == other.map_);
|
|
||||||
return it_ != other.it_;
|
return it_ != other.it_;
|
||||||
}
|
}
|
||||||
|
|
||||||
ConstIterator(ConstNodeIterator it, const Self *map) : it_(std::move(it)), map_(map) {
|
explicit ConstIterator(Iterator it) : it_(std::move(it)) {
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ConstNodeIterator it_;
|
Iterator it_;
|
||||||
const Self *map_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
FlatHashMapImpl() = default;
|
FlatHashMapImpl() = default;
|
||||||
@ -196,14 +189,7 @@ class FlatHashMapImpl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ConstIterator find(const KeyT &key) const {
|
ConstIterator find(const KeyT &key) const {
|
||||||
if (empty()) {
|
return ConstIterator(const_cast<Self*>(this)->find(key));
|
||||||
return end();
|
|
||||||
}
|
|
||||||
auto it = find_bucket_for_insert(key);
|
|
||||||
if (it->empty()) {
|
|
||||||
return end();
|
|
||||||
}
|
|
||||||
return ConstIterator(it, this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
@ -229,17 +215,10 @@ class FlatHashMapImpl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ConstIterator begin() const {
|
ConstIterator begin() const {
|
||||||
if (empty()) {
|
return ConstIterator(const_cast<Self *>(this)->begin());
|
||||||
return end();
|
|
||||||
}
|
|
||||||
auto it = nodes_.begin();
|
|
||||||
while (it->empty()) {
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
return ConstIterator(it, this);
|
|
||||||
}
|
}
|
||||||
ConstIterator end() const {
|
ConstIterator end() const {
|
||||||
return ConstIterator(nodes_.end(), this);
|
return ConstIterator(const_cast<Self *>(this)->end());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class... ArgsT>
|
template <class... ArgsT>
|
||||||
@ -357,14 +336,7 @@ class FlatHashMapImpl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ConstNodeIterator find_bucket_for_insert(const KeyT &key) const {
|
ConstNodeIterator find_bucket_for_insert(const KeyT &key) const {
|
||||||
size_t bucket = calc_bucket(key);
|
return const_cast<Self *>(find_bucket_for_insert(key));
|
||||||
while (!(nodes_[bucket].key() == key) && !nodes_[bucket].empty()) {
|
|
||||||
bucket++;
|
|
||||||
if (bucket == nodes_.size()) {
|
|
||||||
bucket = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nodes_.begin() + bucket;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void resize(size_t size) {
|
void resize(size_t size) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user