]>
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
)
112 int position
= (int) (k
% n
);
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
)
130 int position
= (int) (k
% n
);
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
)
148 int position
= (int) (k
% n
);
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
);
163 if (!hash_table
[position
])
165 hash_table
[position
] = new wxList (wxKEY_STRING
);
166 if (m_deleteContents
) hash_table
[position
]->DeleteContents(TRUE
);
169 hash_table
[position
]->Append (key
, object
);
173 wxObject
*wxHashTable::Get (long key
, long value
) const
180 int position
= (int) (k
% n
);
181 if (!hash_table
[position
])
182 return (wxObject
*) NULL
;
185 wxNode
*node
= hash_table
[position
]->Find (value
);
187 return node
->Data ();
189 return (wxObject
*) NULL
;
193 wxObject
*wxHashTable::Get (long key
, const wxChar
*value
) const
200 int position
= (int) (k
% n
);
201 if (!hash_table
[position
])
202 return (wxObject
*) NULL
;
205 wxNode
*node
= hash_table
[position
]->Find (value
);
207 return node
->Data ();
209 return (wxObject
*) NULL
;
213 wxObject
*wxHashTable::Get (long key
) const
220 int position
= (int) (k
% n
);
221 if (!hash_table
[position
])
222 return (wxObject
*) NULL
;
225 wxNode
*node
= hash_table
[position
]->Find (k
);
226 return node
? node
->Data () : (wxObject
*)NULL
;
230 wxObject
*wxHashTable::Get (const wxChar
*key
) const
232 int position
= (int) (MakeKey (key
) % n
);
234 if (!hash_table
[position
])
235 return (wxObject
*) NULL
;
238 wxNode
*node
= hash_table
[position
]->Find (key
);
239 return node
? node
->Data () : (wxObject
*)NULL
;
243 wxObject
*wxHashTable::Delete (long key
)
250 int position
= (int) (k
% n
);
251 if (!hash_table
[position
])
252 return (wxObject
*) NULL
;
255 wxNode
*node
= hash_table
[position
]->Find (k
);
258 wxObject
*data
= node
->Data ();
264 return (wxObject
*) NULL
;
268 wxObject
*wxHashTable::Delete (const wxChar
*key
)
270 int position
= (int) (MakeKey (key
) % n
);
271 if (!hash_table
[position
])
272 return (wxObject
*) NULL
;
275 wxNode
*node
= hash_table
[position
]->Find (key
);
278 wxObject
*data
= node
->Data ();
284 return (wxObject
*) NULL
;
288 wxObject
*wxHashTable::Delete (long key
, int value
)
295 int position
= (int) (k
% n
);
296 if (!hash_table
[position
])
297 return (wxObject
*) NULL
;
300 wxNode
*node
= hash_table
[position
]->Find (value
);
303 wxObject
*data
= node
->Data ();
309 return (wxObject
*) NULL
;
313 wxObject
*wxHashTable::Delete (long key
, const wxChar
*value
)
315 int position
= (int) (key
% n
);
316 if (!hash_table
[position
])
317 return (wxObject
*) NULL
;
320 wxNode
*node
= hash_table
[position
]->Find (value
);
323 wxObject
*data
= node
->Data ();
329 return (wxObject
*) NULL
;
333 long wxHashTable::MakeKey (const wxChar
*string
) const
338 int_key
+= (wxUChar
) *string
++;
343 void wxHashTable::BeginFind (void)
345 current_position
= -1;
346 current_node
= (wxNode
*) NULL
;
349 wxNode
*wxHashTable::Next (void)
351 wxNode
*found
= (wxNode
*) NULL
;
353 while (!end
&& !found
)
358 if (current_position
>= n
)
360 current_position
= -1;
361 current_node
= (wxNode
*) NULL
;
366 if (hash_table
[current_position
])
368 current_node
= hash_table
[current_position
]->First ();
369 found
= current_node
;
375 current_node
= current_node
->Next ();
376 found
= current_node
;
382 void wxHashTable::DeleteContents (bool flag
)
385 m_deleteContents
= flag
;
386 for (i
= 0; i
< n
; i
++)
389 hash_table
[i
]->DeleteContents (flag
);
393 void wxHashTable::Clear (void)
396 for (i
= 0; i
< n
; i
++)
399 hash_table
[i
]->Clear ();