]>
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 #if !USE_SHARED_LIBRARY
33 IMPLEMENT_DYNAMIC_CLASS(wxHashTable
, wxObject
)
36 wxHashTable::wxHashTable (int the_key_type
, int size
)
39 current_position
= -1;
40 current_node
= (wxNode
*) NULL
;
42 key_type
= the_key_type
;
43 hash_table
= new wxList
*[size
];
45 for (i
= 0; i
< size
; i
++)
46 hash_table
[i
] = (wxList
*) NULL
;
49 wxHashTable::~wxHashTable (void)
52 for (i
= 0; i
< n
; i
++)
58 bool wxHashTable::Create(int the_key_type
, int size
)
61 current_position
= -1;
62 current_node
= (wxNode
*) NULL
;
64 key_type
= the_key_type
;
67 hash_table
= new wxList
*[size
];
69 for (i
= 0; i
< size
; i
++)
70 hash_table
[i
] = (wxList
*) NULL
;
74 void wxHashTable::Put (long key
, long value
, wxObject
* object
)
81 int position
= (int) (k
% n
);
82 if (!hash_table
[position
])
83 hash_table
[position
] = new wxList (wxKEY_INTEGER
);
85 hash_table
[position
]->Append (value
, object
);
88 void wxHashTable::Put (long key
, const char *value
, wxObject
* object
)
95 int position
= (int) (k
% n
);
96 if (!hash_table
[position
])
97 hash_table
[position
] = new wxList (wxKEY_INTEGER
);
99 hash_table
[position
]->Append (value
, object
);
102 void wxHashTable::Put (long key
, wxObject
* object
)
109 int position
= (int) (k
% n
);
110 if (!hash_table
[position
])
111 hash_table
[position
] = new wxList (wxKEY_INTEGER
);
113 hash_table
[position
]->Append (k
, object
);
116 void wxHashTable::Put (const char *key
, wxObject
* object
)
118 int position
= (int) (MakeKey (key
) % n
);
120 if (!hash_table
[position
])
121 hash_table
[position
] = new wxList (wxKEY_STRING
);
123 hash_table
[position
]->Append (key
, object
);
126 wxObject
*wxHashTable::Get (long key
, long value
) const
133 int position
= (int) (k
% n
);
134 if (!hash_table
[position
])
135 return (wxObject
*) NULL
;
138 wxNode
*node
= hash_table
[position
]->Find (value
);
140 return node
->Data ();
142 return (wxObject
*) NULL
;
146 wxObject
*wxHashTable::Get (long key
, const char *value
) const
153 int position
= (int) (k
% n
);
154 if (!hash_table
[position
])
155 return (wxObject
*) NULL
;
158 wxNode
*node
= hash_table
[position
]->Find (value
);
160 return node
->Data ();
162 return (wxObject
*) NULL
;
166 wxObject
*wxHashTable::Get (long key
) const
173 int position
= (int) (k
% n
);
174 if (!hash_table
[position
])
175 return (wxObject
*) NULL
;
178 wxNode
*node
= hash_table
[position
]->Find (k
);
179 return node
? node
->Data () : (wxObject
*)NULL
;
183 wxObject
*wxHashTable::Get (const char *key
) const
185 int position
= (int) (MakeKey (key
) % n
);
187 if (!hash_table
[position
])
188 return (wxObject
*) NULL
;
191 wxNode
*node
= hash_table
[position
]->Find (key
);
192 return node
? node
->Data () : (wxObject
*)NULL
;
196 wxObject
*wxHashTable::Delete (long key
)
203 int position
= (int) (k
% n
);
204 if (!hash_table
[position
])
205 return (wxObject
*) NULL
;
208 wxNode
*node
= hash_table
[position
]->Find (k
);
211 wxObject
*data
= node
->Data ();
216 return (wxObject
*) NULL
;
220 wxObject
*wxHashTable::Delete (const char *key
)
222 int position
= (int) (MakeKey (key
) % n
);
223 if (!hash_table
[position
])
224 return (wxObject
*) NULL
;
227 wxNode
*node
= hash_table
[position
]->Find (key
);
230 wxObject
*data
= node
->Data ();
235 return (wxObject
*) NULL
;
239 wxObject
*wxHashTable::Delete (long key
, int value
)
246 int position
= (int) (k
% n
);
247 if (!hash_table
[position
])
248 return (wxObject
*) NULL
;
251 wxNode
*node
= hash_table
[position
]->Find (value
);
254 wxObject
*data
= node
->Data ();
259 return (wxObject
*) NULL
;
263 wxObject
*wxHashTable::Delete (long key
, const char *value
)
265 int position
= (int) (key
% n
);
266 if (!hash_table
[position
])
267 return (wxObject
*) NULL
;
270 wxNode
*node
= hash_table
[position
]->Find (value
);
273 wxObject
*data
= node
->Data ();
278 return (wxObject
*) NULL
;
282 long wxHashTable::MakeKey (const char *string
) const
287 int_key
+= (unsigned char) *string
++;
292 void wxHashTable::BeginFind (void)
294 current_position
= -1;
295 current_node
= (wxNode
*) NULL
;
298 wxNode
*wxHashTable::Next (void)
300 wxNode
*found
= (wxNode
*) NULL
;
302 while (!end
&& !found
)
307 if (current_position
>= n
)
309 current_position
= -1;
310 current_node
= (wxNode
*) NULL
;
315 if (hash_table
[current_position
])
317 current_node
= hash_table
[current_position
]->First ();
318 found
= current_node
;
324 current_node
= current_node
->Next ();
325 found
= current_node
;
331 void wxHashTable::DeleteContents (bool flag
)
334 for (i
= 0; i
< n
; i
++)
337 hash_table
[i
]->DeleteContents (flag
);
341 void wxHashTable::Clear (void)
344 for (i
= 0; i
< n
; i
++)
347 hash_table
[i
]->Clear ();