FreeNOS
Associative.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 Niek Linnenbank
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef __LIBSTD_ASSOCIATIVE_H
19#define __LIBSTD_ASSOCIATIVE_H
20
21#include "Container.h"
22#include "Comparable.h"
23#include "Types.h"
24#include "Macros.h"
25#include "List.h"
26#include "ListIterator.h"
27
39template <class K, class V> class Associative : public Container, public Comparable<Associative<K,V> >
40{
41 public:
42
53 virtual bool insert(const K & key, const V & item)
54 {
55 return false;
56 }
57
66 virtual bool append(const K & key, const V & item)
67 {
68 return false;
69 }
70
78 virtual int remove(const K & key)
79 {
80 return 0;
81 }
82
86 virtual void clear()
87 {
88 List<K> k = keys();
89
90 for (ListIterator<K> i(k); i.hasNext(); i++)
91 remove(i.current());
92 }
93
99 virtual List<K> keys() const = 0;
100
104 virtual List<K> keys(const V & value) const = 0;
105
111 virtual bool contains(const K & key) const
112 {
113 return values(key).count() > 0;
114 }
115
121 virtual List<V> values() const = 0;
122
128 virtual List<V> values(const K & key) const = 0;
129
137 virtual const V * get(const K & key) const = 0;
138
148 virtual const V & at(const K & key) const = 0;
149
160 virtual const V value(const K & key, const V defaultValue = V()) const = 0;
161
169 virtual int compareTo(const Associative<K,V> &a) const
170 {
171 Size sz = size();
172 Size cnt = count();
173 List<V> vals = a.values();
174
175 // Size must be equal
176 if (a.size() != sz)
177 return a.size() - sz;
178
179 // Count must be equal
180 if (a.count() != cnt)
181 return a.count() - cnt;
182
183 // All elements must be equal
184 for (ListIterator<V> i(values()); i.hasCurrent(); i++)
185 if (!vals.contains(i.current()))
186 return 1;
187
188 return 0;
189 }
190
198 virtual bool equals(const Associative<K,V> &a) const
199 {
200 return compareTo(a) == 0;
201 }
202
213 const V & operator [] (K key) const
214 {
215 return at(key);
216 }
217};
218
224#endif /* __LIBSTD_ASSOCIATIVE_H */
Associatives are containers that provide a mapping of keys to values.
Definition Associative.h:40
virtual bool append(const K &key, const V &item)
Append the given item to the Association.
Definition Associative.h:66
virtual bool insert(const K &key, const V &item)
Inserts the given item to the Assocation.
Definition Associative.h:53
virtual const V value(const K &key, const V defaultValue=V()) const =0
Return the first value for the given key.
virtual const V & at(const K &key) const =0
Returns a reference to the first value for the given key.
virtual List< K > keys(const V &value) const =0
Retrieve list of Keys for the given value.
virtual bool contains(const K &key) const
Check if the given key exists.
virtual List< V > values() const =0
Retrieve all values inside the Association.
virtual void clear()
Removes all items from the Association.
Definition Associative.h:86
const V & operator[](K key) const
Returns the first value for the given key.
virtual const V * get(const K &key) const =0
Returns the first value for the given key.
virtual int remove(const K &key)
Removes all items associated with the given key.
Definition Associative.h:78
virtual List< V > values(const K &key) const =0
Retrieve values for the given key inside the Association.
virtual List< K > keys() const =0
Retrieve all keys inside the Association.
virtual bool equals(const Associative< K, V > &a) const
Test if an Associative is equal to an other Associative.
virtual int compareTo(const Associative< K, V > &a) const
Compare this instance to another instance.
Objects which can be compared to each other.
Definition Comparable.h:35
Containers provide access to stored items.
Definition Container.h:36
virtual Size count() const =0
Returns the number of items inside the Container.
virtual Size size() const =0
Returns the maximum size of this Container.
Iterate through a List.
virtual bool hasCurrent() const
Check if there is a current item on the List.
virtual bool hasNext() const
Check if there is more on the List to iterate.
Simple linked list template class.
Definition List.h:37
virtual bool contains(const T t) const
Check whether an element is on the List.
Definition List.h:219
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128