]>
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;
42 key_type
= the_key_type
;
43 hash_table
= new wxList
*[size
];
45 for (i
= 0; i
< size
; i
++)
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;
64 key_type
= the_key_type
;
67 hash_table
= new wxList
*[size
];
69 for (i
= 0; i
< size
; i
++)
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
])
138 wxNode
*node
= hash_table
[position
]->Find (value
);
140 return node
->Data ();
146 wxObject
*wxHashTable::Get (long key
, const char *value
) const
153 int position
= (int) (k
% n
);
154 if (!hash_table
[position
])
158 wxNode
*node
= hash_table
[position
]->Find (value
);
160 return node
->Data ();
166 wxObject
*wxHashTable::Get (long key
) const
173 int position
= (int) (k
% n
);
174 if (!hash_table
[position
])
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
])
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
])
208 wxNode
*node
= hash_table
[position
]->Find (k
);
211 wxObject
*data
= node
->Data ();
220 wxObject
*wxHashTable::Delete (const char *key
)
222 int position
= (int) (MakeKey (key
) % n
);
223 if (!hash_table
[position
])
227 wxNode
*node
= hash_table
[position
]->Find (key
);
230 wxObject
*data
= node
->Data ();
239 wxObject
*wxHashTable::Delete (long key
, int value
)
246 int position
= (int) (k
% n
);
247 if (!hash_table
[position
])
251 wxNode
*node
= hash_table
[position
]->Find (value
);
254 wxObject
*data
= node
->Data ();
263 wxObject
*wxHashTable::Delete (long key
, const char *value
)
265 int position
= (int) (key
% n
);
266 if (!hash_table
[position
])
270 wxNode
*node
= hash_table
[position
]->Find (value
);
273 wxObject
*data
= node
->Data ();
282 long wxHashTable::MakeKey (const char *string
) const
287 int_key
+= (unsigned char) *string
++;
292 void wxHashTable::BeginFind (void)
294 current_position
= -1;
298 wxNode
*wxHashTable::Next (void)
300 wxNode
*found
= NULL
;
302 while (!end
&& !found
)
307 if (current_position
>= n
)
309 current_position
= -1;
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 ();