ObjFW  Diff

Differences From Artifact [6e8102a07c]:

To Artifact [d7091488bd]:

  • File src/OFIterator.m — part of check-in [bbf1f79b8f] at 2009-09-08 16:06:10 on branch trunk — New OFDictionary implementation and removal of a hack in OFList.

    The new implementation is easier to use as it does automatic resizing,
    but therefore it's not realtime-capable anymore. The new implementation
    should also be a little bit faster.

    I decided to change the implementation as only very few need a
    realtime-capable dictionary and those few will most likely write their
    own implementation for their specific case anyway.

    As the new implementation no longer uses OFList, this also made it
    possible to remove a hack from OFList. (user: js, size: 1644) [annotate] [blame] [check-ins using]


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
87
@implementation OFIterator
- init
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- initWithData: (OFList**)data_
	  size: (size_t)size_
{


	self = [super init];

	data = data_;
	size = size_;








	last = NULL;


	return self;
}

- (of_iterator_pair_t)nextKeyObjectPair
{
	of_iterator_pair_t next;

	for (;;) {
		if (last == NULL) {
			for (; pos < size && data[pos] == nil; pos++);
			if (pos == size) {
				next.key = nil;
				next.object = nil;
				next.hash = 0;
				return next;
			}

			last = (of_dictionary_list_object_t*)


			    [data[pos++] first];
			next.key = last->key;
			next.object = last->object;
			next.hash = last->hash;

			return next;
		}


		if ((last = last->next) != NULL) {
			next.key = last->key;
			next.object = last->object;


			next.hash = last->hash;
			return next;
		}
	}

}

- reset
{
	pos = 0;
	last = NULL;

	return self;
}
@end

@implementation OFDictionary (OFIterator)
- (OFIterator*)iterator
{
	return [[[OFIterator alloc] initWithData: data
					    size: size] autorelease];
}
@end







|


>
>


<

>
>

>
>
>
>
>
|
>




|

|

|
<
<
|
|
|
<
<
|
|
|
>
>
|
<
|
<
>
|
|
>

|
|
|
>
>
|
|
|
|
>





<












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
87
88
89
90
91
92
93
94
95
96
@implementation OFIterator
- init
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- initWithData: (struct of_dictionary_bucket*)data_
	  size: (size_t)size_
{
	size_t i;

	self = [super init];


	size = size_;
	data = [self allocMemoryForNItems: size
				 withSize: sizeof(struct of_dictionary_bucket)];

	for (i = 0; i < size; i++) {
		if (data_[i].key != nil) {
			data[i].key = [data_[i].key copy];
			data[i].object = [data_[i].object retain];
		} else
			data[i].key = nil;
	}

	return self;
}

- (void)dealloc
{
	size_t i;

	for (i = 0; i < size; i++) {


		if (data[i].key != nil) {
			[data[i].key release];
			[data[i].object release];


		}
	}

	[super dealloc];
}


- (of_iterator_pair_t)nextKeyObjectPair

{
	of_iterator_pair_t next;

	for (; pos < size && data[pos].key == nil; pos++);

	if (pos < size) {
		next.key = data[pos].key;
		next.object = data[pos].object;
		pos++;
	} else {
		next.key = nil;
		next.object = nil;
	}

	return next;
}

- reset
{
	pos = 0;


	return self;
}
@end

@implementation OFDictionary (OFIterator)
- (OFIterator*)iterator
{
	return [[[OFIterator alloc] initWithData: data
					    size: size] autorelease];
}
@end