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