14#ifndef WPIUTIL_WPI_STRINGMAP_H
15#define WPIUTIL_WPI_STRINGMAP_H
25#include <initializer_list>
30template <
typename ValueTy>
class StringMapConstIterator;
31template <
typename ValueTy>
class StringMapIterator;
32template <
typename ValueTy>
class StringMapKeyIterator;
53 RHS.TheTable =
nullptr;
56 RHS.NumTombstones = 0;
113template <
typename ValueTy,
typename AllocatorTy = MallocAllocator>
134 StringMap(std::initializer_list<std::pair<std::string_view, ValueTy>> List)
151 unsigned *HashTable = (
unsigned *)(TheTable + NumBuckets + 1),
152 *RHSHashTable = (
unsigned *)(RHS.
TheTable + NumBuckets + 1);
156 for (
unsigned I = 0, E = NumBuckets; I != E; ++I) {
158 if (!Bucket || Bucket == getTombstoneVal()) {
159 TheTable[I] = Bucket;
163 TheTable[I] = MapEntryTy::create(
164 static_cast<MapEntryTy *
>(Bucket)->getKey(), getAllocator(),
165 static_cast<MapEntryTy *
>(Bucket)->getValue());
166 HashTable[I] = RHSHashTable[I];
188 for (
unsigned I = 0, E = NumBuckets; I != E; ++I) {
190 if (Bucket && Bucket != getTombstoneVal()) {
191 static_cast<MapEntryTy *
>(Bucket)->Destroy(getAllocator());
198 using AllocTy::getAllocator;
223 int Bucket = FindKey(Key);
226 return iterator(TheTable + Bucket,
true);
230 int Bucket = FindKey(Key);
248 auto Iter = this->
find(std::move(Val));
249 assert(Iter != this->end() &&
"StringMap::at failed due to a missing key");
263 template <
typename InputTy>
270 if (size() != RHS.
size())
273 for (
const auto &KeyValue : *
this) {
274 auto FindInRHS = RHS.
find(KeyValue.getKey());
276 if (FindInRHS == RHS.
end())
279 if (!(KeyValue.getValue() == FindInRHS->getValue()))
292 unsigned BucketNo = LookupBucketFor(KeyValue->
getKey());
294 if (Bucket && Bucket != getTombstoneVal())
297 if (Bucket == getTombstoneVal())
301 assert(NumItems + NumTombstones <= NumBuckets);
311 std::pair<iterator, bool>
insert(std::pair<std::string_view, ValueTy> KV) {
312 return try_emplace(KV.first, std::move(KV.second));
318 template <
typename InputIt>
void insert(InputIt First, InputIt Last) {
319 for (InputIt It = First; It != Last; ++It)
326 void insert(std::initializer_list<std::pair<std::string_view, ValueTy>> List) {
327 insert(List.begin(), List.end());
332 template <
typename V>
334 auto Ret = try_emplace(Key, std::forward<V>(Val));
336 Ret.first->second = std::forward<V>(Val);
344 template <
typename... ArgsTy>
346 unsigned BucketNo = LookupBucketFor(Key);
348 if (Bucket && Bucket != getTombstoneVal())
349 return std::make_pair(
iterator(TheTable + BucketNo,
false),
352 if (Bucket == getTombstoneVal())
355 MapEntryTy::create(Key, getAllocator(), std::forward<ArgsTy>(Args)...);
357 assert(NumItems + NumTombstones <= NumBuckets);
359 BucketNo = RehashTable(BucketNo);
360 return std::make_pair(
iterator(TheTable + BucketNo,
false),
true);
370 for (
unsigned I = 0, E = NumBuckets; I != E; ++I) {
372 if (Bucket && Bucket != getTombstoneVal()) {
373 static_cast<MapEntryTy *
>(Bucket)->Destroy(getAllocator());
401template <
typename DerivedTy,
typename ValueTy>
412 bool NoAdvance =
false)
415 AdvancePastEmptyBuckets();
420 return static_cast<DerivedTy &
>(*this);
423 friend bool operator==(
const DerivedTy &LHS,
const DerivedTy &RHS) {
424 return LHS.Ptr == RHS.Ptr;
429 AdvancePastEmptyBuckets();
430 return static_cast<DerivedTy &
>(*this);
441 ReversePastEmptyBuckets();
442 return static_cast<DerivedTy &
>(*this);
452 void AdvancePastEmptyBuckets() {
456 void ReversePastEmptyBuckets() {
462template <
typename ValueTy>
465 const StringMapEntry<ValueTy>> {
472 bool NoAdvance =
false)
473 :
base(Bucket, NoAdvance) {}
480template <
typename ValueTy>
482 StringMapEntry<ValueTy>> {
489 bool NoAdvance =
false)
490 :
base(Bucket, NoAdvance) {}
501template <
typename ValueTy>
504 StringMapConstIterator<ValueTy>,
505 std::forward_iterator_tag, std::string_view> {
518template <
typename ValueTy>
521 if (&lhs == &rhs)
return true;
524 if (lhs.
size() != rhs.
size())
return false;
529 for (
auto i = lhs.
begin(), end = lhs.
end(); i != end; ++i)
531 std::sort(lhs_items.
begin(), lhs_items.
end(),
534 return a->getKey() < b->getKey();
539 for (
auto i = rhs.
begin(), end = rhs.
end(); i != end; ++i)
541 std::sort(rhs_items.
begin(), rhs_items.
end(),
544 return a->getKey() < b->getKey();
548 for (
auto a = lhs_items.
begin(),
b = rhs_items.
begin(),
549 aend = lhs_items.
end(), bend = rhs_items.
end();
550 a != aend &&
b != bend; ++a, ++
b) {
551 if ((*a)->first() != (*b)->first() || (*a)->second != (*b)->second)
557template <
typename ValueTy>
560 return !(lhs == rhs);
563template <
typename ValueTy>
566 if (&lhs == &rhs)
return false;
571 for (
auto i = lhs.
begin(), end = lhs.
end(); i != end; ++i)
573 std::sort(lhs_keys.
begin(), lhs_keys.
end());
577 for (
auto i = rhs.
begin(), end = rhs.
end(); i != end; ++i)
579 std::sort(rhs_keys.
begin(), rhs_keys.
end());
582 return lhs_keys < rhs_keys;
585template <
typename ValueTy>
591template <
typename ValueTy>
594 return !(lhs <= rhs);
597template <
typename ValueTy>
This file defines MallocAllocator.
#define LLVM_ALLOCATORHOLDER_EMPTYBASE
Definition: AllocatorBase.h:25
This file defines counterparts of C library allocation functions defined in the namespace 'std'.
This file defines the SmallVector class.
This file defines the StringMapEntry class - it is intended to be a low dependency implementation det...
and restrictions which apply to each piece of software is included later in this file and or inside of the individual applicable source files The disclaimer of warranty in the WPILib license above applies to all code in and nothing in any of the other licenses gives permission to use the names of FIRST nor the names of the WPILib contributors to endorse or promote products derived from this software The following pieces of software have additional or alternate and or Google Inc All rights reserved Redistribution and use in source and binary with or without are permitted provided that the following conditions are this list of conditions and the following disclaimer *Redistributions in binary form must reproduce the above copyright this list of conditions and the following disclaimer in the documentation and or other materials provided with the distribution *Neither the name of Google Inc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED BUT NOT LIMITED THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY OR CONSEQUENTIAL WHETHER IN STRICT OR EVEN IF ADVISED OF THE POSSIBILITY OF SUCH January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source documentation and configuration files Object form shall mean any form resulting from mechanical transformation or translation of a Source including but not limited to compiled object generated and conversions to other media types Work shall mean the work of whether in Source or Object made available under the as indicated by a copyright notice that is included in or attached to the whether in Source or Object that is based or other modifications as a an original work of authorship For the purposes of this Derivative Works shall not include works that remain separable or merely the Work and Derivative Works thereof Contribution shall mean any work of including the original version of the Work and any modifications or additions to that Work or Derivative Works that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner For the purposes of this submitted means any form of or written communication sent to the Licensor or its including but not limited to communication on electronic mailing source code control and issue tracking systems that are managed or on behalf the Licensor for the purpose of discussing and improving the but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as Not a Contribution Contributor shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work Grant of Copyright License Subject to the terms and conditions of this each Contributor hereby grants to You a non no royalty free
Definition: ThirdPartyNotices.txt:151
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1202
void reserve(size_type N)
Definition: SmallVector.h:669
void push_back(const T &Elt)
Definition: SmallVector.h:418
iterator begin()
Definition: SmallVector.h:272
iterator end()
Definition: SmallVector.h:274
Definition: StringMap.h:465
StringMapConstIterator()=default
StringMapConstIterator(StringMapEntryBase **Bucket, bool NoAdvance=false)
Definition: StringMap.h:471
const StringMapEntry< ValueTy > & operator*() const
Definition: StringMap.h:475
StringMapEntryBase - Shared base class of StringMapEntry instances.
Definition: StringMapEntry.h:29
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
Definition: StringMapEntry.h:106
std::string_view getKey() const
Definition: StringMapEntry.h:112
void Destroy(AllocatorTy &allocator)
Destroy - Destroy this StringMapEntry, releasing memory back to the specified allocator.
Definition: StringMapEntry.h:146
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:116
StringMap & operator=(StringMap RHS)
Definition: StringMap.h:177
void insert(InputIt First, InputIt Last)
Inserts elements from range [first, last).
Definition: StringMap.h:318
const_iterator find(std::string_view Key) const
Definition: StringMap.h:229
const_iterator end() const
Definition: StringMap.h:213
std::pair< iterator, bool > insert(std::pair< std::string_view, ValueTy > KV)
insert - Inserts the specified key/value pair into the map if the key isn't already in the map.
Definition: StringMap.h:311
iterator find(std::string_view Key)
Definition: StringMap.h:222
void insert(std::initializer_list< std::pair< std::string_view, ValueTy > > List)
Inserts elements from initializer list ilist.
Definition: StringMap.h:326
size_t size_type
Definition: StringMap.h:203
~StringMap()
Definition: StringMap.h:183
StringMap(StringMap &&RHS)
Definition: StringMap.h:139
iterator begin()
Definition: StringMap.h:208
std::pair< iterator, bool > try_emplace(std::string_view Key, ArgsTy &&...Args)
Emplace a new element for the specified key into the map if the key isn't already in the map.
Definition: StringMap.h:345
ValueTy & operator[](std::string_view Key)
Lookup the ValueTy for the Key, or create a default constructed value if the key is not in the map.
Definition: StringMap.h:255
StringMap(AllocatorTy A)
Definition: StringMap.h:127
StringMap(unsigned InitialSize, AllocatorTy A)
Definition: StringMap.h:130
StringMap()
Definition: StringMap.h:122
StringMap(const StringMap &RHS)
Definition: StringMap.h:142
size_type count(std::string_view Key) const
count - Return 1 if the element is in the map, 0 otherwise.
Definition: StringMap.h:261
const_iterator begin() const
Definition: StringMap.h:210
void erase(iterator I)
Definition: StringMap.h:386
void clear()
Definition: StringMap.h:364
void remove(MapEntryTy *KeyValue)
remove - Remove the specified key/value pair from the map, but do not erase it.
Definition: StringMap.h:384
StringMap(std::initializer_list< std::pair< std::string_view, ValueTy > > List)
Definition: StringMap.h:134
std::pair< iterator, bool > insert_or_assign(std::string_view Key, V &&Val)
Inserts an element or assigns to the current element if the key already exists.
Definition: StringMap.h:333
const char * key_type
Definition: StringMap.h:200
ValueTy lookup(std::string_view Key) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: StringMap.h:238
iterator end()
Definition: StringMap.h:209
StringMap(unsigned InitialSize)
Definition: StringMap.h:124
iterator_range< StringMapKeyIterator< ValueTy > > keys() const
Definition: StringMap.h:217
bool operator==(const StringMap &RHS) const
equal - check whether both of the containers are equal.
Definition: StringMap.h:269
bool contains(std::string_view Key) const
contains - Return true if the element is in the map, false otherwise.
Definition: StringMap.h:258
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:291
bool erase(std::string_view Key)
Definition: StringMap.h:392
size_type count(const StringMapEntry< InputTy > &MapEntry) const
Definition: StringMap.h:264
ValueTy mapped_type
Definition: StringMap.h:201
Alloc & getAllocator()
Definition: AllocatorBase.h:115
bool operator!=(const StringMap &RHS) const
Definition: StringMap.h:286
const ValueTy & at(std::string_view Val) const
at - Return the entry for the specified key, or abort if no such entry exists.
Definition: StringMap.h:247
StringMapImpl - This is the base class of StringMap that is shared among all of its instantiations.
Definition: StringMap.h:36
StringMapImpl(unsigned InitSize, unsigned ItemSize)
unsigned getNumItems() const
Definition: StringMap.h:96
StringMapImpl(unsigned itemSize)
Definition: StringMap.h:48
StringMapEntryBase ** TheTable
Definition: StringMap.h:41
StringMapImpl(StringMapImpl &&RHS) noexcept
Definition: StringMap.h:49
unsigned NumBuckets
Definition: StringMap.h:42
unsigned NumTombstones
Definition: StringMap.h:44
int FindKey(std::string_view Key) const
FindKey - Look up the bucket that contains the specified key.
void init(unsigned Size)
Allocate the table with the specified number of buckets and otherwise setup the map as empty.
unsigned LookupBucketFor(std::string_view Key)
LookupBucketFor - Look up the bucket that the specified string should end up in.
unsigned RehashTable(unsigned BucketNo=0)
unsigned ItemSize
Definition: StringMap.h:45
unsigned NumItems
Definition: StringMap.h:43
bool empty() const
Definition: StringMap.h:98
void swap(StringMapImpl &Other)
Definition: StringMap.h:101
StringMapEntryBase * RemoveKey(std::string_view Key)
RemoveKey - Remove the StringMapEntry for the specified key from the table, returning it.
unsigned size() const
Definition: StringMap.h:99
static StringMapEntryBase * getTombstoneVal()
Definition: StringMap.h:91
void RemoveKey(StringMapEntryBase *V)
RemoveKey - Remove the specified StringMapEntry from the table, but do not delete it.
unsigned getNumBuckets() const
Definition: StringMap.h:95
static constexpr uintptr_t TombstoneIntVal
Definition: StringMap.h:87
Definition: StringMap.h:404
DerivedTy operator--(int)
Definition: StringMap.h:445
StringMapIterBase(StringMapEntryBase **Bucket, bool NoAdvance=false)
Definition: StringMap.h:411
DerivedTy & operator=(const DerivedTy &Other)
Definition: StringMap.h:418
DerivedTy operator++(int)
Definition: StringMap.h:433
DerivedTy & operator++()
Definition: StringMap.h:427
StringMapIterBase()=default
friend bool operator==(const DerivedTy &LHS, const DerivedTy &RHS)
Definition: StringMap.h:423
StringMapEntryBase ** Ptr
Definition: StringMap.h:406
DerivedTy & operator--()
Definition: StringMap.h:439
Definition: StringMap.h:482
StringMapIterator(StringMapEntryBase **Bucket, bool NoAdvance=false)
Definition: StringMap.h:488
StringMapIterator()=default
StringMapEntry< ValueTy > & operator*() const
Definition: StringMap.h:492
Definition: StringMap.h:505
std::string_view operator*() const
Definition: StringMap.h:515
StringMapKeyIterator()=default
StringMapKeyIterator(StringMapConstIterator< ValueTy > Iter)
Definition: StringMap.h:512
Definition: AllocatorBase.h:110
CRTP base class for adapting an iterator to a different type.
Definition: iterator.h:237
const StringMapConstIterator< ValueTy > & wrapped() const
Definition: iterator.h:250
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition: iterator.h:80
A range adaptor for a pair of iterators.
Definition: iterator_range.h:42
basic_string_view< char > string_view
Definition: core.h:501
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool
Definition: core.h:2120
constexpr auto count() -> size_t
Definition: core.h:1203
uint128_t uintptr_t
Definition: format.h:484
WPI_BASIC_JSON_TPL_DECLARATION void swap(wpi::WPI_BASIC_JSON_TPL &j1, wpi::WPI_BASIC_JSON_TPL &j2) noexcept(//NOLINT(readability-inconsistent-declaration-parameter-name) is_nothrow_move_constructible< wpi::WPI_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression) is_nothrow_move_assignable< wpi::WPI_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Definition: json.h:5219
Definition: ntcore_cpp.h:26
constexpr bool operator>(const unexpected< E > &lhs, const unexpected< E > &rhs)
Definition: expected:192
constexpr bool contains(std::string_view str, std::string_view other) noexcept
Checks if str contains the substring other.
Definition: StringExtras.h:320
constexpr bool operator<(const unexpected< E > &lhs, const unexpected< E > &rhs)
Definition: expected:184
constexpr bool operator<=(const unexpected< E > &lhs, const unexpected< E > &rhs)
Definition: expected:188
constexpr bool operator>=(const unexpected< E > &lhs, const unexpected< E > &rhs)
Definition: expected:196
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Definition: iterator_range.h:75
constexpr bool operator==(const unexpected< E > &lhs, const unexpected< E > &rhs)
Definition: expected:176
constexpr bool operator!=(const unexpected< E > &lhs, const unexpected< E > &rhs)
Definition: expected:180
A traits type that is used to handle pointer types and things that are just wrappers for pointers as ...
Definition: PointerLikeTypeTraits.h:25