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 
39 template <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 */
Container.h
Macros.h
Associative::remove
virtual int remove(const K &key)
Removes all items associated with the given key.
Definition: Associative.h:78
Types.h
Container::count
virtual Size count() const =0
Returns the number of items inside the Container.
Container
Containers provide access to stored items.
Definition: Container.h:35
Associative::values
virtual List< V > values() const =0
Retrieve all values inside the Association.
Associative::append
virtual bool append(const K &key, const V &item)
Append the given item to the Association.
Definition: Associative.h:66
Comparable
Objects which can be compared to each other.
Definition: Comparable.h:34
Associative::insert
virtual bool insert(const K &key, const V &item)
Inserts the given item to the Assocation.
Definition: Associative.h:53
Associative::clear
virtual void clear()
Removes all items from the Association.
Definition: Associative.h:86
Associative::compareTo
virtual int compareTo(const Associative< K, V > &a) const
Compare this instance to another instance.
Definition: Associative.h:169
Associative::equals
virtual bool equals(const Associative< K, V > &a) const
Test if an Associative is equal to an other Associative.
Definition: Associative.h:198
Associative::contains
virtual bool contains(const K &key) const
Check if the given key exists.
Definition: Associative.h:111
ListIterator::hasCurrent
virtual bool hasCurrent() const
Check if there is a current item on the List.
Definition: ListIterator.h:104
Associative::keys
virtual List< K > keys() const =0
Retrieve all keys inside the Association.
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
Associative::value
virtual const V value(const K &key, const V defaultValue=V()) const =0
Return the first value for the given key.
ListIterator.h
ListIterator::hasNext
virtual bool hasNext() const
Check if there is more on the List to iterate.
Definition: ListIterator.h:94
Associative::get
virtual const V * get(const K &key) const =0
Returns the first value for the given key.
Associative
Associatives are containers that provide a mapping of keys to values.
Definition: Associative.h:39
Associative::operator[]
const V & operator[](K key) const
Returns the first value for the given key.
Definition: Associative.h:213
Comparable.h
List::contains
virtual bool contains(const T t) const
Check whether an element is on the List.
Definition: List.h:219
List< K >
Associative::at
virtual const V & at(const K &key) const =0
Returns a reference to the first value for the given key.
Container::size
virtual Size size() const =0
Returns the maximum size of this Container.
List.h
ListIterator< K >