ObjFW  Artifact [d7091488bd]

Artifact d7091488bd0613e4b671bd5a502daae78c3a2d3f4777e75595c8e1da2e825cee:

  • 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]


/*
 * Copyright (c) 2008 - 2009
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#include "config.h"

#import "OFIterator.h"
#import "OFDictionary.h"
#import "OFExceptions.h"

/* Reference for static linking */
int _OFIterator_reference;

@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