]>
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 current_position
= -1;
38 current_node
= (wxNode
*) NULL
;
40 key_type
= the_key_type
;
41 hash_table
= new wxList
*[size
];
43 for (i
= 0; i
< size
; i
++)
44 hash_table
[i
] = (wxList
*) NULL
;
47 wxHashTable::~wxHashTable (void)
52 void wxHashTable::Destroy(void)
54 if (!hash_table
) return;
56 for (i
= 0; i
< n
; i
++)
63 bool wxHashTable::Create(int the_key_type
, int size
)
66 current_position
= -1;
67 current_node
= (wxNode
*) NULL
;
69 key_type
= the_key_type
;
72 hash_table
= new wxList
*[size
];
74 for (i
= 0; i
< size
; i
++)
75 hash_table
[i
] = (wxList
*) NULL
;
80 void wxHashTable::DoCopy(const wxHashTable
& table
)
83 current_position
= table
.current_position
;
84 current_node
= NULL
; // doesn't matter - Next() will reconstruct it
85 key_type
= table
.key_type
;
87 hash_table
= new wxList
*[n
];
88 for (int i
= 0; i
< n
; i
++) {
89 if (table
.hash_table
[i
] == NULL
)
92 hash_table
[i
] = new wxList(key_type
);
93 *(hash_table
[i
]) = *(table
.hash_table
[i
]);
98 void wxHashTable::Put (long key
, long value
, wxObject
* object
)
105 int position
= (int) (k
% n
);
106 if (!hash_table
[position
])
107 hash_table
[position
] = new wxList (wxKEY_INTEGER
);
109 hash_table
[position
]->Append (value
, object
);
112 void wxHashTable::Put (long key
, const wxChar
*value
, wxObject
* object
)
119 int position
= (int) (k
% n
);
120 if (!hash_table
[position
])
121 hash_table
[position
] = new wxList (wxKEY_INTEGER
);
123 hash_table
[position
]->Append (value
, object
);
126 void wxHashTable::Put (long key
, wxObject
* object
)
133 int position
= (int) (k
% n
);
134 if (!hash_table
[position
])
135 hash_table
[position
] = new wxList (wxKEY_INTEGER
);
137 hash_table
[position
]->Append (k
, object
);
140 void wxHashTable::Put (const wxChar
*key
, wxObject
* object
)
142 int position
= (int) (MakeKey (key
) % n
);
144 if (!hash_table
[position
])
145 hash_table
[position
] = new wxList (wxKEY_STRING
);
147 hash_table
[position
]->Append (key
, object
);
150 wxObject
*wxHashTable::Get (long key
, long value
) const
157 int position
= (int) (k
% n
);
158 if (!hash_table
[position
])
159 return (wxObject
*) NULL
;
162 wxNode
*node
= hash_table
[position
]->Find (value
);
164 return node
->Data ();
166 return (wxObject
*) NULL
;
170 wxObject
*wxHashTable::Get (long key
, const wxChar
*value
) const
177 int position
= (int) (k
% n
);
178 if (!hash_table
[position
])
179 return (wxObject
*) NULL
;
182 wxNode
*node
= hash_table
[position
]->Find (value
);
184 return node
->Data ();
186 return (wxObject
*) NULL
;
190 wxObject
*wxHashTable::Get (long key
) const
197 int position
= (int) (k
% n
);
198 if (!hash_table
[position
])
199 return (wxObject
*) NULL
;
202 wxNode
*node
= hash_table
[position
]->Find (k
);
203 return node
? node
->Data () : (wxObject
*)NULL
;
207 wxObject
*wxHashTable::Get (const wxChar
*key
) const
209 int position
= (int) (MakeKey (key
) % n
);
211 if (!hash_table
[position
])
212 return (wxObject
*) NULL
;
215 wxNode
*node
= hash_table
[position
]->Find (key
);
216 return node
? node
->Data () : (wxObject
*)NULL
;
220 wxObject
*wxHashTable::Delete (long key
)
227 int position
= (int) (k
% n
);
228 if (!hash_table
[position
])
229 return (wxObject
*) NULL
;
232 wxNode
*node
= hash_table
[position
]->Find (k
);
235 wxObject
*data
= node
->Data ();
240 return (wxObject
*) NULL
;
244 wxObject
*wxHashTable::Delete (const wxChar
*key
)
246 int position
= (int) (MakeKey (key
) % n
);
247 if (!hash_table
[position
])
248 return (wxObject
*) NULL
;
251 wxNode
*node
= hash_table
[position
]->Find (key
);
254 wxObject
*data
= node
->Data ();
259 return (wxObject
*) NULL
;
263 wxObject
*wxHashTable::Delete (long key
, int value
)
270 int position
= (int) (k
% n
);
271 if (!hash_table
[position
])
272 return (wxObject
*) NULL
;
275 wxNode
*node
= hash_table
[position
]->Find (value
);
278 wxObject
*data
= node
->Data ();
283 return (wxObject
*) NULL
;
287 wxObject
*wxHashTable::Delete (long key
, const wxChar
*value
)
289 int position
= (int) (key
% n
);
290 if (!hash_table
[position
])
291 return (wxObject
*) NULL
;
294 wxNode
*node
= hash_table
[position
]->Find (value
);
297 wxObject
*data
= node
->Data ();
302 return (wxObject
*) NULL
;
306 long wxHashTable::MakeKey (const wxChar
*string
) const
311 int_key
+= (wxUChar
) *string
++;
316 void wxHashTable::BeginFind (void)
318 current_position
= -1;
319 current_node
= (wxNode
*) NULL
;
322 wxNode
*wxHashTable::Next (void)
324 wxNode
*found
= (wxNode
*) NULL
;
326 while (!end
&& !found
)
331 if (current_position
>= n
)
333 current_position
= -1;
334 current_node
= (wxNode
*) NULL
;
339 if (hash_table
[current_position
])
341 current_node
= hash_table
[current_position
]->First ();
342 found
= current_node
;
348 current_node
= current_node
->Next ();
349 found
= current_node
;
355 void wxHashTable::DeleteContents (bool flag
)
358 for (i
= 0; i
< n
; i
++)
361 hash_table
[i
]->DeleteContents (flag
);
365 void wxHashTable::Clear (void)
368 for (i
= 0; i
< n
; i
++)
371 hash_table
[i
]->Clear ();