Edinburgh Speech Tools  2.1-release
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EST_TKVL.h
1 /*************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 1995,1996 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*************************************************************************/
33 /* Author : Paul Taylor */
34 /* Date : January 1995 */
35 /*-----------------------------------------------------------------------*/
36 /* Key/Value list template class */
37 /* */
38 /*=======================================================================*/
39 #ifndef __EST_TKVL_H__
40 #define __EST_TKVL_H__
41 
42 #include <cmath>
43 
44 using namespace std;
45 
46 #include "EST_TList.h"
47 #include "instantiate/EST_TKVLI.h"
48 #include "EST_TIterator.h"
49 
50 class EST_String;
51 
52 
53 /** \class EST_TKVI
54  * @ingroup containerclasses
55  * \brief Templated Key-Value Item. Serves as the items in the list of the
56 EST_TKVL class. */
57 template<class K, class V> class EST_TKVI {
58  public:
59  K k;
60  V v;
61 
62  inline bool operator==(const EST_TKVI<K,V> &i){
63  return( (i.k == k) && (i.v == v) );
64  }
65 
66  friend ostream& operator << (ostream& s, EST_TKVI<K,V> const &i)
67  { return s << i.k << "\t" << i.v << "\n"; }
68 };
69 
70 
71 /** \class EST_TKVL
72  * @ingroup containerclasses
73  * \brief Templated Key-Value list. Objects of type EST_TKVL contain lists which
74 are accessed by a key of type `K`, which returns a value of type `V`. */
75 template<class K, class V> class EST_TKVL {
76  private:
77  EST_Litem *find_pair_key(const K &key) const;
78  EST_Litem *find_pair_val(const V &val) const;
79  public:
80  /**@name Constructor functions */
81  ///@{
82  /// default constructor
83  EST_TKVL() {;}
84  /// copy constructor
85  EST_TKVL(const EST_TKVL<K, V> &kv);
86  ///@}
87 
88  /// default value, returned when there is no such entry.
89  static V *default_val;
90 
91  /// default value, returned when there is no such entry.
92  static K *default_key;
93 
94  /// Linked list of key-val pairs. Don't use
95  /// this as it will be made private in the future
97 
98  /// number of key value pairs in list
99  const int length() const {return list.length();}
100 
101  /// Return First key value pair in list
102  EST_Litem * head() const {return list.head();};
103 
104  /// Empty list.
105  void clear();
106 
107  /**@name Access functions.
108  */
109  ///@{
110  /// return value according to key (const)
111  const V &val(const K &rkey, bool m=0) const;
112  /// return value according to key (non-const)
113  V &val(const K &rkey, bool m=0);
114  /// return value according to ptr
115  const V &val(EST_Litem *ptr, bool m=0) const;
116  /// return value according to ptr
117  V &val(EST_Litem *ptr, bool m=0);
118  /// value or default
119  const V &val_def(const K &rkey, const V &def) const;
120 
121  /// find key, reference by ptr
122  const K &key(EST_Litem *ptr, int m=1) const;
123  /// find key, reference by ptr
124  K &key(EST_Litem *ptr, int m=1);
125 
126  /// return first matching key, referenced by val
127  const K &key(const V &v, int m=1) const;
128 
129  /** change key-val pair. If no corresponding entry is present, add
130  to end of list.
131  */
132  int change_val(const K &rkey,const V &rval);
133  /** change key-val pair. If no corresponding entry is present, add
134  to end of list.*/
135  int change_val(EST_Litem *ptr,const V &rval); // change key-val pair.
136  /// change name of key pair.
137  int change_key(EST_Litem *ptr,const K &rkey);
138 
139  /// add key-val pair to list
140  int add_item(const K &rkey,const V &rval, int no_search = 0);
141 
142  /// remove key and val pair from list
143  int remove_item(const K &rkey, int quiet = 0);
144 
145  ///@}
146 
147  /// Returns true if key is present.
148  const int present(const K &rkey) const;
149 
150  /// apply function to each pair
151  void map(void (*func)(K&, V&));
152 
153  friend ostream& operator << (ostream& s, EST_TKVL<K,V> const &l)
154  {EST_Litem *p;
155  for (p = l.list.head(); p ; p = p->next())
156  s << l.list(p).k << "\t" << l.list(p).v << endl;
157  return s;
158  }
159 
160  /// full copy of KV list.
161  EST_TKVL<K, V> & operator = (const EST_TKVL<K,V> &kv);
162  /// add kv after existing list.
163  EST_TKVL<K, V> & operator += (const EST_TKVL<K,V> &kv);
164  /// make new concatenated list
165  EST_TKVL<K, V> operator + (const EST_TKVL<K,V> &kv);
166 
167  // Iteration support
168 
169 protected:
170  struct IPointer { EST_Litem *p; };
171 
172  void point_to_first(IPointer &ip) const { ip.p = list.head(); }
173  void move_pointer_forwards(IPointer &ip) const { ip.p = ip.p->next(); }
174  bool points_to_something(const IPointer &ip) const { return ip.p != NULL; }
175  EST_TKVI<K, V> &points_at(const IPointer &ip) { return list(ip.p); }
176 
177  friend class EST_TIterator< EST_TKVL<K, V>, IPointer, EST_TKVI<K, V> >;
178  friend class EST_TStructIterator< EST_TKVL<K, V>, IPointer, EST_TKVI<K, V> >;
179  friend class EST_TRwIterator< EST_TKVL<K, V>, IPointer, EST_TKVI<K, V> >;
180  friend class EST_TRwStructIterator< EST_TKVL<K, V>, IPointer, EST_TKVI<K, V> >;
181 
182 public:
183  typedef EST_TKVI<K, V> Entry;
184  typedef EST_TStructIterator< EST_TKVL<K, V>, IPointer, Entry> Entries;
185  typedef EST_TRwStructIterator< EST_TKVL<K, V>, IPointer, Entry> RwEntries;
186 
187  // Iteration support
188 
189 protected:
190  struct IPointer_k { EST_Litem *p; };
191 
192  void point_to_first(IPointer_k &ip) const { ip.p = list.head(); }
193  void move_pointer_forwards(IPointer_k &ip) const { ip.p = ip.p->next(); }
194  bool points_to_something(const IPointer_k &ip) const { return ip.p != NULL; }
195  K &points_at(const IPointer_k &ip) { return list(ip.p).k; }
196 
197  friend class EST_TIterator< EST_TKVL<K, V>, IPointer_k, K >;
198  friend class EST_TRwIterator< EST_TKVL<K, V>, IPointer_k, K >;
199 
200 public:
201  typedef K KeyEntry;
202  typedef EST_TIterator< EST_TKVL<K, V>, IPointer_k, KeyEntry> KeyEntries;
203  typedef EST_TRwIterator< EST_TKVL<K, V>, IPointer_k, KeyEntry> KeyRwEntries;
204 
205 };
206 
207 #endif // __KVL_H__
static K * default_key
default value, returned when there is no such entry.
Definition: EST_TKVL.h:92
EST_TKVL()
default constructor
Definition: EST_TKVL.h:83
Templated Key-Value Item. Serves as the items in the list of the EST_TKVL class.
Definition: EST_TKVL.h:57
EST_TList< EST_TKVI< K, V > > list
Linked list of key-val pairs. Don't use this as it will be made private in the future.
Definition: EST_TKVL.h:96
EST_Litem * head() const
Return First key value pair in list.
Definition: EST_TKVL.h:102
Templated Key-Value list. Objects of type EST_TKVL contain lists which are accessed by a key of type ...
Definition: EST_TKVL.h:75
const int length() const
number of key value pairs in list
Definition: EST_TKVL.h:99
static V * default_val
default value, returned when there is no such entry.
Definition: EST_TKVL.h:89