Edinburgh Speech Tools  2.4-release
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends 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 /** Templated Key-Value Item. Serves as the items in the list of the
54 EST_TKVL class. */
55 template<class K, class V> class EST_TKVI {
56  public:
57  K k;
58  V v;
59 
60  inline bool operator==(const EST_TKVI<K,V> &i){
61  return( (i.k == k) && (i.v == v) );
62  }
63 
64  friend ostream& operator << (ostream& s, EST_TKVI<K,V> const &i)
65  { return s << i.k << "\t" << i.v << "\n"; }
66 };
67 
68 
69 /** Templated Key-Value list. Objects of type EST_TKVL contain lists which
70 are accessed by a key of type {\bf K}, which returns a value of type
71 {\bf V}. */
72 template<class K, class V> class EST_TKVL {
73  private:
74  EST_Litem *find_pair_key(const K &key) const;
75  EST_Litem *find_pair_val(const V &val) const;
76  public:
77  /**@name Constructor functions */
78  //@{
79  /// default constructor
80  EST_TKVL() {;}
81  /// copy constructor
82  EST_TKVL(const EST_TKVL<K, V> &kv);
83  //@}
84 
85  /// default value, returned when there is no such entry.
86  static V *default_val;
87 
88  /// default value, returned when there is no such entry.
89  static K *default_key;
90 
91  /// Linked list of key-val pairs. Don't use
92  /// this as it will be made private in the future
94 
95  /// number of key value pairs in list
96  const int length() const {return list.length();}
97 
98  /// Return First key value pair in list
99  EST_Litem * head() const {return list.head();};
100 
101  /// Empty list.
102  void clear();
103 
104  /**@name Access functions.
105  */
106  //@{
107  /// return value according to key (const)
108  const V &val(const K &rkey, bool m=0) const;
109  /// return value according to key (non-const)
110  V &val(const K &rkey, bool m=0);
111  /// return value according to ptr
112  const V &val(EST_Litem *ptr, bool m=0) const;
113  /// return value according to ptr
114  V &val(EST_Litem *ptr, bool m=0);
115  /// value or default
116  const V &val_def(const K &rkey, const V &def) const;
117 
118  /// find key, reference by ptr
119  const K &key(EST_Litem *ptr, int m=1) const;
120  /// find key, reference by ptr
121  K &key(EST_Litem *ptr, int m=1);
122 
123  /// return first matching key, referenced by val
124  const K &key(const V &v, int m=1) const;
125 
126  /** change key-val pair. If no corresponding entry is present, add
127  to end of list.
128  */
129  int change_val(const K &rkey,const V &rval);
130  /** change key-val pair. If no corresponding entry is present, add
131  to end of list.*/
132  int change_val(EST_Litem *ptr,const V &rval); // change key-val pair.
133  /// change name of key pair.
134  int change_key(EST_Litem *ptr,const K &rkey);
135 
136  /// add key-val pair to list
137  int add_item(const K &rkey,const V &rval, int no_search = 0);
138 
139  /// remove key and val pair from list
140  int remove_item(const K &rkey, int quiet = 0);
141 
142  //@}
143 
144  /// Returns true if key is present.
145  const int present(const K &rkey) const;
146 
147  /// apply function to each pair
148  void map(void (*func)(K&, V&));
149 
150  friend ostream& operator << (ostream& s, EST_TKVL<K,V> const &l)
151  {EST_Litem *p;
152  for (p = l.list.head(); p ; p = p->next())
153  s << l.list(p).k << "\t" << l.list(p).v << endl;
154  return s;
155  }
156 
157  /// full copy of KV list.
158  EST_TKVL<K, V> & operator = (const EST_TKVL<K,V> &kv);
159  /// add kv after existing list.
160  EST_TKVL<K, V> & operator += (const EST_TKVL<K,V> &kv);
161  /// make new concatenated list
162  EST_TKVL<K, V> operator + (const EST_TKVL<K,V> &kv);
163 
164  // Iteration support
165 
166 protected:
167  struct IPointer { EST_Litem *p; };
168 
169  void point_to_first(IPointer &ip) const { ip.p = list.head(); }
170  void move_pointer_forwards(IPointer &ip) const { ip.p = ip.p->next(); }
171  bool points_to_something(const IPointer &ip) const { return ip.p != NULL; }
172  EST_TKVI<K, V> &points_at(const IPointer &ip) { return list(ip.p); }
173 
174  friend class EST_TIterator< EST_TKVL<K, V>, IPointer, EST_TKVI<K, V> >;
175  friend class EST_TStructIterator< EST_TKVL<K, V>, IPointer, EST_TKVI<K, V> >;
176  friend class EST_TRwIterator< EST_TKVL<K, V>, IPointer, EST_TKVI<K, V> >;
177  friend class EST_TRwStructIterator< EST_TKVL<K, V>, IPointer, EST_TKVI<K, V> >;
178 
179 public:
180  typedef EST_TKVI<K, V> Entry;
181  typedef EST_TStructIterator< EST_TKVL<K, V>, IPointer, Entry> Entries;
182  typedef EST_TRwStructIterator< EST_TKVL<K, V>, IPointer, Entry> RwEntries;
183 
184  // Iteration support
185 
186 protected:
187  struct IPointer_k { EST_Litem *p; };
188 
189  void point_to_first(IPointer_k &ip) const { ip.p = list.head(); }
190  void move_pointer_forwards(IPointer_k &ip) const { ip.p = ip.p->next(); }
191  bool points_to_something(const IPointer_k &ip) const { return ip.p != NULL; }
192  K &points_at(const IPointer_k &ip) { return list(ip.p).k; }
193 
194  friend class EST_TIterator< EST_TKVL<K, V>, IPointer_k, K >;
195  friend class EST_TRwIterator< EST_TKVL<K, V>, IPointer_k, K >;
196 
197 public:
198  typedef K KeyEntry;
199  typedef EST_TIterator< EST_TKVL<K, V>, IPointer_k, KeyEntry> KeyEntries;
200  typedef EST_TRwIterator< EST_TKVL<K, V>, IPointer_k, KeyEntry> KeyRwEntries;
201 
202 };
203 
204 #endif // __KVL_H__