]>
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)
54 void wxHashTable::Destroy(void)
56 if (!hash_table
) return;
58 for (i
= 0; i
< n
; i
++)
65 bool wxHashTable::Create(int the_key_type
, int size
)
68 current_position
= -1;
69 current_node
= (wxNode
*) NULL
;
71 key_type
= the_key_type
;
74 hash_table
= new wxList
*[size
];
76 for (i
= 0; i
< size
; i
++)
77 hash_table
[i
] = (wxList
*) NULL
;
81 void wxHashTable::Put (long key
, long value
, wxObject
* object
)
88 int position
= (int) (k
% n
);
89 if (!hash_table
[position
])
90 hash_table
[position
] = new wxList (wxKEY_INTEGER
);
92 hash_table
[position
]->Append (value
, object
);
95 void wxHashTable::Put (long key
, const char *value
, wxObject
* object
)
102 int position
= (int) (k
% n
);
103 if (!hash_table
[position
])
104 hash_table
[position
] = new wxList (wxKEY_INTEGER
);
106 hash_table
[position
]->Append (value
, object
);
109 void wxHashTable::Put (long key
, wxObject
* object
)
116 int position
= (int) (k
% n
);
117 if (!hash_table
[position
])
118 hash_table
[position
] = new wxList (wxKEY_INTEGER
);
120 hash_table
[position
]->Append (k
, object
);
123 void wxHashTable::Put (const char *key
, wxObject
* object
)
125 int position
= (int) (MakeKey (key
) % n
);
127 if (!hash_table
[position
])
128 hash_table
[position
] = new wxList (wxKEY_STRING
);
130 hash_table
[position
]->Append (key
, object
);
133 wxObject
*wxHashTable::Get (long key
, long value
) const
140 int position
= (int) (k
% n
);
141 if (!hash_table
[position
])
142 return (wxObject
*) NULL
;
145 wxNode
*node
= hash_table
[position
]->Find (value
);
147 return node
->Data ();
149 return (wxObject
*) NULL
;
153 wxObject
*wxHashTable::Get (long key
, const char *value
) const
160 int position
= (int) (k
% n
);
161 if (!hash_table
[position
])
162 return (wxObject
*) NULL
;
165 wxNode
*node
= hash_table
[position
]->Find (value
);
167 return node
->Data ();
169 return (wxObject
*) NULL
;
173 wxObject
*wxHashTable::Get (long key
) const
180 int position
= (int) (k
% n
);
181 if (!hash_table
[position
])
182 return (wxObject
*) NULL
;
185 wxNode
*node
= hash_table
[position
]->Find (k
);
186 return node
? node
->Data () : (wxObject
*)NULL
;
190 wxObject
*wxHashTable::Get (const char *key
) const
192 int position
= (int) (MakeKey (key
) % n
);
194 if (!hash_table
[position
])
195 return (wxObject
*) NULL
;
198 wxNode
*node
= hash_table
[position
]->Find (key
);
199 return node
? node
->Data () : (wxObject
*)NULL
;
203 wxObject
*wxHashTable::Delete (long key
)
210 int position
= (int) (k
% n
);
211 if (!hash_table
[position
])
212 return (wxObject
*) NULL
;
215 wxNode
*node
= hash_table
[position
]->Find (k
);
218 wxObject
*data
= node
->Data ();
223 return (wxObject
*) NULL
;
227 wxObject
*wxHashTable::Delete (const char *key
)
229 int position
= (int) (MakeKey (key
) % n
);
230 if (!hash_table
[position
])
231 return (wxObject
*) NULL
;
234 wxNode
*node
= hash_table
[position
]->Find (key
);
237 wxObject
*data
= node
->Data ();
242 return (wxObject
*) NULL
;
246 wxObject
*wxHashTable::Delete (long key
, int value
)
253 int position
= (int) (k
% n
);
254 if (!hash_table
[position
])
255 return (wxObject
*) NULL
;
258 wxNode
*node
= hash_table
[position
]->Find (value
);
261 wxObject
*data
= node
->Data ();
266 return (wxObject
*) NULL
;
270 wxObject
*wxHashTable::Delete (long key
, const char *value
)
272 int position
= (int) (key
% n
);
273 if (!hash_table
[position
])
274 return (wxObject
*) NULL
;
277 wxNode
*node
= hash_table
[position
]->Find (value
);
280 wxObject
*data
= node
->Data ();
285 return (wxObject
*) NULL
;
289 long wxHashTable::MakeKey (const char *string
) const
294 int_key
+= (unsigned char) *string
++;
299 void wxHashTable::BeginFind (void)
301 current_position
= -1;
302 current_node
= (wxNode
*) NULL
;
305 wxNode
*wxHashTable::Next (void)
307 wxNode
*found
= (wxNode
*) NULL
;
309 while (!end
&& !found
)
314 if (current_position
>= n
)
316 current_position
= -1;
317 current_node
= (wxNode
*) NULL
;
322 if (hash_table
[current_position
])
324 current_node
= hash_table
[current_position
]->First ();
325 found
= current_node
;
331 current_node
= current_node
->Next ();
332 found
= current_node
;
338 void wxHashTable::DeleteContents (bool flag
)
341 for (i
= 0; i
< n
; i
++)
344 hash_table
[i
]->DeleteContents (flag
);
348 void wxHashTable::Clear (void)
351 for (i
= 0; i
< n
; i
++)
354 hash_table
[i
]->Clear ();