 |
Gorgon Game Engine
|
Go to the documentation of this file.
5 #pragma warning(error: 4239)
14 #include "../Utils/Assert.h"
20 namespace Containers {
34 template<
class K_,
class T_, K_ (*KeyFn)(
const T_&) = (K_(*)(
const T_&))
nullptr,
template <
class ...>
class M_=std::map,
class C_=std::less<K_>>
36 using MapType=M_<K_, T_*, C_, std::allocator<std::pair<const K_, T_*>>>;
41 template <
class I_,
class H_>
45 std::pair<const typename H_::KeyType, typename H_::ValueType &>
47 typedef std::pair<const typename H_::KeyType, typename H_::ValueType &>
Type;
64 if(container==
nullptr) {
65 throw std::runtime_error(
"Iterator is not valid.");
68 currentit=container->mapping.erase(current());
76 if(container==
nullptr) {
77 throw std::runtime_error(
"Iterator is not valid.");
80 auto item=currentit->second;
82 currentit=container->mapping.erase(currentit);
87 void SetItem(
typename H_::ValueType &newitem,
bool deleteprev=
false) {
89 delete currentit->second;
91 currentit->second=&newitem;
95 Iterator_(H_ &container,
const I_ iterator) : currentit(iterator), container(&container) {
101 Type current()
const {
103 throw std::out_of_range(
"Iterator is not valid.");
105 return {currentit->first, *(currentit->second)};
108 bool isvalid()
const {
109 if(container==
nullptr)
return false;
111 return currentit!=container->mapping.end();
114 bool moveby(
long amount) {
115 if(container==
nullptr)
return false;
118 if(amount==0)
return isvalid();
121 for(
int i=0;i<amount;i++)
125 for(
int i=amount;i<0;i++)
132 bool compare(
const Iterator_ &it)
const {
133 return it.currentit==currentit;
137 currentit=it.currentit;
138 container=it.container;
141 long distance(
const Iterator_ &it)
const {
142 return it.currentit-currentit;
145 bool isbefore(
const Iterator_ &it)
const {
146 return currentit<it.currentit;
161 H_ *container =
nullptr;
164 template<
class I_,
class H_>
180 this->currentit=it.currentit;
181 this->container=it.container;
186 Iterator_<typename MapType::const_iterator, const
Hashmap>(h, it) {
191 void SetKey(
const K_ &newkey) {}
202 Hashmap(std::initializer_list<std::pair<const K_, T_*>> list) : mapping(list) {
204 for(
auto &p : list) {
205 assert(p.second &&
"Element is nullptr");
211 Hashmap(std::initializer_list<std::pair<const K_, T_&>> list) {
212 for(
auto &p : list) {
213 mapping.insert(std::make_pair(p.first, &p.second));
220 assert(KeyFn &&
"Key retrieval function should be set.");
222 for(
auto &p : list) {
224 mapping.insert(std::make_pair(KeyFn(*p), p));
248 swap(mapping, other.mapping);
264 void Add(
const K_ &key, T_ &obj,
bool deleteprev =
false) {
265 auto it = mapping.find(key);
266 if( it != mapping.end() ) {
273 mapping.insert(std::make_pair(key, &obj));
280 void Add(
const K_ &key, T_ *obj,
bool deleteprev =
false) {
282 auto it = mapping.find(key);
283 if( it != mapping.end() ) {
290 mapping.insert(std::make_pair(key, obj));
294 auto it = mapping.find(key);
295 if( it != mapping.end() ) {
306 void Add(T_ &obj,
bool deleteprev=
false) {
307 assert(KeyFn!=
nullptr &&
"Key retrieval function should be set.");
309 Add(KeyFn(obj), obj, deleteprev);
315 void Add(T_ *obj,
bool deleteprev=
false) {
316 assert(KeyFn!=
nullptr &&
"Key retrieval function should be set.");
319 Add(KeyFn(*obj), obj, deleteprev);
321 Add({}, obj, deleteprev);
333 auto it = mapping.find(key);
334 if(it!=mapping.end()) {
349 decltype(mapping) newmap;
352 swap(mapping, newmap);
358 for(
auto &p : mapping) {
367 for(
auto &p : mapping) {
376 return (
long)mapping.size();
381 return (
long)mapping.size();
386 auto it = mapping.find(key);
388 if(it == mapping.end()) {
392 return *(it->second);
397 return mapping.count(key)!=0;
403 return Iterator(*
this, mapping.find(key));
408 ConstIterator
Find(
const K_ &key)
const {
409 return ConstIterator(*
this, mapping.find(key));
416 return Iterator(*
this, mapping.begin());
421 return Iterator(*
this, mapping.end());
426 return Iterator(*
this, mapping.begin());
431 auto it = mapping.end();
440 return ConstIterator(*
this, mapping.begin());
444 ConstIterator
end()
const {
445 return ConstIterator(*
this, mapping.end());
450 return ConstIterator(*
this, mapping.begin());
455 auto it = mapping.end();
459 return ConstIterator(*
this, it);
468 typename std::enable_if<TMP::IsStreamable<K__>::Value,
void>::type properthrow(
const K__ &key)
const {
469 std::stringstream ss;
470 ss<<
"Item not found: ";
473 ASSERT(
false, ss.str(), 0, 8);
475 throw std::runtime_error(ss.str());
479 typename std::enable_if<!TMP::IsStreamable<K__>::Value,
void>::type properthrow(
const K__ &key)
const {
481 ASSERT(
false,
"Item not found", 0, 8);
483 throw std::runtime_error(
"Item not found");
489 template<
class K_,
class T_, K_ (KeyFn)(
const T_&)=
nullptr, template <class ...>
class M_=std::map,
class C_=std::less<K_>>
void DeleteAll()
Deletes and removes all the elements of this map.
Definition: Hashmap.h:357
void Remove(const I_ &first, const I_ &end)
This function works with collection iterators.
Definition: Iterator.h:386
Hashmap(Hashmap &&other)
Move constructor.
Definition: Hashmap.h:233
void RemoveAll()
Removes all elements from this mapping without deleting them.
Definition: Hashmap.h:342
ConstIterator(const Iterator &it)
Regular iterators can be converted to const iterators.
Definition: Hashmap.h:179
void swap(Collection< T_ > &l, Collection< T_ > &r)
Swaps two collections.
Definition: Collection.h:707
This class is a reference based hashmap.
Definition: Hashmap.h:35
long GetCount() const
Returns the number of elements in the map.
Definition: Hashmap.h:375
void Add(T_ *obj, bool deleteprev=false)
Adds the given item by retrieving the related key.
Definition: Hashmap.h:315
Iterator Last()
returns the iterator to the last item
Definition: Hashmap.h:430
K_ KeyType
Definition: Hashmap.h:169
ConstIterator begin() const
begin iterator
Definition: Hashmap.h:439
void Add(const K_ &key, T_ &obj, bool deleteprev=false)
Adds the given item with the related key.
Definition: Hashmap.h:264
This file contains template metaprogramming methods and classes used throughout Gorgon Library.
Generic iterator interface.
Definition: Iterator.h:219
void Delete(const K_ &key)
Removes the item with the given key from the mapping and deletes it.
Definition: Hashmap.h:332
void Destroy()
Deletes and removes all the elements of this map, in addition to destroying data used.
Definition: Hashmap.h:366
void Swap(Hashmap &other)
Swaps two hashmaps.
Definition: Hashmap.h:245
ConstIterator First() const
returns the iterator to the first item
Definition: Hashmap.h:449
Iterator_< typename std::map< K_, T_ * >::iterator, Hashmap > Iterator
Regular iterator.
Definition: Hashmap.h:172
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
void Add(T_ &obj, bool deleteprev=false)
Adds the given item by retrieving the related key.
Definition: Hashmap.h:306
Iterator end()
end iterator
Definition: Hashmap.h:420
#define ASSERT(expression, message,...)
Replaces regular assert to allow messages and backtrace.
Definition: Assert.h:161
void Add(const K_ &key, T_ *obj, bool deleteprev=false)
Adds the given item with the related key.
Definition: Hashmap.h:280
contains filesystem Iterator. Lists file and directories.
long GetSize() const
Returns the number of elements in the map.
Definition: Hashmap.h:380
void Remove(const K_ &key)
Removes the item with the given key from the mapping.
Definition: Hashmap.h:326
Iterator begin()
Definition: Hashmap.h:415
Hashmap Duplicate() const
Definition: Hashmap.h:237
Type
Definition: Shader.h:14
Hashmap(const Hashmap &)=delete
Copy constructor is disabled.
ConstIterator Last() const
returns the iterator to the last item
Definition: Hashmap.h:454
Hashmap(std::initializer_list< T_ * > list)
Filling constructor that takes the keys using KeyFn function.
Definition: Hashmap.h:219
Hashmap(std::initializer_list< std::pair< const K_, T_ & >> list)
Filling constructor. This constructor uses initializer list of std::pair<K_, T_&>
Definition: Hashmap.h:211
Const iterator allows iteration of const collections.
Definition: Hashmap.h:175
ConstIterator end() const
end iterator
Definition: Hashmap.h:444
ConstIterator Find(const K_ &key) const
Finds the given key in the hashmap and returns iterator for it.
Definition: Hashmap.h:408
Iterator First()
returns the iterator to the first item
Definition: Hashmap.h:425
T_ ValueType
Definition: Hashmap.h:168
T_ & operator[](const K_ &key) const
If not found throws.
Definition: Hashmap.h:385
ValueType
Possible value types.
Definition: Instruction.h:61
Hashmap & operator=(const Hashmap &other)=delete
Copy constructor is disabled.
void Collapse()
Clears the contents of the map and releases the memory used for the list.
Definition: Hashmap.h:348
Iterator Find(const K_ &key)
Finds the given key in the hashmap and returns iterator for it.
Definition: Hashmap.h:402
Hashmap()
Default constructor.
Definition: Hashmap.h:196
Hashmap(std::initializer_list< std::pair< const K_, T_ * >> list)
Filling constructor.
Definition: Hashmap.h:202
bool Exists(const K_ &key) const
Checks if an element with the given key exists.
Definition: Hashmap.h:396
friend class Iterator_
Definition: Hashmap.h:165
void swap(Hashmap< K_, T_, KeyFn, M_, C_ > &left, Hashmap< K_, T_, KeyFn, M_, C_ > &right)
Definition: Hashmap.h:490