muwerk ustd Library
A low-resource, minimal implementation of Arrays, Maps and Queues for low-resource avrs.
Loading...
Searching...
No Matches
ustd_map.h
1// ustd_map.h - ustd queue class
2
3#pragma once
4#include "ustd_array.h"
5
6namespace ustd {
7
8#define MAX_MAP_SIZE UINT_MAX
9
62template <class K, class V> class map {
63 private:
64 unsigned int size;
65 unsigned int peakSize;
66 unsigned int allocSize;
67 unsigned int startSize;
68 unsigned int maxSize;
69 unsigned int incSize;
70 bool shrink;
71 V bad = {};
72
73 public:
74 ustd::array<K> keys;
77 public:
78 map(unsigned int startSize = ARRAY_INIT_SIZE, unsigned int maxSize = ARRAY_MAX_SIZE,
79 unsigned int incSize = ARRAY_INC_SIZE, bool shrink = true)
80 : startSize(startSize), maxSize(maxSize), incSize(incSize), shrink(shrink),
81 keys(array<K>(startSize, maxSize, incSize, shrink)),
82 values(array<V>(startSize, maxSize, incSize, shrink)) {
98 size = 0;
99 allocSize = startSize;
100 }
101
102 /* Implicit, no need:
103 map(const map &mp) {
104 size = mp.size;
105 peakSize = mp.peakSize;
106 allocSize = mp.allocSize;
107 startSize = mp.startSize;
108 maxSize = mp.maxSize;
109 incSize = mp.incSize;
110 shrink = mp.shrink;
111 bad = mp.bad;
112 keys = mp.keys;
113 values = mp.values;
114 } */
115
118 }
119
120 V operator[](K key) const {
125 for (unsigned int i = 0; i < keys.length(); i++) {
126 if (keys[i] == key)
127 return values[i];
128 }
129 return bad;
130 }
131
132 V &operator[](K key) {
137 for (unsigned int i = 0; i < keys.length(); i++) {
138 if (keys[i] == key) {
139 return values[i];
140 }
141 }
142 int i = keys.add(key);
143 if (i == -1) {
144 return bad;
145 }
146 if (i >= 0) {
147 size++;
148 return values[i];
149 }
150 return bad;
151 }
152
153 int find(K key) {
157 for (unsigned int i = 0; i < keys.length(); i++) {
158 if (keys[i] == key)
159 return i;
160 }
161 return -1;
162 }
163
164 int erase(K key) {
169 for (unsigned int i = 0; i < keys.length(); i++) {
170 if (keys[i] == key) {
171 values.erase(i);
172 keys.erase(i);
173 return i;
174 }
175 }
176 return -1;
177 }
178
179 void setInvalidValue(V &entryInvalidValue) {
186 bad = entryInvalidValue;
187 }
188
189 bool isEmpty() {
192 if (size == 0)
193 return true;
194 else
195 return false;
196 }
197
203 return keys;
204 }
205
206 unsigned int length() {
209 return (size);
210 }
211
212 unsigned int peak() {
215 return (peakSize);
216 }
217};
218} // namespace ustd
Lightweight c++11 array implementation.
Definition: ustd_array.h:123
bool erase(unsigned int index)
Definition: ustd_array.h:305
int add(T &entry)
Definition: ustd_array.h:288
unsigned int length() const
Definition: ustd_array.h:378
Lightweight c++11 dictionary map implementation.
Definition: ustd_map.h:62
int find(K key)
Definition: ustd_map.h:153
ustd::array< V > values
Definition: ustd_map.h:75
const ustd::array< K > & keysArray()
Definition: ustd_map.h:198
V operator[](K key) const
Definition: ustd_map.h:120
void setInvalidValue(V &entryInvalidValue)
Definition: ustd_map.h:179
V & operator[](K key)
Definition: ustd_map.h:132
~map()
Definition: ustd_map.h:116
unsigned int length()
Definition: ustd_map.h:206
int erase(K key)
Definition: ustd_map.h:164
unsigned int peak()
Definition: ustd_map.h:212
bool isEmpty()
Definition: ustd_map.h:189
map(unsigned int startSize=ARRAY_INIT_SIZE, unsigned int maxSize=ARRAY_MAX_SIZE, unsigned int incSize=ARRAY_INC_SIZE, bool shrink=true)
Definition: ustd_map.h:78
The ustd namespace.
Definition: ustd_array.h:34