ACE+TAO-7_0_8
[ACE_TAO.git] / ACE / apps / JAWS2 / JAWS / Assoc_Array.h
blob811f75ae4d559ab2156819c31fba6bac64350be2
1 /* -*- c++ -*- */
2 #ifndef JAWS_ASSOC_ARRAY_H
3 #define JAWS_ASSOC_ARRAY_H
5 template <class KEY, class DATA> class JAWS_Assoc_Array_Iterator;
7 template <class KEY, class DATA>
8 class JAWS_Assoc_Array
11 friend class JAWS_Assoc_Array_Iterator<KEY, DATA>;
13 public:
14 JAWS_Assoc_Array (int maxsize = 1024);
15 ~JAWS_Assoc_Array (void);
17 int index (const KEY &k);
18 // Returns the index into the array associated with key k
19 // Returns -1 if not found.
21 DATA * find (const KEY &k);
22 // Returns the data associated with key k. 0 if not found.
24 DATA * find_by_index (int i);
25 // Returns the data associated with array index i. Returns 0 if the
26 // index is invalid.
28 DATA * insert (const KEY &k, const DATA &d);
29 // Inserts a *copy* of the key and data into the associated array.
30 // Both KEY and DATA must have well defined semantics for copy
31 // construction. This method returns a pointer to the inserted item
32 // copy, or 0 if an error occurred. NOTE: if an identical key
33 // already exists in the tree, no new item is created, and the
34 // returned pointer addresses the existing item associated with the
35 // existing key.
37 int remove (const KEY &k);
38 // Removes the item associated with the given key from the
39 // tree and destroys it. Returns 1 if it found the item
40 // and successfully destroyed it, 0 if it did not find the
41 // item, or -1 if an error occurred.
43 void clear (void);
44 // Destroys all keys and associated data.
46 protected:
48 int find_i (const KEY &k);
49 // If k points to an associated data item, then this function
50 // returns the index into the arrays that hold it. Otherwise, it
51 // returns an index suitable to insert the item. If the item is not
52 // found and the table is full, maxsize_ is returned.
54 private:
55 KEY **k_array_;
56 DATA **d_array_;
57 int maxsize_;
60 template <class KEY, class DATA>
61 class JAWS_Assoc_Array_Iterator
63 public:
65 JAWS_Assoc_Array_Iterator (const JAWS_Assoc_Array<KEY, DATA> &aa);
66 ~JAWS_Assoc_Array_Iterator (void);
68 KEY * key (void);
69 DATA * data (void);
71 int first (void);
72 int last (void);
73 int next (void);
74 int previous (void);
75 int is_done (void);
77 private:
79 // declare private and do not define: explicitly
80 // prevent assignment and copy construction of iterators
81 JAWS_Assoc_Array_Iterator (const JAWS_Assoc_Array_Iterator<KEY, DATA> &);
82 void operator= (const JAWS_Assoc_Array_Iterator<KEY, DATA> &);
84 private:
86 const JAWS_Assoc_Array<KEY, DATA> &aa_;
88 int i_;
89 // The current item pointed by iterator.
91 int j_;
92 // The next item to be pointed to by iterator.
96 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
97 #include "JAWS/Assoc_Array.cpp"
98 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */
100 #endif /* !defined (JAWS_ASSOC_ARRAY_H) */