/work/dcmtk-install/include/dcmtk/dcmdata/dchashdi.h
Line | Count | Source |
1 | | /* |
2 | | * |
3 | | * Copyright (C) 1994-2023, OFFIS e.V. |
4 | | * All rights reserved. See COPYRIGHT file for details. |
5 | | * |
6 | | * This software and supporting documentation were developed by |
7 | | * |
8 | | * OFFIS e.V. |
9 | | * R&D Division Health |
10 | | * Escherweg 2 |
11 | | * D-26121 Oldenburg, Germany |
12 | | * |
13 | | * |
14 | | * Module: dcmdata |
15 | | * |
16 | | * Author: Andrew Hewett |
17 | | * |
18 | | * Purpose: Hash table interface for DICOM data dictionary |
19 | | * |
20 | | */ |
21 | | |
22 | | #ifndef DCHASHDI_H |
23 | | #define DCHASHDI_H |
24 | | |
25 | | #include "dcmtk/config/osconfig.h" /* make sure OS specific configuration is included first */ |
26 | | #include "dcmtk/ofstd/oflist.h" |
27 | | #include "dcmtk/ofstd/ofstream.h" |
28 | | #include "dcmtk/dcmdata/dcdefine.h" |
29 | | |
30 | | class DcmDictEntry; |
31 | | class DcmTagKey; |
32 | | class DcmHashDict; |
33 | | |
34 | | typedef OFListIterator(DcmDictEntry *) DcmDictEntryListIterator; |
35 | | typedef OFListConstIterator(DcmDictEntry *) DcmDictEntryListConstIterator; |
36 | | |
37 | | /** an ordered list of pointers to DcmDictEntry objects |
38 | | */ |
39 | | class DCMTK_DCMDATA_EXPORT DcmDictEntryList |
40 | | { |
41 | | public: |
42 | | |
43 | | /// constructor |
44 | | DcmDictEntryList() : list_() {} |
45 | | |
46 | | /// destructor |
47 | | ~DcmDictEntryList(); |
48 | | |
49 | | /// clears list and deletes all entries |
50 | | void clear(); |
51 | | |
52 | | /// @return the number of elements in this list |
53 | | unsigned int size() const; |
54 | | |
55 | | /// @return True if this list is empty |
56 | | OFBool empty() const; |
57 | | |
58 | | /// @return Iterator to the beginning of the list |
59 | | DcmDictEntryListIterator begin(); |
60 | | |
61 | | /// @return Iterator past the end of the list |
62 | | DcmDictEntryListIterator end(); |
63 | | |
64 | | /// @return Iterator to the beginning of the list |
65 | | DcmDictEntryListConstIterator begin() const; |
66 | | |
67 | | /// @return Iterator past the end of the list |
68 | | DcmDictEntryListConstIterator end() const; |
69 | | |
70 | | /** Insert a new element in the list |
71 | | * @param position Position to insert after |
72 | | * @param entry Entry to insert |
73 | | * @return Iterator to the new entry |
74 | | */ |
75 | | DcmDictEntryListIterator insert(DcmDictEntryListIterator position, DcmDictEntry *entry); |
76 | | |
77 | | /** Remove an element from the list. |
78 | | * @param entry The entry to remove |
79 | | */ |
80 | | void remove(DcmDictEntry *entry); |
81 | | |
82 | | /** Append a new entry to the list. |
83 | | * @param entry The entry to append. |
84 | | */ |
85 | | void push_back(DcmDictEntry *entry); |
86 | | |
87 | | /** inserts an entry into the list and returns any replaced entry |
88 | | * @param entry new list entry |
89 | | * @return replaced list entry or NULL |
90 | | */ |
91 | | DcmDictEntry* insertAndReplace(DcmDictEntry* entry); |
92 | | |
93 | | /** find an entry in the set |
94 | | * @param key tag key of the entry to be searched for |
95 | | * @param privCreator private creator identifier, may be NULL |
96 | | * @return pointer to entry (if found), otherwise NULL |
97 | | */ |
98 | | DcmDictEntry *find(const DcmTagKey& key, const char *privCreator); |
99 | | |
100 | | private: |
101 | | |
102 | | /// private undefined copy constructor |
103 | | DcmDictEntryList(const DcmDictEntryList&); |
104 | | |
105 | | /// private undefined copy assignment operator |
106 | | DcmDictEntryList& operator=(const DcmDictEntryList&); |
107 | | |
108 | | /// list of entries |
109 | | OFList<DcmDictEntry *> list_; |
110 | | }; |
111 | | |
112 | | |
113 | | /** iterator class for traversing a DcmHashDict |
114 | | */ |
115 | | class DCMTK_DCMDATA_EXPORT DcmHashDictIterator |
116 | | { |
117 | | public: |
118 | | |
119 | | /// default constructor |
120 | | DcmHashDictIterator() |
121 | | : dict(NULL), hindex(0), iterating(OFFalse), iter() |
122 | | { init(NULL); } |
123 | | |
124 | | /** constructor, creates iterator to existing hash dictionary |
125 | | * @param d pointer to dictionary |
126 | | * @param atEnd if true, iterator points after last element |
127 | | * of hash dictionary, otherwise iterator points to first element |
128 | | */ |
129 | | DcmHashDictIterator(const DcmHashDict* d, OFBool atEnd = OFFalse) |
130 | | : dict(NULL), hindex(0), iterating(OFFalse), iter() |
131 | | { init(d, atEnd); } |
132 | | |
133 | | /** copy constructor |
134 | | * @param i the iterator to copy |
135 | | */ |
136 | | DcmHashDictIterator(const DcmHashDictIterator& i) |
137 | | : dict(i.dict), hindex(i.hindex), iterating(i.iterating), iter(i.iter) |
138 | 0 | { } |
139 | | |
140 | | /** copy assignment operator |
141 | | * @param i the Iterator to copy and assign |
142 | | */ |
143 | | DcmHashDictIterator& operator=(const DcmHashDictIterator& i) |
144 | | { dict = i.dict; hindex = i.hindex; |
145 | | iterating = i.iterating; iter = i.iter; return *this; } |
146 | | |
147 | | /** comparison equality |
148 | | * @param x the iterator to compare against |
149 | | */ |
150 | | OFBool operator==(const DcmHashDictIterator& x) const |
151 | | { return iterating ? x.iterating && (hindex == x.hindex) && (iter == x.iter) : !x.iterating; } |
152 | | |
153 | | /** comparison non-equality |
154 | | * @param x the iterator to compare against |
155 | | */ |
156 | | OFBool operator!=(const DcmHashDictIterator& x) const |
157 | | { return !(*this == x); } |
158 | | |
159 | | /// dereferencing of iterator |
160 | | const DcmDictEntry* operator*() const |
161 | | { return (*iter); } |
162 | | |
163 | | /// pre-increment operator |
164 | | DcmHashDictIterator& operator++() |
165 | | { stepUp(); return *this; } |
166 | | |
167 | | /// post-increment operator |
168 | | DcmHashDictIterator operator++(int) |
169 | 0 | { DcmHashDictIterator tmp(*this); stepUp(); return tmp; } |
170 | | |
171 | | private: |
172 | | |
173 | | /** initializes the iterator |
174 | | * @param d pointer to hash dictionary, may be NULL |
175 | | * @param atEnd if true, iterator points after last element |
176 | | * of hash dictionary, otherwise iterator points to first element |
177 | | */ |
178 | | void init(const DcmHashDict *d, OFBool atEnd = OFFalse); |
179 | | |
180 | | /** implements increment operator on hash dictionary |
181 | | */ |
182 | | void stepUp(); |
183 | | |
184 | | /// pointer to the hash dictionary this iterator traverses |
185 | | const DcmHashDict* dict; |
186 | | |
187 | | /// index of current bucket |
188 | | int hindex; |
189 | | |
190 | | /// flag indicating if iter is currently valid |
191 | | OFBool iterating; |
192 | | |
193 | | /// iterator for traversing a bucket in the hash table |
194 | | DcmDictEntryListIterator iter; |
195 | | }; |
196 | | |
197 | | |
198 | | /** a hash table of pointers to DcmDictEntry objects |
199 | | */ |
200 | | class DCMTK_DCMDATA_EXPORT DcmHashDict |
201 | | { |
202 | | |
203 | | public: |
204 | | /// default constructor |
205 | | DcmHashDict() |
206 | | : hashTab(NULL), lowestBucket(0), highestBucket(0), entryCount(0) |
207 | | { _init(); } |
208 | | |
209 | | /// destructor |
210 | | ~DcmHashDict(); |
211 | | |
212 | | /** counts total number of entries |
213 | | * @return number of entries |
214 | | */ |
215 | | int size() const { return entryCount; } |
216 | | |
217 | | /// clears the hash table of all entries |
218 | | void clear(); |
219 | | |
220 | | /** inserts an entry into hash table (deletes old entry if present) |
221 | | * @param entry pointer to new entry |
222 | | */ |
223 | | void put(DcmDictEntry* entry); |
224 | | |
225 | | /** hash table lookup for the given tag key and private creator name |
226 | | * @param key tag key of the entry to be searched for |
227 | | * @param privCreator private creator identifier, may be NULL |
228 | | * @return pointer to entry (if found), otherwise NULL |
229 | | */ |
230 | | const DcmDictEntry* get(const DcmTagKey& key, const char *privCreator) const; |
231 | | |
232 | | /** deletes the entry for the given tag and private creator identifier |
233 | | * @param key tag key of the entry to be deleted |
234 | | * @param privCreator private creator identifier, may be NULL |
235 | | */ |
236 | | void del(const DcmTagKey& key, const char *privCreator); |
237 | | |
238 | | // iterator over the contents of the hash table |
239 | | friend class DcmHashDictIterator; |
240 | | |
241 | | /// returns iterator to start of hash table |
242 | | DcmHashDictIterator begin() const |
243 | | { DcmHashDictIterator iter(this); return iter; } |
244 | | |
245 | | /// returns iterator to end of hash table |
246 | | DcmHashDictIterator end() const |
247 | | { DcmHashDictIterator iter(this, OFTrue); return iter; } |
248 | | |
249 | | /** prints some information about hash table bucket utilization |
250 | | * @param out the stream to print into |
251 | | */ |
252 | | STD_NAMESPACE ostream& loadSummary(STD_NAMESPACE ostream& out); |
253 | | |
254 | | private: |
255 | | |
256 | | /// private unimplemented copy constructor |
257 | | DcmHashDict(const DcmHashDict &); |
258 | | |
259 | | /// private unimplemented copy assignment operator |
260 | | DcmHashDict &operator=(const DcmHashDict &); |
261 | | |
262 | | /// performs initialization for given hash table size, called from constructor |
263 | | void _init(); |
264 | | |
265 | | /** compute hash value for given tag key |
266 | | * @param key pointer to tag key |
267 | | * @param privCreator private creator identifier, may be NULL |
268 | | * @return hash value |
269 | | */ |
270 | | int hash(const DcmTagKey* key, const char *privCreator) const; |
271 | | |
272 | | /** inserts new entry into given list |
273 | | * @param lst list to add to |
274 | | * @param entry new element to add, will be deleted upon destruction of the hash table |
275 | | * @return pointer to replaced element, if any |
276 | | */ |
277 | | DcmDictEntry* insertInList(DcmDictEntryList& lst, DcmDictEntry* entry); |
278 | | |
279 | | /** removes the entry for the given tag and private creator identifier |
280 | | * @param lst list to remove from |
281 | | * @param key tag key of the entry to be removed |
282 | | * @param privCreator private creator identifier, may be NULL |
283 | | * @return pointer to removed element, if any |
284 | | */ |
285 | | DcmDictEntry* removeInList(DcmDictEntryList& lst, const DcmTagKey& key, const char *privCreator); |
286 | | |
287 | | /** searches entry for the given tag and private creator identifier |
288 | | * @param lst list to search in |
289 | | * @param key tag key of the entry to be searched for |
290 | | * @param privCreator private creator identifier, may be NULL |
291 | | * @return pointer to found element, NULL if not found |
292 | | */ |
293 | | DcmDictEntry* findInList(DcmDictEntryList& lst, const DcmTagKey& key, const char *privCreator) const; |
294 | | |
295 | | /** array of (hash table size) pointers to DcmDictEntryList elements |
296 | | * implementing the different buckets of the hash table |
297 | | */ |
298 | | DcmDictEntryList** hashTab; |
299 | | |
300 | | /// number of buckets in hash table |
301 | | static const int hashTabLength; |
302 | | |
303 | | /// index of lowest bucket for which the DcmDictEntryList has been initialized |
304 | | int lowestBucket; |
305 | | |
306 | | /// index of highest bucket for which the DcmDictEntryList has been initialized |
307 | | int highestBucket; |
308 | | |
309 | | /// number of entries in hash table |
310 | | int entryCount; |
311 | | |
312 | | }; |
313 | | |
314 | | #endif /* DCHASHDI_H */ |