ionContainer.h
Go to the documentation of this file.
1 
9 #pragma once
10 
11 #include "ionTypes.h"
12 #include "ionSmartPointer.h"
13 
14 #include <algorithm>
15 #include <functional>
16 
17 
18 namespace ion
19 {
20 
22  template <typename T, typename U>
23  U * ConditionalMapAccess(map<T, U *> const & Map, T const Key)
24  {
25  auto Iterator = Map.find(Key);
26 
27  if (Iterator != Map.end())
28  return Iterator->second;
29 
30  return nullptr;
31  }
32 
34  template <typename T, typename U>
35  SharedPointer<U> ConditionalMapAccess(map<T, SharedPointer<U>> const & Map, T const Key)
36  {
37  auto Iterator = Map.find(Key);
38 
39  if (Iterator != Map.end())
40  return Iterator->second;
41 
42  return nullptr;
43  }
44 
46  template <typename T, typename U>
47  U * ConditionalMapAccess(unordered_map<T, U *> const & Map, T const Key)
48  {
49  auto Iterator = Map.find(Key);
50 
51  if (Iterator != Map.end())
52  return Iterator->second;
53 
54  return 0;
55  }
56 
58  template <typename T, typename U>
59  bool CheckMapAccess(map<T, U> const & Map, T const Key)
60  {
61  return Map.find(Key) != Map.end();
62  }
63 
65  template <typename T, typename U>
66  bool TryMapAccess(map<T, U> const & Map, T const Key, U & Value)
67  {
68  auto Iterator = Map.find(Key);
69 
70  if (Iterator != Map.end())
71  {
72  Value = Iterator->second;
73  return true;
74  }
75 
76  return false;
77  }
78 
80  template <typename T, typename U>
81  set<T> KeySet(map<T, U> const & Map)
82  {
83  set<T> Return;
84 
85  for (auto it : Map)
86  Return.insert(it.first);
87 
88  return Return;
89  }
90 
92  template <typename T>
93  void AddAtEnd(vector<T> & A, vector<T> const & B)
94  {
95  A.insert(A.end(), B.begin(), B.end());
96  }
97 
99  template <typename T>
100  void EraseRemove(vector<T> & A, T B)
101  {
102  A.erase(std::remove(std::begin(A), std::end(A), B), std::end(A));
103  }
104 
106  template <typename T, typename U>
107  void EraseRemove(vector<T> & A, U B)
108  {
109  A.erase(std::remove(std::begin(A), std::end(A), B), std::end(A));
110  }
111 
113  template <typename T>
114  void DeleteAndClear(vector<T> & A)
115  {
116  std::for_each(A.begin(), A.end(), [](T ptr) { delete ptr; });
117  A.clear();
118  }
119 
120 }
set< T > KeySet(map< T, U > const &Map)
Definition: ionContainer.h:81
Brings some standard library pointer classes into ion namespace.
Definition: CCatmullRomAdvancedSplineInterpolator.h:7
void AddAtEnd(vector< T > &A, vector< T > const &B)
Definition: ionContainer.h:93
void DeleteAndClear(vector< T > &A)
Deletes every pointer element of a vector and then clears the vector.
Definition: ionContainer.h:114
void EraseRemove(vector< T > &A, T B)
Definition: ionContainer.h:100
Defines typedefs for integer and floating point types.
bool CheckMapAccess(map< T, U > const &Map, T const Key)
Definition: ionContainer.h:59
bool TryMapAccess(map< T, U > const &Map, T const Key, U &Value)
Definition: ionContainer.h:66
std::shared_ptr< T > SharedPointer
Definition: ionSmartPointer.h:25
U * ConditionalMapAccess(map< T, U * > const &Map, T const Key)
Definition: ionContainer.h:23