]>
git.saurik.com Git - wxWidgets.git/blob - src/common/hash.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHashTable implementation
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "hash.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
32 IMPLEMENT_DYNAMIC_CLASS(wxHashTable
, wxObject
)
34 wxHashTable::wxHashTable (int the_key_type
, int size
)
37 hash_table
= (wxList
**) NULL
;
38 Create(the_key_type
, size
);
40 m_deleteContents
= FALSE
;
43 current_position = -1;
44 current_node = (wxNode *) NULL;
46 key_type = the_key_type;
47 hash_table = new wxList *[size];
49 for (i = 0; i < size; i++)
50 hash_table[i] = (wxList *) NULL;
54 wxHashTable::~wxHashTable (void)
59 void wxHashTable::Destroy(void)
61 if (!hash_table
) return;
63 for (i
= 0; i
< n
; i
++)
70 bool wxHashTable::Create(int the_key_type
, int size
)
75 current_position
= -1;
76 current_node
= (wxNode
*) NULL
;
78 key_type
= the_key_type
;
79 hash_table
= new wxList
*[size
];
81 for (i
= 0; i
< size
; i
++)
82 hash_table
[i
] = (wxList
*) NULL
;
87 void wxHashTable::DoCopy(const wxHashTable
& table
)
90 current_position
= table
.current_position
;
91 current_node
= NULL
; // doesn't matter - Next() will reconstruct it
92 key_type
= table
.key_type
;
94 hash_table
= new wxList
*[n
];
95 for (int i
= 0; i
< n
; i
++) {
96 if (table
.hash_table
[i
] == NULL
)
99 hash_table
[i
] = new wxList(key_type
);
100 *(hash_table
[i
]) = *(table
.hash_table
[i
]);
105 void wxHashTable::Put (long key
, long value
, wxObject
* object
)
110 int position
= (int) (k
% n
);
111 if (position
< 0) position
= -position
;
113 if (!hash_table
[position
])
115 hash_table
[position
] = new wxList (wxKEY_INTEGER
);
116 if (m_deleteContents
) hash_table
[position
]->DeleteContents(TRUE
);
119 hash_table
[position
]->Append (value
, object
);
123 void wxHashTable::Put (long key
, const wxChar
*value
, wxObject
* object
)
128 int position
= (int) (k
% n
);
129 if (position
< 0) position
= -position
;
131 if (!hash_table
[position
])
133 hash_table
[position
] = new wxList (wxKEY_INTEGER
);
134 if (m_deleteContents
) hash_table
[position
]->DeleteContents(TRUE
);
137 hash_table
[position
]->Append (value
, object
);
141 void wxHashTable::Put (long key
, wxObject
* object
)
146 int position
= (int) (k
% n
);
147 if (position
< 0) position
= -position
;
149 if (!hash_table
[position
])
151 hash_table
[position
] = new wxList (wxKEY_INTEGER
);
152 if (m_deleteContents
) hash_table
[position
]->DeleteContents(TRUE
);
155 hash_table
[position
]->Append (k
, object
);
159 void wxHashTable::Put (const wxChar
*key
, wxObject
* object
)
161 int position
= (int) (MakeKey (key
) % n
);
162 if (position
< 0) position
= -position
;
164 if (!hash_table
[position
])
166 hash_table
[position
] = new wxList (wxKEY_STRING
);
167 if (m_deleteContents
) hash_table
[position
]->DeleteContents(TRUE
);
170 hash_table
[position
]->Append (key
, object
);
174 wxObject
*wxHashTable::Get (long key
, long value
) const
179 int position
= (int) (k
% n
);
180 if (position
< 0) position
= -position
;
182 if (!hash_table
[position
])
183 return (wxObject
*) NULL
;
186 wxNode
*node
= hash_table
[position
]->Find (value
);
188 return node
->Data ();
190 return (wxObject
*) NULL
;
194 wxObject
*wxHashTable::Get (long key
, const wxChar
*value
) const
199 int position
= (int) (k
% n
);
200 if (position
< 0) position
= -position
;
202 if (!hash_table
[position
])
203 return (wxObject
*) NULL
;
206 wxNode
*node
= hash_table
[position
]->Find (value
);
208 return node
->Data ();
210 return (wxObject
*) NULL
;
214 wxObject
*wxHashTable::Get (long key
) const
219 int position
= (int) (k
% n
);
220 if (position
< 0) position
= -position
;
222 if (!hash_table
[position
])
223 return (wxObject
*) NULL
;
226 wxNode
*node
= hash_table
[position
]->Find (k
);
227 return node
? node
->Data () : (wxObject
*)NULL
;
231 wxObject
*wxHashTable::Get (const wxChar
*key
) const
233 int position
= (int) (MakeKey (key
) % n
);
234 if (position
< 0) position
= -position
;
236 if (!hash_table
[position
])
237 return (wxObject
*) NULL
;
240 wxNode
*node
= hash_table
[position
]->Find (key
);
241 return node
? node
->Data () : (wxObject
*)NULL
;
245 wxObject
*wxHashTable::Delete (long key
)
250 int position
= (int) (k
% n
);
251 if (position
< 0) position
= -position
;
253 if (!hash_table
[position
])
254 return (wxObject
*) NULL
;
257 wxNode
*node
= hash_table
[position
]->Find (k
);
260 wxObject
*data
= node
->Data ();
266 return (wxObject
*) NULL
;
270 wxObject
*wxHashTable::Delete (const wxChar
*key
)
272 int position
= (int) (MakeKey (key
) % n
);
273 if (position
< 0) position
= -position
;
275 if (!hash_table
[position
])
276 return (wxObject
*) NULL
;
279 wxNode
*node
= hash_table
[position
]->Find (key
);
282 wxObject
*data
= node
->Data ();
288 return (wxObject
*) NULL
;
292 wxObject
*wxHashTable::Delete (long key
, int value
)
297 int position
= (int) (k
% n
);
298 if (position
< 0) position
= -position
;
300 if (!hash_table
[position
])
301 return (wxObject
*) NULL
;
304 wxNode
*node
= hash_table
[position
]->Find (value
);
307 wxObject
*data
= node
->Data ();
313 return (wxObject
*) NULL
;
317 wxObject
*wxHashTable::Delete (long key
, const wxChar
*value
)
319 int position
= (int) (key
% n
);
320 if (position
< 0) position
= -position
;
322 if (!hash_table
[position
])
323 return (wxObject
*) NULL
;
326 wxNode
*node
= hash_table
[position
]->Find (value
);
329 wxObject
*data
= node
->Data ();
335 return (wxObject
*) NULL
;
339 long wxHashTable::MakeKey (const wxChar
*string
) const
344 int_key
+= (wxUChar
) *string
++;
349 void wxHashTable::BeginFind (void)
351 current_position
= -1;
352 current_node
= (wxNode
*) NULL
;
355 wxNode
*wxHashTable::Next (void)
357 wxNode
*found
= (wxNode
*) NULL
;
359 while (!end
&& !found
)
364 if (current_position
>= n
)
366 current_position
= -1;
367 current_node
= (wxNode
*) NULL
;
372 if (hash_table
[current_position
])
374 current_node
= hash_table
[current_position
]->First ();
375 found
= current_node
;
381 current_node
= current_node
->Next ();
382 found
= current_node
;
388 void wxHashTable::DeleteContents (bool flag
)
391 m_deleteContents
= flag
;
392 for (i
= 0; i
< n
; i
++)
395 hash_table
[i
]->DeleteContents (flag
);
399 void wxHashTable::Clear (void)
402 for (i
= 0; i
< n
; i
++)
405 hash_table
[i
]->Clear ();