]>
git.saurik.com Git - wxWidgets.git/blob - src/common/hash.cpp
4c6ad97c6d6e0bd52f10305f6e5b5195fe96d00e
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
);
42 current_position = -1;
43 current_node = (wxNode *) NULL;
45 key_type = the_key_type;
46 hash_table = new wxList *[size];
48 for (i = 0; i < size; i++)
49 hash_table[i] = (wxList *) NULL;
53 wxHashTable::~wxHashTable (void)
58 void wxHashTable::Destroy(void)
60 if (!hash_table
) return;
62 for (i
= 0; i
< n
; i
++)
69 bool wxHashTable::Create(int the_key_type
, int size
)
74 current_position
= -1;
75 current_node
= (wxNode
*) NULL
;
77 key_type
= the_key_type
;
78 hash_table
= new wxList
*[size
];
80 for (i
= 0; i
< size
; i
++)
81 hash_table
[i
] = (wxList
*) NULL
;
86 void wxHashTable::DoCopy(const wxHashTable
& table
)
89 current_position
= table
.current_position
;
90 current_node
= NULL
; // doesn't matter - Next() will reconstruct it
91 key_type
= table
.key_type
;
93 hash_table
= new wxList
*[n
];
94 for (int i
= 0; i
< n
; i
++) {
95 if (table
.hash_table
[i
] == NULL
)
98 hash_table
[i
] = new wxList(key_type
);
99 *(hash_table
[i
]) = *(table
.hash_table
[i
]);
104 void wxHashTable::Put (long key
, long value
, wxObject
* object
)
111 int position
= (int) (k
% n
);
112 if (!hash_table
[position
])
113 hash_table
[position
] = new wxList (wxKEY_INTEGER
);
115 hash_table
[position
]->Append (value
, object
);
119 void wxHashTable::Put (long key
, const wxChar
*value
, wxObject
* object
)
126 int position
= (int) (k
% n
);
127 if (!hash_table
[position
])
128 hash_table
[position
] = new wxList (wxKEY_INTEGER
);
130 hash_table
[position
]->Append (value
, object
);
134 void wxHashTable::Put (long key
, wxObject
* object
)
141 int position
= (int) (k
% n
);
142 if (!hash_table
[position
])
143 hash_table
[position
] = new wxList (wxKEY_INTEGER
);
145 hash_table
[position
]->Append (k
, object
);
149 void wxHashTable::Put (const wxChar
*key
, wxObject
* object
)
151 int position
= (int) (MakeKey (key
) % n
);
153 if (!hash_table
[position
])
154 hash_table
[position
] = new wxList (wxKEY_STRING
);
156 hash_table
[position
]->Append (key
, object
);
160 wxObject
*wxHashTable::Get (long key
, long value
) const
167 int position
= (int) (k
% n
);
168 if (!hash_table
[position
])
169 return (wxObject
*) NULL
;
172 wxNode
*node
= hash_table
[position
]->Find (value
);
174 return node
->Data ();
176 return (wxObject
*) NULL
;
180 wxObject
*wxHashTable::Get (long key
, const wxChar
*value
) const
187 int position
= (int) (k
% n
);
188 if (!hash_table
[position
])
189 return (wxObject
*) NULL
;
192 wxNode
*node
= hash_table
[position
]->Find (value
);
194 return node
->Data ();
196 return (wxObject
*) NULL
;
200 wxObject
*wxHashTable::Get (long key
) const
207 int position
= (int) (k
% n
);
208 if (!hash_table
[position
])
209 return (wxObject
*) NULL
;
212 wxNode
*node
= hash_table
[position
]->Find (k
);
213 return node
? node
->Data () : (wxObject
*)NULL
;
217 wxObject
*wxHashTable::Get (const wxChar
*key
) const
219 int position
= (int) (MakeKey (key
) % n
);
221 if (!hash_table
[position
])
222 return (wxObject
*) NULL
;
225 wxNode
*node
= hash_table
[position
]->Find (key
);
226 return node
? node
->Data () : (wxObject
*)NULL
;
230 wxObject
*wxHashTable::Delete (long key
)
237 int position
= (int) (k
% n
);
238 if (!hash_table
[position
])
239 return (wxObject
*) NULL
;
242 wxNode
*node
= hash_table
[position
]->Find (k
);
245 wxObject
*data
= node
->Data ();
251 return (wxObject
*) NULL
;
255 wxObject
*wxHashTable::Delete (const wxChar
*key
)
257 int position
= (int) (MakeKey (key
) % n
);
258 if (!hash_table
[position
])
259 return (wxObject
*) NULL
;
262 wxNode
*node
= hash_table
[position
]->Find (key
);
265 wxObject
*data
= node
->Data ();
271 return (wxObject
*) NULL
;
275 wxObject
*wxHashTable::Delete (long key
, int value
)
282 int position
= (int) (k
% n
);
283 if (!hash_table
[position
])
284 return (wxObject
*) NULL
;
287 wxNode
*node
= hash_table
[position
]->Find (value
);
290 wxObject
*data
= node
->Data ();
296 return (wxObject
*) NULL
;
300 wxObject
*wxHashTable::Delete (long key
, const wxChar
*value
)
302 int position
= (int) (key
% n
);
303 if (!hash_table
[position
])
304 return (wxObject
*) NULL
;
307 wxNode
*node
= hash_table
[position
]->Find (value
);
310 wxObject
*data
= node
->Data ();
316 return (wxObject
*) NULL
;
320 long wxHashTable::MakeKey (const wxChar
*string
) const
325 int_key
+= (wxUChar
) *string
++;
330 void wxHashTable::BeginFind (void)
332 current_position
= -1;
333 current_node
= (wxNode
*) NULL
;
336 wxNode
*wxHashTable::Next (void)
338 wxNode
*found
= (wxNode
*) NULL
;
340 while (!end
&& !found
)
345 if (current_position
>= n
)
347 current_position
= -1;
348 current_node
= (wxNode
*) NULL
;
353 if (hash_table
[current_position
])
355 current_node
= hash_table
[current_position
]->First ();
356 found
= current_node
;
362 current_node
= current_node
->Next ();
363 found
= current_node
;
369 void wxHashTable::DeleteContents (bool flag
)
372 for (i
= 0; i
< n
; i
++)
375 hash_table
[i
]->DeleteContents (flag
);
379 void wxHashTable::Clear (void)
382 for (i
= 0; i
< n
; i
++)
385 hash_table
[i
]->Clear ();