2 Copyright (C) 2000-2008 SKYRIX Software AG
3 Copyright (C) 2008 Helge Hess
5 This file is part of SOPE.
7 SOPE is free software; you can redistribute it and/or modify it under
8 the terms of the GNU Lesser General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with SOPE; see the file COPYING. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include "NSArray+enumerator.h"
26 @implementation NSArray(enumerator)
28 - (id)initWithObjectsFromEnumerator:(NSEnumerator *)_enumerator {
29 NSMutableArray *array = nil;
31 array = [[NSMutableArray alloc]
32 initWithObjectsFromEnumerator:_enumerator];
33 self = [self initWithArray:array];
34 [array release]; array = nil;
40 - (NSArray *)mappedArrayUsingSelector:(SEL)_selector {
41 int i, count = [self count];
45 if (_selector == NULL) return self;
47 objAtIndex = [self methodForSelector:@selector(objectAtIndex:)];
49 for (i = 0; i < count; i++) {
50 objects[i] = objAtIndex(self, @selector(objectAtIndex:), i);
51 objects[i] = [objects[i] performSelector:_selector];
52 if (objects[i] == nil)
53 objects[i] = [NSNull null];
56 return [NSArray arrayWithObjects:objects count:count];
58 - (NSArray *)mappedArrayUsingSelector:(SEL)_selector withObject:(id)_object {
59 int i, count = [self count];
63 if (_selector == NULL) return self;
65 objAtIndex = [self methodForSelector:@selector(objectAtIndex:)];
67 for (i = 0; i < count; i++) {
68 objects[i] = [objAtIndex(self, @selector(objectAtIndex:), i)
69 performSelector:_selector withObject:_object];
71 if (objects[i] == nil)
72 objects[i] = [NSNull null];
75 return [NSArray arrayWithObjects:objects count:count];
78 - (NSSet *)mappedSetUsingSelector:(SEL)_selector {
79 return [NSSet setWithArray:[self mappedArrayUsingSelector:_selector]];
81 - (NSSet *)mappedSetUsingSelector:(SEL)_selector withObject:(id)_object {
82 return [NSSet setWithArray:[self mappedArrayUsingSelector:_selector
86 #if !LIB_FOUNDATION_LIBRARY
88 - (NSArray *)map:(SEL)_sel {
89 unsigned int index, count;
93 array = [NSMutableArray arrayWithCapacity:count];
94 for (index = 0; index < count; index++) {
95 [array insertObject:[[self objectAtIndex:index] performSelector:_sel]
101 - (NSArray *)map:(SEL)_sel with:(id)_arg {
102 unsigned int index, count;
105 count = [self count];
106 array = [NSMutableArray arrayWithCapacity:count];
107 for (index = 0; index < count; index++) {
108 [array insertObject:[[self objectAtIndex:index]
109 performSelector:_sel withObject:_arg]
117 @end /* NSArray(enumerator) */
119 @implementation NSMutableArray(enumerator)
121 - (id)initWithObjectsFromEnumerator:(NSEnumerator *)_enumerator {
122 #if NeXT_Foundation_LIBRARY || COCOA_Foundation_LIBRARY
123 NSMutableArray *ma = [[NSMutableArray alloc] initWithCapacity:64];
126 while ((obj = [_enumerator nextObject]))
129 self = [self initWithArray:ma];
130 [ma release]; ma = nil;
133 if ((self = [self init]) != nil) {
136 // Does not work on Cocoa because we only have NSCFArray over there ...
137 while ((obj = [_enumerator nextObject]))
138 [self addObject:obj];
144 @end /* NSMutableArray(enumerator) */
146 void __link_NGExtensions_NSArrayEnumerator() {
147 __link_NGExtensions_NSArrayEnumerator();