forked from tada/pljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIterator.c
More file actions
87 lines (79 loc) · 1.75 KB
/
Copy pathIterator.c
File metadata and controls
87 lines (79 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* Copyright (c) 2004, 2005, 2006 TADA AB - Taby Sweden
* Distributed under the terms shown in the file COPYRIGHT
* found in the root folder of this project or at
* http://eng.tada.se/osprojects/COPYRIGHT.html
*
* @author Thomas Hallgren
*/
#include "pljava/HashMap_priv.h"
#include "pljava/Iterator.h"
struct Iterator_
{
struct PgObject_ PgObject_extension;
HashMap source;
uint32 sourceTableSize;
uint32 currentBucket;
Entry nextEntry;
};
static PgObjectClass s_IteratorClass;
Iterator Iterator_create(HashMap source)
{
Iterator self = (Iterator)PgObjectClass_allocInstance(s_IteratorClass, GetMemoryChunkContext(source));
self->source = source;
self->sourceTableSize = source->tableSize;
self->currentBucket = 0;
self->nextEntry = 0;
return self;
}
static Entry Iterator_peekNext(Iterator self)
{
uint32 tableSize = self->source->tableSize;
if(tableSize != self->sourceTableSize)
{
/* Rehash during Iteration. We can't continue.
*/
self->nextEntry = 0;
}
else if(self->nextEntry == 0)
{
/* Go to next bucket
*/
Entry* table = self->source->table;
while(self->currentBucket < tableSize)
{
Entry nxt = table[self->currentBucket];
if(nxt != 0)
{
self->nextEntry = nxt;
break;
}
self->currentBucket++;
}
}
return self->nextEntry;
}
bool Iterator_hasNext(Iterator self)
{
return Iterator_peekNext(self) != 0;
}
Entry Iterator_next(Iterator self)
{
Entry nxt = Iterator_peekNext(self);
if(nxt != 0)
{
Entry nxtNxt = nxt->next;
if(nxtNxt == 0)
/*
* Leave this bucket.
*/
self->currentBucket++;
self->nextEntry = nxtNxt;
}
return nxt;
}
extern void Iterator_initialize(void);
void Iterator_initialize(void)
{
s_IteratorClass = PgObjectClass_create("Iterator", sizeof(struct Iterator_), 0);
}