44 *
55 * Authors: Dennis Smit <[email protected] > 66 *
7- * $Id: lv_hashmap.c,v 1.10 2006-02-05 18:45:57 synap Exp $
7+ * $Id: lv_hashmap.c,v 1.11 2006-02-17 22:00:17 synap Exp $
88 *
99 * This program is free software; you can redistribute it and/or modify
1010 * it under the terms of the GNU Lesser General Public License as
@@ -38,6 +38,9 @@ struct _HashmapIterContext {
3838 VisObject * object ;
3939
4040 int index ;
41+ int retrieved ;
42+ int first ;
43+
4144 VisListEntry * le ;
4245};
4346
@@ -130,6 +133,8 @@ static VisCollectionIter *hashmap_iter (VisCollection *collection)
130133 visual_object_initialize (VISUAL_OBJECT (context ), TRUE, NULL );
131134 context -> index = 0 ;
132135 context -> le = NULL ;
136+ context -> retrieved = FALSE;
137+ context -> first = TRUE;
133138
134139 iter = visual_collection_iter_new (hashmap_iter_assign , hashmap_iter_next , hashmap_iter_has_more ,
135140 hashmap_iter_get_data , collection , VISUAL_OBJECT (context ));
@@ -139,46 +144,91 @@ static VisCollectionIter *hashmap_iter (VisCollection *collection)
139144
140145static void hashmap_iter_assign (VisCollectionIter * iter , VisCollection * collection , VisObject * itercontext , int index )
141146{
147+ VisHashmap * hashmap = VISUAL_HASHMAP (collection );
148+ int i ;
149+
150+ if (index >= hashmap -> tablesize )
151+ return ;
142152
153+ for (i = 0 ; i < index ; i ++ ) {
154+ hashmap_iter_next (iter , collection , itercontext );
155+ }
143156}
144157
145158static int hashmap_iter_has_more (VisCollectionIter * iter , VisCollection * collection , VisObject * itercontext )
146159{
160+ VisHashmap * hashmap = VISUAL_HASHMAP (collection );
161+ HashmapIterContext * context = HASHMAP_ITERCONTEXT (itercontext );
162+
163+ if (context -> index >= hashmap -> tablesize )
164+ return FALSE;
165+
166+ /* First entry */
167+ if (context -> first ) {
168+ context -> first = FALSE;
169+
170+ for (; context -> index < hashmap -> tablesize ; context -> index ++ ) {
171+ if (visual_collection_size (VISUAL_COLLECTION (& hashmap -> table [context -> index ].list )) > 0 ) {
172+ context -> le = hashmap -> table [context -> index ].list .head ;
173+ context -> retrieved = FALSE;
174+
175+ return TRUE;
176+ }
177+ }
178+ }
179+
180+ /* Check for next chain entry */
181+ if (context -> le != NULL && context -> le -> next != NULL ) {
182+ context -> le = context -> le -> next ;
183+
184+ return TRUE;
185+ }
186+
187+ /* No next in chain, next array entry */
188+ context -> index ++ ;
189+ for (; context -> index < hashmap -> tablesize ; context -> index ++ ) {
190+ if (visual_collection_size (VISUAL_COLLECTION (& hashmap -> table [context -> index ].list )) > 0 ) {
191+ context -> le = hashmap -> table [context -> index ].list .head ;
192+ context -> retrieved = FALSE;
193+
194+ return TRUE;
195+ }
196+ }
147197
198+ return FALSE;
148199}
149200
150201static void hashmap_iter_next (VisCollectionIter * iter , VisCollection * collection , VisObject * itercontext )
151202{
152203 VisHashmap * hashmap = VISUAL_HASHMAP (collection );
153204 HashmapIterContext * context = HASHMAP_ITERCONTEXT (itercontext );
154- int i ;
155205
156- if (context -> index >= hashmap -> size )
157- return ;
206+ if (context -> retrieved == FALSE) {
207+ if (context -> first == TRUE) {
208+ hashmap_iter_has_more (iter , collection , itercontext );
209+
210+ context -> first = FALSE;
211+ }
158212
159- /* FIXME initial start case doesn't work */
160- if (context -> le -> next != NULL ) {
161- visual_list_next (& hashmap -> table [i ].list , & context -> le );
213+ context -> retrieved = TRUE;
162214
163215 return ;
164216 }
165217
166- /* Find the next valid chain */
167- for (i = context -> index ; i < hashmap -> size ; i ++ ) {
168- VisHashmapEntry * mentry ;
169-
170- mentry = & hashmap -> table [i ];
171-
172-
173-
174- }
218+ hashmap_iter_has_more (iter , collection , itercontext );
219+ context -> retrieved = TRUE;
175220
176221 return ;
177222}
178223
179224static void * hashmap_iter_get_data (VisCollectionIter * iter , VisCollection * collection , VisObject * itercontext )
180225{
226+ HashmapIterContext * context = HASHMAP_ITERCONTEXT (itercontext );
227+ VisHashmapChainEntry * mentry ;
228+
229+ mentry = context -> le -> data ;
181230
231+ return mentry -> data ;
182232}
183233
184234
0 commit comments