Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Dictionary.h
Go to the documentation of this file.
1 /* Project FoundationEx
2  Destroyer Studios LLC
3  Copyright © 1997-2004. All Rights Reserved.
4 
5  SUBSYSTEM: FoundationEx
6  FILE: Dictionary.h
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Declaration of the Dictionary class
13 */
14 
15 #ifndef Dictionary_h
16 #define Dictionary_h
17 
18 #include "Text.h"
19 
20 // +-------------------------------------------------------------------+
21 
22 template <class T> class Dictionary;
23 template <class T> class DictionaryIter;
24 template <class T> class DictionaryCell;
25 
26 // +-------------------------------------------------------------------+
27 
28 template <class T> class Dictionary
29 {
30 public:
31  Dictionary();
32  ~Dictionary();
33 
34  T& operator[](const Text& key);
35 
36  void insert(const Text& key, const T& val);
37  void remove(const Text& key);
38 
39  void clear();
40 
41  int size() const { return items; }
42  int isEmpty() const { return !items; }
43 
44  int contains(const Text& key) const;
45  T find(const Text& key, T defval) const;
46 
47 private:
48  void init();
49 
50  int items;
51 
52  typedef DictionaryCell<T>* PTR;
53  PTR table[256];
54 
55  friend class DictionaryIter<T>;
56 };
57 
58 // +-------------------------------------------------------------------+
59 
60 template <class T> class DictionaryIter
61 {
62 public:
65 
66  int operator++(); // prefix
67 
68  void reset();
69  void forth();
70 
71  Text key() const;
72  T value() const;
73 
74  void attach(Dictionary<T>& l);
76 
77 private:
78  Dictionary<T>* dict;
79  DictionaryCell<T>* here;
80  int chain;
81 };
82 
83 // +-------------------------------------------------------------------+
84 
85 template <class T> class DictionaryCell
86 {
87 public:
88  DictionaryCell(const Text& k) : key(k), value( ), next(0) { }
89  DictionaryCell(const Text& k, const T& v) : key(k), value(v), next(0) { }
91 
93  T value;
95 };
96 
97 // +-------------------------------------------------------------------+
98 
99 #include "Dictionary.inl"
100 #endif Dictionary_h
101