1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
/*
* rtmap.h
*
* Copyright (c) 2000-2004 by Florian Fischer (florianfischer@gmx.de)
* and Martin Trautmann (martintrautmann@gmx.de)
*
* This file may be distributed and/or modified under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation.
*
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
* WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
*/
/** \file
* Contains the class template <tt>Map</tt>, which implements a key-value
* map.
* @see Map
*/
#ifndef __LRT_MAP__
#define __LRT_MAP__
#include "rtiterator.h"
#include "rtlist.h"
#include "rtstring.h"
namespace lrt {
// fwd declare
template<class K, class V> class Map;
/** A key-value pair. */
template<class K, class V> class Pair {
public:
/** Creates a new "standard" key-value pair by calling the
* key's and value's default constructors. */
Pair() : key(), value() {}
/** Creates a key-value pair and copies the given data into it. */
Pair(const K& k, const V& v) : key(k), value(v) {}
/** Returns a constant reference to the key. */
inline const K& getKey() const { return key; }
/** Returns a constant reference to the value. */
inline const V& getValue() const { return value; }
/** Returns a modifiable reference to the key. */
inline K& getKey() { return key; }
/** Returns a modifiable reference to the value. */
inline V& getValue() { return value; }
private:
friend class Map<K,V>;
K key;
V value;
};
/** A map collection type (a.k.a.\ associative array).
* This implementation of a map uses a linked list to store its data and
* it therefore quite slow (for large data sets). <br>
* <b>Note:</b> All types which should be used as keys in a Map must implement
* the comparison operators <tt><</tt> and <tt>==</tt>!
*/
template<class K, class V> class Map {
public:
/** The map iterator type: You can alternatively use
* Iterator<Map<K,V>::Pair>
* or Map<K,V>::Iterator (which is somewhat shorter). */
typedef Iterator<Pair<K,V> > Iterator;
/** Creates a new empty map. The map entries are sorted using the default
* <tt>stdCompare()</tt> function. */
Map();
/** Creates a new empty map, whose entries are sorted with a given comparison
* function. */
Map(int (*compareFun)(const K&, const K&));
/** Creates a new map, copying all the contents over from the given map. */
Map(const Map &);
virtual ~Map() {}
/** Clears this map, then copies all the contents of the given map into it. */
const Map& operator=(const Map &);
/** Returns <tt>True</tt> if there is a mapping for the given key in this map. */
bool isSet(const K&) const;
/** Returns a constant reference to the value the given key is mapped to, or
* <tt>noValue.getValue()</tt> if it is not mapped to anything. */
const V& get(const K&) const;
/** Returns a modifiable reference to the value the given key is mapped to, or
* if it was not mapped to anything, inserts the key into the map, so that
* the reference can be assigned to something. */
V& get(const K&);
/** Put a new mapping of the given key and value into the map. If the given key
* was already mapped to something, the mapping is replaced.
* @return A reference to the inserted key-value pair. */
Pair<K,V>& put(const K&, const V&);
/** Copies all mappings from the given map into this one. Existing mappings are
* replaced (if they have the same key). */
void putAll(const Map<K,V>&);
/** Removes the mapping of the given key from this map. If the key was not mapped
* to anything, just does nothing.
* @return <tt>True</tt> if there was actually a mapping removed. */
bool remove(const K&);
/** Removes the mapping the given iterator points to.
* Moves the iterator to the next mapping of the list. */
void remove(Iterator& iter);
/** Clears this map by removing and deleting all entries. */
void clear();
//Pair& insert(const Pair&);
/** Returns a constant reference to the value the given key is mapped to, or
* <tt>noValue.getValue()</tt> if it is not mapped to anything. */
inline const V& operator[](const K&) const;
/** Returns a modifiable reference to the value the given key is mapped to, or
* if it was not mapped to anything, inserts the key into the map, so that
* the reference can be assigned to something. */
inline V& operator[](const K&);
/** Returns the element count of this map. */
int length() const;
/** Returns an unmodifyable iterator to the first element of the map.
* If the map is empty, the iterator points to nothing; you can use
* Iterator::hasElement() to check this. */
Iterator begin() const;
/** Returns an unmodifyable iterator to the last element of the map.
* If the map is empty, the iterator points to nothing; you can use
* Iterator::hasElement() to check this. */
Iterator end() const;
/** Returns a modifyable iterator to the first element of the map.
* If the map is empty, the iterator points to nothing; you can use
* Iterator::hasElement() to check this. */
Iterator begin();
/** Returns a modifyable iterator to the last element of the map.
* If the map is empty, the iterator points to nothing; you can use
* Iterator::hasElement() to check this. */
Iterator end();
/** The pair which is defined to be no value. */
static Pair<K,V> noValue;
protected:
/** The function used to compare the keys. */
int (*compareFun)(const K&, const K&);
private:
List< Pair<K,V> > data;
};
/** A map using <tt>String</tt>s as keys.
* The only difference to the basic Map template is, that StringMaps can be case
* insensitive.
*/
template<class V> class StringMap : public Map<String,V>
{
public:
/** Creates an empty StringMap which is case sensitive. */
StringMap() : Map<String,V>(&String::compare) {}
/** Creates an empty StringMap, which is optionally case insensitive. */
StringMap(bool caseSensitive) :
Map<String,V>(&String::compare)
{
if(!caseSensitive) compareFun = &String::compareIgnoreCase;
}
/** Creates a new StringMap, copying all the contents over from the given map. */
StringMap(const StringMap& sm) : Map<String,V>(sm) {}
virtual ~StringMap() {}
/** Clears this StringMap, then copies all the contents of the given map into it. */
const StringMap& operator=(const StringMap& sm)
{ Map<String,V>::operator=(sm); return *this; }
};
} // namespace
#include "rtmap.templ.cpp"
#endif
|