]> err.no Git - sope/blob - sope-core/NGExtensions/FdExt.subproj/NSArray+enumerator.m
Drop apache 1 build-dependency
[sope] / sope-core / NGExtensions / FdExt.subproj / NSArray+enumerator.m
1 /*
2   Copyright (C) 2000-2008 SKYRIX Software AG
3   Copyright (C) 2008      Helge Hess
4
5   This file is part of SOPE.
6
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
10   later version.
11
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.
16
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
20   02111-1307, USA.
21 */
22
23 #include "NSArray+enumerator.h"
24 #include "common.h"
25
26 @implementation NSArray(enumerator)
27
28 - (id)initWithObjectsFromEnumerator:(NSEnumerator *)_enumerator {
29   NSMutableArray *array = nil;
30   
31   array = [[NSMutableArray alloc]
32                            initWithObjectsFromEnumerator:_enumerator];
33   self = [self initWithArray:array];
34   [array release]; array = nil;
35   return self;
36 }
37
38 /* mapping */
39
40 - (NSArray *)mappedArrayUsingSelector:(SEL)_selector {
41   int i, count = [self count];
42   id  objects[count];
43   IMP objAtIndex;
44   
45   if (_selector == NULL) return self;
46
47   objAtIndex = [self methodForSelector:@selector(objectAtIndex:)];
48
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];
54   }
55
56   return [NSArray arrayWithObjects:objects count:count];
57 }
58 - (NSArray *)mappedArrayUsingSelector:(SEL)_selector withObject:(id)_object {
59   int i, count = [self count];
60   id  objects[count];
61   IMP objAtIndex;
62   
63   if (_selector == NULL) return self;
64
65   objAtIndex = [self methodForSelector:@selector(objectAtIndex:)];
66
67   for (i = 0; i < count; i++) {
68     objects[i] = [objAtIndex(self, @selector(objectAtIndex:), i)
69                             performSelector:_selector withObject:_object];
70
71     if (objects[i] == nil)
72       objects[i] = [NSNull null];
73   }
74
75   return [NSArray arrayWithObjects:objects count:count];
76 }
77
78 - (NSSet *)mappedSetUsingSelector:(SEL)_selector {
79   return [NSSet setWithArray:[self mappedArrayUsingSelector:_selector]];
80 }
81 - (NSSet *)mappedSetUsingSelector:(SEL)_selector withObject:(id)_object {
82   return [NSSet setWithArray:[self mappedArrayUsingSelector:_selector
83                                    withObject:_object]];
84 }
85
86 #if !LIB_FOUNDATION_LIBRARY
87
88 - (NSArray *)map:(SEL)_sel {
89   unsigned int index, count;
90   id array;
91   
92   count = [self count];
93   array = [NSMutableArray arrayWithCapacity:count];
94   for (index = 0; index < count; index++) {
95     [array insertObject:[[self objectAtIndex:index] performSelector:_sel]
96            atIndex:index];
97   }
98   return array;
99 }
100
101 - (NSArray *)map:(SEL)_sel with:(id)_arg {
102   unsigned int index, count;
103   id array;
104   
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]
110            atIndex:index];
111   }
112   return array;
113 }
114
115 #endif
116
117 @end /* NSArray(enumerator) */
118
119 @implementation NSMutableArray(enumerator) 
120
121 - (id)initWithObjectsFromEnumerator:(NSEnumerator *)_enumerator {
122 #if NeXT_Foundation_LIBRARY || COCOA_Foundation_LIBRARY
123   NSMutableArray *ma = [[NSMutableArray alloc] initWithCapacity:64];
124   id obj;
125
126   while ((obj = [_enumerator nextObject]))
127     [ma addObject:obj];
128
129   self = [self initWithArray:ma];
130   [ma release]; ma = nil;
131   return self;
132 #else
133   if ((self = [self init]) != nil) {
134     id obj = nil;
135      
136     // Does not work on Cocoa because we only have NSCFArray over there ...
137     while ((obj = [_enumerator nextObject]))
138       [self addObject:obj];
139   }
140   return self;
141 #endif
142 }
143
144 @end /* NSMutableArray(enumerator)  */
145
146 void __link_NGExtensions_NSArrayEnumerator() {
147   __link_NGExtensions_NSArrayEnumerator();
148 }