]>
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
;
82 void wxHashTable::DoCopy(const wxHashTable
& table
)
85 current_position
= table
.current_position
;
86 current_node
= NULL
; // doesn't matter - Next() will reconstruct it
87 key_type
= table
.key_type
;
89 hash_table
= new wxList
*[n
];
90 for (int i
= 0; i
< n
; i
++) {
91 if (table
.hash_table
[i
] == NULL
)
94 hash_table
[i
] = new wxList(key_type
);
95 *(hash_table
[i
]) = *(table
.hash_table
[i
]);
100 void wxHashTable::Put (long key
, long value
, wxObject
* object
)
107 int position
= (int) (k
% n
);
108 if (!hash_table
[position
])
109 hash_table
[position
] = new wxList (wxKEY_INTEGER
);
111 hash_table
[position
]->Append (value
, object
);
114 void wxHashTable::Put (long key
, const wxChar
*value
, wxObject
* object
)
121 int position
= (int) (k
% n
);
122 if (!hash_table
[position
])
123 hash_table
[position
] = new wxList (wxKEY_INTEGER
);
125 hash_table
[position
]->Append (value
, object
);
128 void wxHashTable::Put (long key
, wxObject
* object
)
135 int position
= (int) (k
% n
);
136 if (!hash_table
[position
])
137 hash_table
[position
] = new wxList (wxKEY_INTEGER
);
139 hash_table
[position
]->Append (k
, object
);
142 void wxHashTable::Put (const wxChar
*key
, wxObject
* object
)
144 int position
= (int) (MakeKey (key
) % n
);
146 if (!hash_table
[position
])
147 hash_table
[position
] = new wxList (wxKEY_STRING
);
149 hash_table
[position
]->Append (key
, object
);
152 wxObject
*wxHashTable::Get (long key
, long value
) const
159 int position
= (int) (k
% n
);
160 if (!hash_table
[position
])
161 return (wxObject
*) NULL
;
164 wxNode
*node
= hash_table
[position
]->Find (value
);
166 return node
->Data ();
168 return (wxObject
*) NULL
;
172 wxObject
*wxHashTable::Get (long key
, const wxChar
*value
) const
179 int position
= (int) (k
% n
);
180 if (!hash_table
[position
])
181 return (wxObject
*) NULL
;
184 wxNode
*node
= hash_table
[position
]->Find (value
);
186 return node
->Data ();
188 return (wxObject
*) NULL
;
192 wxObject
*wxHashTable::Get (long key
) const
199 int position
= (int) (k
% n
);
200 if (!hash_table
[position
])
201 return (wxObject
*) NULL
;
204 wxNode
*node
= hash_table
[position
]->Find (k
);
205 return node
? node
->Data () : (wxObject
*)NULL
;
209 wxObject
*wxHashTable::Get (const wxChar
*key
) const
211 int position
= (int) (MakeKey (key
) % n
);
213 if (!hash_table
[position
])
214 return (wxObject
*) NULL
;
217 wxNode
*node
= hash_table
[position
]->Find (key
);
218 return node
? node
->Data () : (wxObject
*)NULL
;
222 wxObject
*wxHashTable::Delete (long key
)
229 int position
= (int) (k
% n
);
230 if (!hash_table
[position
])
231 return (wxObject
*) NULL
;
234 wxNode
*node
= hash_table
[position
]->Find (k
);
237 wxObject
*data
= node
->Data ();
242 return (wxObject
*) NULL
;
246 wxObject
*wxHashTable::Delete (const wxChar
*key
)
248 int position
= (int) (MakeKey (key
) % n
);
249 if (!hash_table
[position
])
250 return (wxObject
*) NULL
;
253 wxNode
*node
= hash_table
[position
]->Find (key
);
256 wxObject
*data
= node
->Data ();
261 return (wxObject
*) NULL
;
265 wxObject
*wxHashTable::Delete (long key
, int value
)
272 int position
= (int) (k
% 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 wxObject
*wxHashTable::Delete (long key
, const wxChar
*value
)
291 int position
= (int) (key
% n
);
292 if (!hash_table
[position
])
293 return (wxObject
*) NULL
;
296 wxNode
*node
= hash_table
[position
]->Find (value
);
299 wxObject
*data
= node
->Data ();
304 return (wxObject
*) NULL
;
308 long wxHashTable::MakeKey (const wxChar
*string
) const
313 int_key
+= (wxUChar
) *string
++;
318 void wxHashTable::BeginFind (void)
320 current_position
= -1;
321 current_node
= (wxNode
*) NULL
;
324 wxNode
*wxHashTable::Next (void)
326 wxNode
*found
= (wxNode
*) NULL
;
328 while (!end
&& !found
)
333 if (current_position
>= n
)
335 current_position
= -1;
336 current_node
= (wxNode
*) NULL
;
341 if (hash_table
[current_position
])
343 current_node
= hash_table
[current_position
]->First ();
344 found
= current_node
;
350 current_node
= current_node
->Next ();
351 found
= current_node
;
357 void wxHashTable::DeleteContents (bool flag
)
360 for (i
= 0; i
< n
; i
++)
363 hash_table
[i
]->DeleteContents (flag
);
367 void wxHashTable::Clear (void)
370 for (i
= 0; i
< n
; i
++)
373 hash_table
[i
]->Clear ();