Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -7,13 +7,13 @@ LIB_MINOR = ${OBJFW_LIB_MINOR} STATIC_LIB = ${OBJFW_STATIC_LIB} SRCS = OFApplication.m \ OFArray.m \ + OFArray_adjacent.m \ OFAutoreleasePool.m \ OFBlock.m \ - OFCArray.m \ OFConstantString.m \ OFCountedSet.m \ OFDataArray.m \ OFDataArray+Hashing.m \ OFDate.m \ @@ -28,11 +28,11 @@ OFHTTPRequest.m \ OFIntrospection.m \ OFList.m \ OFMD5Hash.m \ OFMutableArray.m \ - OFMutableCArray.m \ + OFMutableArray_adjacent.m \ OFMutableDictionary.m \ OFMutableSet.m \ OFMutableString.m \ OFNull.m \ OFNumber.m \ Index: src/OFArray.m ================================================================== --- src/OFArray.m +++ src/OFArray.m @@ -17,11 +17,11 @@ #include "config.h" #include #import "OFArray.h" -#import "OFCArray.h" +#import "OFArray_adjacent.h" #import "OFString.h" #import "OFXMLElement.h" #import "OFAutoreleasePool.h" #import "OFEnumerationMutationException.h" @@ -36,58 +36,58 @@ } placeholder; @implementation OFArrayPlaceholder - init { - return (id)[[OFCArray alloc] init]; + return (id)[[OFArray_adjacent alloc] init]; } - initWithObject: (id)object { - return (id)[[OFCArray alloc] initWithObject: object]; + return (id)[[OFArray_adjacent alloc] initWithObject: object]; } - initWithObjects: (id)firstObject, ... { id ret; va_list arguments; va_start(arguments, firstObject); - ret = [[OFCArray alloc] initWithObject: firstObject - arguments: arguments]; + ret = [[OFArray_adjacent alloc] initWithObject: firstObject + arguments: arguments]; va_end(arguments); return ret; } - initWithObject: (id)firstObject arguments: (va_list)arguments { - return (id)[[OFCArray alloc] initWithObject: firstObject - arguments: arguments]; + return (id)[[OFArray_adjacent alloc] initWithObject: firstObject + arguments: arguments]; } - initWithArray: (OFArray*)array { - return (id)[[OFCArray alloc] initWithArray: array]; + return (id)[[OFArray_adjacent alloc] initWithArray: array]; } - initWithCArray: (id*)objects { - return (id)[[OFCArray alloc] initWithCArray: objects]; + return (id)[[OFArray_adjacent alloc] initWithCArray: objects]; } - initWithCArray: (id*)objects length: (size_t)length { - return (id)[[OFCArray alloc] initWithCArray: objects - length: length]; + return (id)[[OFArray_adjacent alloc] initWithCArray: objects + length: length]; } - initWithSerialization: (OFXMLElement*)element { - return (id)[[OFCArray alloc] initWithSerialization: element]; + return (id)[[OFArray_adjacent alloc] initWithSerialization: element]; } - retain { return self; ADDED src/OFArray_adjacent.h Index: src/OFArray_adjacent.h ================================================================== --- src/OFArray_adjacent.h +++ src/OFArray_adjacent.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#import "OFArray.h" + +@class OFDataArray; + +/** + * \brief A class for storing objects in an array. + */ +@interface OFArray_adjacent: OFArray +{ + OFDataArray *array; +} +@end ADDED src/OFArray_adjacent.m Index: src/OFArray_adjacent.m ================================================================== --- src/OFArray_adjacent.m +++ src/OFArray_adjacent.m @@ -0,0 +1,356 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#include "config.h" + +#include + +#import "OFArray_adjacent.h" +#import "OFMutableArray_adjacent.h" +#import "OFDataArray.h" +#import "OFString.h" +#import "OFXMLElement.h" +#import "OFAutoreleasePool.h" + +#import "OFEnumerationMutationException.h" +#import "OFInvalidArgumentException.h" +#import "OFOutOfRangeException.h" + +#import "macros.h" + +@implementation OFArray_adjacent +- init +{ + self = [super init]; + + @try { + array = [[OFDataArray alloc] initWithItemSize: sizeof(id)]; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- initWithObject: (id)object +{ + self = [self init]; + + @try { + [array addItem: &object]; + [object retain]; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- initWithObject: (id)firstObject + arguments: (va_list)arguments +{ + self = [self init]; + + @try { + id object; + + [array addItem: &firstObject]; + [firstObject retain]; + + while ((object = va_arg(arguments, id)) != nil) { + [array addItem: &object]; + [object retain]; + } + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- initWithArray: (OFArray*)array_ +{ + id *cArray; + size_t i, count; + + self = [self init]; + + @try { + cArray = [array_ cArray]; + count = [array_ count]; + } @catch (id e) { + [self release]; + @throw e; + } + + @try { + for (i = 0; i < count; i++) + [cArray[i] retain]; + + [array addNItems: count + fromCArray: cArray]; + } @catch (id e) { + for (i = 0; i < count; i++) + [cArray[i] release]; + + /* Prevent double-release of objects */ + [array release]; + array = nil; + + [self release]; + @throw e; + } + + return self; +} + +- initWithCArray: (id*)objects +{ + self = [self init]; + + @try { + id *object; + size_t count = 0; + + for (object = objects; *object != nil; object++) { + [*object retain]; + count++; + } + + [array addNItems: count + fromCArray: objects]; + } @catch (id e) { + id *object; + + for (object = objects; *object != nil; object++) + [*object release]; + + [self release]; + @throw e; + } + + return self; +} + +- initWithCArray: (id*)objects + length: (size_t)length +{ + self = [self init]; + + @try { + size_t i; + + for (i = 0; i < length; i++) + [objects[i] retain]; + + [array addNItems: length + fromCArray: objects]; + } @catch (id e) { + size_t i; + + for (i = 0; i < length; i++) + [objects[i] release]; + + [self release]; + @throw e; + } + + return self; +} + +- initWithSerialization: (OFXMLElement*)element +{ + self = [self init]; + + @try { + OFAutoreleasePool *pool, *pool2; + OFEnumerator *enumerator; + OFXMLElement *child; + + pool = [[OFAutoreleasePool alloc] init]; + + if ((![[element name] isEqual: @"OFArray"] && + ![[element name] isEqual: @"OFMutableArray"]) || + ![[element namespace] isEqual: OF_SERIALIZATION_NS]) + @throw [OFInvalidArgumentException newWithClass: isa + selector: _cmd]; + + enumerator = [[element children] objectEnumerator]; + pool2 = [[OFAutoreleasePool alloc] init]; + + while ((child = [enumerator nextObject]) != nil) { + id object; + + if (![[child namespace] isEqual: OF_SERIALIZATION_NS]) + continue; + + object = [child objectByDeserializing]; + [array addItem: &object]; + [object retain]; + + [pool2 releaseObjects]; + } + + [pool release]; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- (size_t)count +{ + return [array count]; +} + +- (id*)cArray +{ + return [array cArray]; +} + +- (id)objectAtIndex: (size_t)index +{ + return *((id*)[array itemAtIndex: index]); +} + +- (void)getObjects: (id*)buffer + inRange: (of_range_t)range +{ + id *cArray = [array cArray]; + size_t i, count = [array count]; + + if (range.start + range.length > count) + @throw [OFOutOfRangeException newWithClass: isa]; + + for (i = 0; i < range.length; i++) + buffer[i] = cArray[range.start + i]; +} + +- (size_t)indexOfObject: (id)object +{ + id *cArray = [array cArray]; + size_t i, count = [array count]; + + for (i = 0; i < count; i++) + if ([cArray[i] isEqual: object]) + return i; + + return OF_INVALID_INDEX; +} + +- (size_t)indexOfObjectIdenticalTo: (id)object +{ + id *cArray = [array cArray]; + size_t i, count = [array count]; + + for (i = 0; i < count; i++) + if (cArray[i] == object) + return i; + + return OF_INVALID_INDEX; +} + + +- (OFArray*)objectsInRange: (of_range_t)range +{ + size_t count = [array count]; + + if (range.start + range.length > count) + @throw [OFOutOfRangeException newWithClass: isa]; + + return [OFArray arrayWithCArray: (id*)[array cArray] + range.start + length: range.length]; +} + +- (BOOL)isEqual: (id)object +{ + OFArray *otherArray; + id *cArray, *otherCArray; + size_t i, count; + + if ([object class] != [OFArray_adjacent class] && + [object class] != [OFMutableArray_adjacent class]) + return [super isEqual: object]; + + otherArray = object; + + count = [array count]; + + if (count != [otherArray count]) + return NO; + + cArray = [array cArray]; + otherCArray = [otherArray cArray]; + + for (i = 0; i < count; i++) + if (![cArray[i] isEqual: otherCArray[i]]) + return NO; + + return YES; +} + +- (uint32_t)hash +{ + id *cArray = [array cArray]; + size_t i, count = [array count]; + uint32_t hash; + + OF_HASH_INIT(hash); + + for (i = 0; i < count; i++) { + uint32_t h = [cArray[i] hash]; + + OF_HASH_ADD(hash, h >> 24); + OF_HASH_ADD(hash, (h >> 16) & 0xFF); + OF_HASH_ADD(hash, (h >> 8) & 0xFF); + OF_HASH_ADD(hash, h & 0xFF); + } + + OF_HASH_FINALIZE(hash); + + return hash; +} + +#ifdef OF_HAVE_BLOCKS +- (void)enumerateObjectsUsingBlock: (of_array_enumeration_block_t)block +{ + id *cArray = [array cArray]; + size_t i, count = [array count]; + BOOL stop = NO; + + for (i = 0; i < count && !stop; i++) + block(cArray[i], i, &stop); +} +#endif + +- (void)dealloc +{ + id *cArray = [array cArray]; + size_t i, count = [array count]; + + for (i = 0; i < count; i++) + [cArray[i] release]; + + [array release]; + + [super dealloc]; +} +@end DELETED src/OFCArray.h Index: src/OFCArray.h ================================================================== --- src/OFCArray.h +++ src/OFCArray.h @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2008, 2009, 2010, 2011 - * Jonathan Schleifer - * - * All rights reserved. - * - * This file is part of ObjFW. It may be distributed under the terms of the - * Q Public License 1.0, which can be found in the file LICENSE.QPL included in - * the packaging of this file. - * - * Alternatively, it may be distributed under the terms of the GNU General - * Public License, either version 2 or 3, which can be found in the file - * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this - * file. - */ - -#import "OFArray.h" - -@class OFDataArray; - -/** - * \brief A class for storing objects in an array. - */ -@interface OFCArray: OFArray -{ - OFDataArray *array; -} -@end DELETED src/OFCArray.m Index: src/OFCArray.m ================================================================== --- src/OFCArray.m +++ src/OFCArray.m @@ -1,356 +0,0 @@ -/* - * Copyright (c) 2008, 2009, 2010, 2011 - * Jonathan Schleifer - * - * All rights reserved. - * - * This file is part of ObjFW. It may be distributed under the terms of the - * Q Public License 1.0, which can be found in the file LICENSE.QPL included in - * the packaging of this file. - * - * Alternatively, it may be distributed under the terms of the GNU General - * Public License, either version 2 or 3, which can be found in the file - * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this - * file. - */ - -#include "config.h" - -#include - -#import "OFCArray.h" -#import "OFMutableCArray.h" -#import "OFDataArray.h" -#import "OFString.h" -#import "OFXMLElement.h" -#import "OFAutoreleasePool.h" - -#import "OFEnumerationMutationException.h" -#import "OFInvalidArgumentException.h" -#import "OFOutOfRangeException.h" - -#import "macros.h" - -@implementation OFCArray -- init -{ - self = [super init]; - - @try { - array = [[OFDataArray alloc] initWithItemSize: sizeof(id)]; - } @catch (id e) { - [self release]; - @throw e; - } - - return self; -} - -- initWithObject: (id)object -{ - self = [self init]; - - @try { - [array addItem: &object]; - [object retain]; - } @catch (id e) { - [self release]; - @throw e; - } - - return self; -} - -- initWithObject: (id)firstObject - arguments: (va_list)arguments -{ - self = [self init]; - - @try { - id object; - - [array addItem: &firstObject]; - [firstObject retain]; - - while ((object = va_arg(arguments, id)) != nil) { - [array addItem: &object]; - [object retain]; - } - } @catch (id e) { - [self release]; - @throw e; - } - - return self; -} - -- initWithArray: (OFArray*)array_ -{ - id *cArray; - size_t i, count; - - self = [self init]; - - @try { - cArray = [array_ cArray]; - count = [array_ count]; - } @catch (id e) { - [self release]; - @throw e; - } - - @try { - for (i = 0; i < count; i++) - [cArray[i] retain]; - - [array addNItems: count - fromCArray: cArray]; - } @catch (id e) { - for (i = 0; i < count; i++) - [cArray[i] release]; - - /* Prevent double-release of objects */ - [array release]; - array = nil; - - [self release]; - @throw e; - } - - return self; -} - -- initWithCArray: (id*)objects -{ - self = [self init]; - - @try { - id *object; - size_t count = 0; - - for (object = objects; *object != nil; object++) { - [*object retain]; - count++; - } - - [array addNItems: count - fromCArray: objects]; - } @catch (id e) { - id *object; - - for (object = objects; *object != nil; object++) - [*object release]; - - [self release]; - @throw e; - } - - return self; -} - -- initWithCArray: (id*)objects - length: (size_t)length -{ - self = [self init]; - - @try { - size_t i; - - for (i = 0; i < length; i++) - [objects[i] retain]; - - [array addNItems: length - fromCArray: objects]; - } @catch (id e) { - size_t i; - - for (i = 0; i < length; i++) - [objects[i] release]; - - [self release]; - @throw e; - } - - return self; -} - -- initWithSerialization: (OFXMLElement*)element -{ - self = [self init]; - - @try { - OFAutoreleasePool *pool, *pool2; - OFEnumerator *enumerator; - OFXMLElement *child; - - pool = [[OFAutoreleasePool alloc] init]; - - if ((![[element name] isEqual: @"OFArray"] && - ![[element name] isEqual: @"OFMutableArray"]) || - ![[element namespace] isEqual: OF_SERIALIZATION_NS]) - @throw [OFInvalidArgumentException newWithClass: isa - selector: _cmd]; - - enumerator = [[element children] objectEnumerator]; - pool2 = [[OFAutoreleasePool alloc] init]; - - while ((child = [enumerator nextObject]) != nil) { - id object; - - if (![[child namespace] isEqual: OF_SERIALIZATION_NS]) - continue; - - object = [child objectByDeserializing]; - [array addItem: &object]; - [object retain]; - - [pool2 releaseObjects]; - } - - [pool release]; - } @catch (id e) { - [self release]; - @throw e; - } - - return self; -} - -- (size_t)count -{ - return [array count]; -} - -- (id*)cArray -{ - return [array cArray]; -} - -- (id)objectAtIndex: (size_t)index -{ - return *((id*)[array itemAtIndex: index]); -} - -- (void)getObjects: (id*)buffer - inRange: (of_range_t)range -{ - id *cArray = [array cArray]; - size_t i, count = [array count]; - - if (range.start + range.length > count) - @throw [OFOutOfRangeException newWithClass: isa]; - - for (i = 0; i < range.length; i++) - buffer[i] = cArray[range.start + i]; -} - -- (size_t)indexOfObject: (id)object -{ - id *cArray = [array cArray]; - size_t i, count = [array count]; - - for (i = 0; i < count; i++) - if ([cArray[i] isEqual: object]) - return i; - - return OF_INVALID_INDEX; -} - -- (size_t)indexOfObjectIdenticalTo: (id)object -{ - id *cArray = [array cArray]; - size_t i, count = [array count]; - - for (i = 0; i < count; i++) - if (cArray[i] == object) - return i; - - return OF_INVALID_INDEX; -} - - -- (OFArray*)objectsInRange: (of_range_t)range -{ - size_t count = [array count]; - - if (range.start + range.length > count) - @throw [OFOutOfRangeException newWithClass: isa]; - - return [OFArray arrayWithCArray: (id*)[array cArray] + range.start - length: range.length]; -} - -- (BOOL)isEqual: (id)object -{ - OFArray *otherArray; - id *cArray, *otherCArray; - size_t i, count; - - if (![object isKindOfClass: [OFCArray class]] && - ![object isKindOfClass: [OFMutableCArray class]]) - return [super isEqual: object]; - - otherArray = object; - - count = [array count]; - - if (count != [otherArray count]) - return NO; - - cArray = [array cArray]; - otherCArray = [otherArray cArray]; - - for (i = 0; i < count; i++) - if (![cArray[i] isEqual: otherCArray[i]]) - return NO; - - return YES; -} - -- (uint32_t)hash -{ - id *cArray = [array cArray]; - size_t i, count = [array count]; - uint32_t hash; - - OF_HASH_INIT(hash); - - for (i = 0; i < count; i++) { - uint32_t h = [cArray[i] hash]; - - OF_HASH_ADD(hash, h >> 24); - OF_HASH_ADD(hash, (h >> 16) & 0xFF); - OF_HASH_ADD(hash, (h >> 8) & 0xFF); - OF_HASH_ADD(hash, h & 0xFF); - } - - OF_HASH_FINALIZE(hash); - - return hash; -} - -#ifdef OF_HAVE_BLOCKS -- (void)enumerateObjectsUsingBlock: (of_array_enumeration_block_t)block -{ - id *cArray = [array cArray]; - size_t i, count = [array count]; - BOOL stop = NO; - - for (i = 0; i < count && !stop; i++) - block(cArray[i], i, &stop); -} -#endif - -- (void)dealloc -{ - id *cArray = [array cArray]; - size_t i, count = [array count]; - - for (i = 0; i < count; i++) - [cArray[i] release]; - - [array release]; - - [super dealloc]; -} -@end Index: src/OFMutableArray.m ================================================================== --- src/OFMutableArray.m +++ src/OFMutableArray.m @@ -17,11 +17,11 @@ #include "config.h" #include #import "OFMutableArray.h" -#import "OFMutableCArray.h" +#import "OFMutableArray_adjacent.h" #import "OFAutoreleasePool.h" #import "OFEnumerationMutationException.h" #import "OFInvalidArgumentException.h" #import "OFNotImplementedException.h" @@ -34,58 +34,59 @@ } placeholder; @implementation OFMutableArrayPlaceholder - init { - return (id)[[OFMutableCArray alloc] init]; + return (id)[[OFMutableArray_adjacent alloc] init]; } - initWithObject: (id)object { - return (id)[[OFMutableCArray alloc] initWithObject: object]; + return (id)[[OFMutableArray_adjacent alloc] initWithObject: object]; } - initWithObjects: (id)firstObject, ... { id ret; va_list arguments; va_start(arguments, firstObject); - ret = [[OFMutableCArray alloc] initWithObject: firstObject - arguments: arguments]; + ret = [[OFMutableArray_adjacent alloc] initWithObject: firstObject + arguments: arguments]; va_end(arguments); return ret; } - initWithObject: (id)firstObject arguments: (va_list)arguments { - return (id)[[OFMutableCArray alloc] initWithObject: firstObject - arguments: arguments]; + return (id)[[OFMutableArray_adjacent alloc] initWithObject: firstObject + arguments: arguments]; } - initWithArray: (OFArray*)array { - return (id)[[OFMutableCArray alloc] initWithArray: array]; + return (id)[[OFMutableArray_adjacent alloc] initWithArray: array]; } - initWithCArray: (id*)objects { - return (id)[[OFMutableCArray alloc] initWithCArray: objects]; + return (id)[[OFMutableArray_adjacent alloc] initWithCArray: objects]; } - initWithCArray: (id*)objects length: (size_t)length { - return (id)[[OFMutableCArray alloc] initWithCArray: objects - length: length]; + return (id)[[OFMutableArray_adjacent alloc] initWithCArray: objects + length: length]; } - initWithSerialization: (OFXMLElement*)element { - return (id)[[OFMutableCArray alloc] initWithSerialization: element]; + return (id)[[OFMutableArray_adjacent alloc] + initWithSerialization: element]; } - retain { return self; ADDED src/OFMutableArray_adjacent.h Index: src/OFMutableArray_adjacent.h ================================================================== --- src/OFMutableArray_adjacent.h +++ src/OFMutableArray_adjacent.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#import "OFArray.h" + +@class OFDataArray; + +/** + * \brief A class for storing, adding and removing objects in an array. + */ +@interface OFMutableArray_adjacent: OFMutableArray +{ + OFDataArray *array; + unsigned long mutations; +} +@end ADDED src/OFMutableArray_adjacent.m Index: src/OFMutableArray_adjacent.m ================================================================== --- src/OFMutableArray_adjacent.m +++ src/OFMutableArray_adjacent.m @@ -0,0 +1,279 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#include "config.h" + +#include + +#import "OFMutableArray_adjacent.h" +#import "OFArray_adjacent.h" +#import "OFDataArray.h" +#import "OFAutoreleasePool.h" + +#import "OFEnumerationMutationException.h" +#import "OFInvalidArgumentException.h" +#import "OFOutOfRangeException.h" + +@implementation OFMutableArray_adjacent ++ (void)initialize +{ + if (self == [OFMutableArray_adjacent class]) + [self inheritMethodsFromClass: [OFArray_adjacent class]]; +} + +- (void)addObject: (id)object +{ + [array addItem: &object]; + [object retain]; + + mutations++; +} + +- (void)addObject: (id)object + atIndex: (size_t)index +{ + [array addItem: &object + atIndex: index]; + [object retain]; + + mutations++; +} + +- (void)replaceObject: (id)oldObject + withObject: (id)newObject +{ + id *cArray = [array cArray]; + size_t i, count = [array count]; + + for (i = 0; i < count; i++) { + if ([cArray[i] isEqual: oldObject]) { + [newObject retain]; + [cArray[i] release]; + cArray[i] = newObject; + + return; + } + } +} + +- (void)replaceObjectAtIndex: (size_t)index + withObject: (id)object +{ + id *cArray = [array cArray]; + id oldObject; + + if (index >= [array count]) + @throw [OFOutOfRangeException newWithClass: isa]; + + oldObject = cArray[index]; + cArray[index] = [object retain]; + [oldObject release]; +} + +- (void)replaceObjectIdenticalTo: (id)oldObject + withObject: (id)newObject +{ + id *cArray = [array cArray]; + size_t i, count = [array count]; + + for (i = 0; i < count; i++) { + if (cArray[i] == oldObject) { + [newObject retain]; + [cArray[i] release]; + cArray[i] = newObject; + + return; + } + } +} + +- (void)removeObject: (id)object +{ + id *cArray = [array cArray]; + size_t i, count = [array count]; + + for (i = 0; i < count; i++) { + if ([cArray[i] isEqual: object]) { + object = cArray[i]; + + [array removeItemAtIndex: i]; + mutations++; + + [object release]; + + return; + } + } +} + +- (void)removeObjectIdenticalTo: (id)object +{ + id *cArray = [array cArray]; + size_t i, count = [array count]; + + for (i = 0; i < count; i++) { + if (cArray[i] == object) { + [array removeItemAtIndex: i]; + mutations++; + + [object release]; + + return; + } + } +} + +- (void)removeObjectAtIndex: (size_t)index +{ + id object = [self objectAtIndex: index]; + [array removeItemAtIndex: index]; + [object release]; + + mutations++; +} + +- (void)removeNObjects: (size_t)nObjects +{ + id *cArray = [array cArray], *copy; + size_t i, count = [array count]; + + if (nObjects > count) + @throw [OFOutOfRangeException newWithClass: isa]; + + copy = [self allocMemoryForNItems: nObjects + withSize: sizeof(id)]; + memcpy(copy, cArray + (count - nObjects), nObjects * sizeof(id)); + + @try { + [array removeNItems: nObjects]; + mutations++; + + for (i = 0; i < nObjects; i++) + [copy[i] release]; + } @finally { + [self freeMemory: copy]; + } +} + +- (void)removeObjectsInRange: (of_range_t)range +{ + id *cArray = [array cArray], *copy; + size_t i, count = [array count]; + + if (range.length > count - range.start) + @throw [OFOutOfRangeException newWithClass: isa]; + + copy = [self allocMemoryForNItems: range.length + withSize: sizeof(id)]; + memcpy(copy, cArray + range.start, range.length * sizeof(id)); + + @try { + [array removeNItems: range.length + atIndex: range.start]; + mutations++; + + for (i = 0; i < range.length; i++) + [copy[i] release]; + } @finally { + [self freeMemory: copy]; + } +} + +- (void)removeLastObject +{ + id object = [self objectAtIndex: [array count] - 1]; + [array removeLastItem]; + [object release]; + + mutations++; +} + +- (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state + objects: (id*)objects + count: (int)count +{ + /* + * Super means the implementation from OFArray here, as OFMutableArray + * does not reimplement it. As OFArray_adjacent does not reimplement it + * either, this is fine. + */ + int ret = [super countByEnumeratingWithState: state + objects: objects + count: count]; + + state->mutationsPtr = &mutations; + + return ret; +} + +- (OFEnumerator*)objectEnumerator +{ + return [[[OFArrayEnumerator alloc] + initWithArray: self + mutationsPtr: &mutations] autorelease]; +} + +#ifdef OF_HAVE_BLOCKS +- (void)enumerateObjectsUsingBlock: (of_array_enumeration_block_t)block +{ + id *cArray = [array cArray]; + size_t i, count = [array count]; + BOOL stop = NO; + unsigned long mutations2 = mutations; + + for (i = 0; i < count && !stop; i++) { + if (mutations != mutations2) + @throw [OFEnumerationMutationException + newWithClass: isa + object: self]; + + block(cArray[i], i, &stop); + } +} + +- (void)replaceObjectsUsingBlock: (of_array_replace_block_t)block +{ + id *cArray = [array cArray]; + size_t i, count = [array count]; + BOOL stop = NO; + unsigned long mutations2 = mutations; + + for (i = 0; i < count && !stop; i++) { + id newObject; + + if (mutations != mutations2) + @throw [OFEnumerationMutationException + newWithClass: isa + object: self]; + + newObject = block(cArray[i], i, &stop); + + if (newObject == nil) + @throw [OFInvalidArgumentException newWithClass: isa + selector: _cmd]; + + [newObject retain]; + [cArray[i] release]; + cArray[i] = newObject; + } +} +#endif + +- (void)makeImmutable +{ + isa = [OFArray_adjacent class]; +} +@end DELETED src/OFMutableCArray.h Index: src/OFMutableCArray.h ================================================================== --- src/OFMutableCArray.h +++ src/OFMutableCArray.h @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2008, 2009, 2010, 2011 - * Jonathan Schleifer - * - * All rights reserved. - * - * This file is part of ObjFW. It may be distributed under the terms of the - * Q Public License 1.0, which can be found in the file LICENSE.QPL included in - * the packaging of this file. - * - * Alternatively, it may be distributed under the terms of the GNU General - * Public License, either version 2 or 3, which can be found in the file - * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this - * file. - */ - -#import "OFArray.h" - -@class OFDataArray; - -/** - * \brief A class for storing, adding and removing objects in an array. - */ -@interface OFMutableCArray: OFMutableArray -{ - OFDataArray *array; - unsigned long mutations; -} -@end DELETED src/OFMutableCArray.m Index: src/OFMutableCArray.m ================================================================== --- src/OFMutableCArray.m +++ src/OFMutableCArray.m @@ -1,279 +0,0 @@ -/* - * Copyright (c) 2008, 2009, 2010, 2011 - * Jonathan Schleifer - * - * All rights reserved. - * - * This file is part of ObjFW. It may be distributed under the terms of the - * Q Public License 1.0, which can be found in the file LICENSE.QPL included in - * the packaging of this file. - * - * Alternatively, it may be distributed under the terms of the GNU General - * Public License, either version 2 or 3, which can be found in the file - * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this - * file. - */ - -#include "config.h" - -#include - -#import "OFMutableCArray.h" -#import "OFCArray.h" -#import "OFDataArray.h" -#import "OFAutoreleasePool.h" - -#import "OFEnumerationMutationException.h" -#import "OFInvalidArgumentException.h" -#import "OFOutOfRangeException.h" - -@implementation OFMutableCArray -+ (void)initialize -{ - if (self == [OFMutableCArray class]) - [self inheritMethodsFromClass: [OFCArray class]]; -} - -- (void)addObject: (id)object -{ - [array addItem: &object]; - [object retain]; - - mutations++; -} - -- (void)addObject: (id)object - atIndex: (size_t)index -{ - [array addItem: &object - atIndex: index]; - [object retain]; - - mutations++; -} - -- (void)replaceObject: (id)oldObject - withObject: (id)newObject -{ - id *cArray = [array cArray]; - size_t i, count = [array count]; - - for (i = 0; i < count; i++) { - if ([cArray[i] isEqual: oldObject]) { - [newObject retain]; - [cArray[i] release]; - cArray[i] = newObject; - - return; - } - } -} - -- (void)replaceObjectAtIndex: (size_t)index - withObject: (id)object -{ - id *cArray = [array cArray]; - id oldObject; - - if (index >= [array count]) - @throw [OFOutOfRangeException newWithClass: isa]; - - oldObject = cArray[index]; - cArray[index] = [object retain]; - [oldObject release]; -} - -- (void)replaceObjectIdenticalTo: (id)oldObject - withObject: (id)newObject -{ - id *cArray = [array cArray]; - size_t i, count = [array count]; - - for (i = 0; i < count; i++) { - if (cArray[i] == oldObject) { - [newObject retain]; - [cArray[i] release]; - cArray[i] = newObject; - - return; - } - } -} - -- (void)removeObject: (id)object -{ - id *cArray = [array cArray]; - size_t i, count = [array count]; - - for (i = 0; i < count; i++) { - if ([cArray[i] isEqual: object]) { - object = cArray[i]; - - [array removeItemAtIndex: i]; - mutations++; - - [object release]; - - return; - } - } -} - -- (void)removeObjectIdenticalTo: (id)object -{ - id *cArray = [array cArray]; - size_t i, count = [array count]; - - for (i = 0; i < count; i++) { - if (cArray[i] == object) { - [array removeItemAtIndex: i]; - mutations++; - - [object release]; - - return; - } - } -} - -- (void)removeObjectAtIndex: (size_t)index -{ - id object = [self objectAtIndex: index]; - [array removeItemAtIndex: index]; - [object release]; - - mutations++; -} - -- (void)removeNObjects: (size_t)nObjects -{ - id *cArray = [array cArray], *copy; - size_t i, count = [array count]; - - if (nObjects > count) - @throw [OFOutOfRangeException newWithClass: isa]; - - copy = [self allocMemoryForNItems: nObjects - withSize: sizeof(id)]; - memcpy(copy, cArray + (count - nObjects), nObjects * sizeof(id)); - - @try { - [array removeNItems: nObjects]; - mutations++; - - for (i = 0; i < nObjects; i++) - [copy[i] release]; - } @finally { - [self freeMemory: copy]; - } -} - -- (void)removeObjectsInRange: (of_range_t)range -{ - id *cArray = [array cArray], *copy; - size_t i, count = [array count]; - - if (range.length > count - range.start) - @throw [OFOutOfRangeException newWithClass: isa]; - - copy = [self allocMemoryForNItems: range.length - withSize: sizeof(id)]; - memcpy(copy, cArray + range.start, range.length * sizeof(id)); - - @try { - [array removeNItems: range.length - atIndex: range.start]; - mutations++; - - for (i = 0; i < range.length; i++) - [copy[i] release]; - } @finally { - [self freeMemory: copy]; - } -} - -- (void)removeLastObject -{ - id object = [self objectAtIndex: [array count] - 1]; - [array removeLastItem]; - [object release]; - - mutations++; -} - -- (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state - objects: (id*)objects - count: (int)count -{ - /* - * Super means the implementation from OFArray here, as OFMutableArray - * does not reimplement it. As OFCArray does not reimplement it either, - * this is fine. - */ - int ret = [super countByEnumeratingWithState: state - objects: objects - count: count]; - - state->mutationsPtr = &mutations; - - return ret; -} - -- (OFEnumerator*)objectEnumerator -{ - return [[[OFArrayEnumerator alloc] - initWithArray: self - mutationsPtr: &mutations] autorelease]; -} - -#ifdef OF_HAVE_BLOCKS -- (void)enumerateObjectsUsingBlock: (of_array_enumeration_block_t)block -{ - id *cArray = [array cArray]; - size_t i, count = [array count]; - BOOL stop = NO; - unsigned long mutations2 = mutations; - - for (i = 0; i < count && !stop; i++) { - if (mutations != mutations2) - @throw [OFEnumerationMutationException - newWithClass: isa - object: self]; - - block(cArray[i], i, &stop); - } -} - -- (void)replaceObjectsUsingBlock: (of_array_replace_block_t)block -{ - id *cArray = [array cArray]; - size_t i, count = [array count]; - BOOL stop = NO; - unsigned long mutations2 = mutations; - - for (i = 0; i < count && !stop; i++) { - id newObject; - - if (mutations != mutations2) - @throw [OFEnumerationMutationException - newWithClass: isa - object: self]; - - newObject = block(cArray[i], i, &stop); - - if (newObject == nil) - @throw [OFInvalidArgumentException newWithClass: isa - selector: _cmd]; - - [newObject retain]; - [cArray[i] release]; - cArray[i] = newObject; - } -} -#endif - -- (void)makeImmutable -{ - isa = [OFCArray class]; -} -@end