]>
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
);
41 current_position = -1;
42 current_node = (wxNode *) NULL;
44 key_type = the_key_type;
45 hash_table = new wxList *[size];
47 for (i = 0; i < size; i++)
48 hash_table[i] = (wxList *) NULL;
52 wxHashTable::~wxHashTable (void)
57 void wxHashTable::Destroy(void)
59 if (!hash_table
) return;
61 for (i
= 0; i
< n
; i
++)
68 bool wxHashTable::Create(int the_key_type
, int size
)
73 current_position
= -1;
74 current_node
= (wxNode
*) NULL
;
76 key_type
= the_key_type
;
77 hash_table
= new wxList
*[size
];
79 for (i
= 0; i
< size
; i
++)
80 hash_table
[i
] = (wxList
*) NULL
;
85 void wxHashTable::DoCopy(const wxHashTable
& table
)
88 current_position
= table
.current_position
;
89 current_node
= NULL
; // doesn't matter - Next() will reconstruct it
90 key_type
= table
.key_type
;
92 hash_table
= new wxList
*[n
];
93 for (int i
= 0; i
< n
; i
++) {
94 if (table
.hash_table
[i
] == NULL
)
97 hash_table
[i
] = new wxList(key_type
);
98 *(hash_table
[i
]) = *(table
.hash_table
[i
]);
103 void wxHashTable::Put (long key
, long value
, wxObject
* object
)
110 int position
= (int) (k
% n
);
111 if (!hash_table
[position
])
112 hash_table
[position
] = new wxList (wxKEY_INTEGER
);
114 hash_table
[position
]->Append (value
, object
);
117 void wxHashTable::Put (long key
, const wxChar
*value
, wxObject
* object
)
124 int position
= (int) (k
% n
);
125 if (!hash_table
[position
])
126 hash_table
[position
] = new wxList (wxKEY_INTEGER
);
128 hash_table
[position
]->Append (value
, object
);
131 void wxHashTable::Put (long key
, wxObject
* object
)
138 int position
= (int) (k
% n
);
139 if (!hash_table
[position
])
140 hash_table
[position
] = new wxList (wxKEY_INTEGER
);
142 hash_table
[position
]->Append (k
, object
);
145 void wxHashTable::Put (const wxChar
*key
, wxObject
* object
)
147 int position
= (int) (MakeKey (key
) % n
);
149 if (!hash_table
[position
])
150 hash_table
[position
] = new wxList (wxKEY_STRING
);
152 hash_table
[position
]->Append (key
, object
);
155 wxObject
*wxHashTable::Get (long key
, long value
) const
162 int position
= (int) (k
% n
);
163 if (!hash_table
[position
])
164 return (wxObject
*) NULL
;
167 wxNode
*node
= hash_table
[position
]->Find (value
);
169 return node
->Data ();
171 return (wxObject
*) NULL
;
175 wxObject
*wxHashTable::Get (long key
, const wxChar
*value
) const
182 int position
= (int) (k
% n
);
183 if (!hash_table
[position
])
184 return (wxObject
*) NULL
;
187 wxNode
*node
= hash_table
[position
]->Find (value
);
189 return node
->Data ();
191 return (wxObject
*) NULL
;
195 wxObject
*wxHashTable::Get (long key
) const
202 int position
= (int) (k
% n
);
203 if (!hash_table
[position
])
204 return (wxObject
*) NULL
;
207 wxNode
*node
= hash_table
[position
]->Find (k
);
208 return node
? node
->Data () : (wxObject
*)NULL
;
212 wxObject
*wxHashTable::Get (const wxChar
*key
) const
214 int position
= (int) (MakeKey (key
) % n
);
216 if (!hash_table
[position
])
217 return (wxObject
*) NULL
;
220 wxNode
*node
= hash_table
[position
]->Find (key
);
221 return node
? node
->Data () : (wxObject
*)NULL
;
225 wxObject
*wxHashTable::Delete (long key
)
232 int position
= (int) (k
% n
);
233 if (!hash_table
[position
])
234 return (wxObject
*) NULL
;
237 wxNode
*node
= hash_table
[position
]->Find (k
);
240 wxObject
*data
= node
->Data ();
245 return (wxObject
*) NULL
;
249 wxObject
*wxHashTable::Delete (const wxChar
*key
)
251 int position
= (int) (MakeKey (key
) % n
);
252 if (!hash_table
[position
])
253 return (wxObject
*) NULL
;
256 wxNode
*node
= hash_table
[position
]->Find (key
);
259 wxObject
*data
= node
->Data ();
264 return (wxObject
*) NULL
;
268 wxObject
*wxHashTable::Delete (long key
, int value
)
275 int position
= (int) (k
% n
);
276 if (!hash_table
[position
])
277 return (wxObject
*) NULL
;
280 wxNode
*node
= hash_table
[position
]->Find (value
);
283 wxObject
*data
= node
->Data ();
288 return (wxObject
*) NULL
;
292 wxObject
*wxHashTable::Delete (long key
, const wxChar
*value
)
294 int position
= (int) (key
% n
);
295 if (!hash_table
[position
])
296 return (wxObject
*) NULL
;
299 wxNode
*node
= hash_table
[position
]->Find (value
);
302 wxObject
*data
= node
->Data ();
307 return (wxObject
*) NULL
;
311 long wxHashTable::MakeKey (const wxChar
*string
) const
316 int_key
+= (wxUChar
) *string
++;
321 void wxHashTable::BeginFind (void)
323 current_position
= -1;
324 current_node
= (wxNode
*) NULL
;
327 wxNode
*wxHashTable::Next (void)
329 wxNode
*found
= (wxNode
*) NULL
;
331 while (!end
&& !found
)
336 if (current_position
>= n
)
338 current_position
= -1;
339 current_node
= (wxNode
*) NULL
;
344 if (hash_table
[current_position
])
346 current_node
= hash_table
[current_position
]->First ();
347 found
= current_node
;
353 current_node
= current_node
->Next ();
354 found
= current_node
;
360 void wxHashTable::DeleteContents (bool flag
)
363 for (i
= 0; i
< n
; i
++)
366 hash_table
[i
]->DeleteContents (flag
);
370 void wxHashTable::Clear (void)
373 for (i
= 0; i
< n
; i
++)
376 hash_table
[i
]->Clear ();