Wired Foundation  2.0
A foundation framework for the Wired implementation on Mac OS X
NSArray-WIFoundation.m
00001 /* $Id$ */
00002 
00003 /*
00004  *  Copyright (c) 2003-2009 Axel Andersson
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *  1. Redistributions of source code must retain the above copyright
00011  *     notice, this list of conditions and the following disclaimer.
00012  *  2. Redistributions in binary form must reproduce the above copyright
00013  *     notice, this list of conditions and the following disclaimer in the
00014  *     documentation and/or other materials provided with the distribution.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00017  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
00020  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00021  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00023  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
00024  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00025  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00026  * POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00029 #import <WiredFoundation/NSArray-WIFoundation.h>
00030 #import <objc/message.h>
00031 
00032 @implementation NSArray(WIFoundation)
00033 
00034 - (NSString *)stringAtIndex:(NSUInteger)index {
00035         if(index < [self count])
00036                 return [self objectAtIndex:index];
00037         
00038         return @"";
00039 }
00040 
00041 
00042 
00043 - (id)safeObjectAtIndex:(NSUInteger)index {
00044         if(index < [self count])
00045                 return [self objectAtIndex:index];
00046         
00047         return NULL;
00048 }
00049 
00050 
00051 
00052 #pragma mark -
00053 
00054 - (NSUInteger)indexOfString:(NSString *)string {
00055         return [self indexOfString:string options:0];
00056 }
00057 
00058 
00059 
00060 - (NSUInteger)indexOfString:(NSString *)string options:(NSUInteger)options {
00061         NSEnumerator    *enumerator;
00062         NSRange                 range;
00063         id                              object;
00064         NSUInteger              i = 0;
00065 
00066         enumerator = [self objectEnumerator];
00067 
00068         while((object = [enumerator nextObject])) {
00069                 if([object isKindOfClass:[NSString class]]) {
00070                         range = [object rangeOfString:string options:options];
00071                         
00072                         if(range.location != NSNotFound)
00073                                 return i;
00074                 }
00075                 
00076                 i++;
00077         }
00078 
00079         return NSNotFound;
00080 }
00081 
00082 
00083 
00084 - (BOOL)containsString:(NSString *)string {
00085         return ([self indexOfString:string options:0] != NSNotFound);
00086 }
00087 
00088 
00089 
00090 - (BOOL)containsString:(NSString *)string options:(NSUInteger)options {
00091         return ([self indexOfString:string options:options] != NSNotFound);
00092 }
00093 
00094 
00095 
00096 - (NSArray *)stringsMatchingString:(NSString *)string {
00097         return [self stringsMatchingString:string options:0];
00098 }
00099 
00100 
00101 
00102 - (NSArray *)stringsMatchingString:(NSString *)string options:(NSUInteger)options {
00103         NSEnumerator    *enumerator;
00104         NSMutableArray  *array;
00105         NSRange                 range;
00106         id                              object;
00107         
00108         array = [NSMutableArray array];
00109         enumerator = [self objectEnumerator];
00110         
00111         while((object = [enumerator nextObject])) {
00112                 if([object isKindOfClass:[NSString class]]) {
00113                         range = [object rangeOfString:string options:options];
00114                         
00115                         if(range.location != NSNotFound)
00116                                 [array addObject:object];
00117                 }
00118         }
00119         
00120         return array;
00121 }
00122 
00123 
00124 - (void)makeObjectsPerformSelector:(SEL)selector withObject:(id)object1 withObject:(id)object2 {
00125         NSEnumerator    *enumerator;
00126         id                              object;
00127 
00128         enumerator = [self objectEnumerator];
00129 
00130         while((object = [enumerator nextObject]))
00131                 objc_msgSend(object, selector, object1, object2);
00132 }
00133 
00134 
00135 
00136 - (void)makeObjectsPerformSelector:(SEL)selector withBool:(BOOL)value {
00137         NSEnumerator    *enumerator;
00138         id                              object;
00139 
00140         enumerator = [self objectEnumerator];
00141 
00142         while((object = [enumerator nextObject]))
00143                 objc_msgSend(object, selector, value);
00144 }
00145 
00146 
00147 
00148 - (NSArray *)subarrayToIndex:(NSUInteger)index {
00149         return [self subarrayWithRange:NSMakeRange(0, index)];
00150 }
00151 
00152 
00153 
00154 - (NSArray *)subarrayFromIndex:(NSUInteger)index {
00155         return [self subarrayWithRange:NSMakeRange(index, [self count] - index)];
00156 }
00157 
00158 
00159 
00160 - (NSArray *)reversedArray {
00161         NSEnumerator    *enumerator;
00162         NSMutableArray  *array;
00163         id                              object;
00164         
00165         array = [NSMutableArray array];
00166         enumerator = [self reverseObjectEnumerator];
00167         
00168         while((object = [enumerator nextObject]))
00169                 [array addObject:object];
00170 
00171         return array;
00172 }
00173 
00174 
00175 
00176 - (NSArray *)shuffledArray {
00177         NSMutableArray          *array;
00178         NSUInteger                      i, count;
00179         
00180         array = [self mutableCopy];
00181         count = [array count];
00182         
00183         for(i = 0; i < count; i++)
00184                 [array moveObjectAtIndex:i toIndex:random() % count];
00185         
00186         return [array autorelease];
00187 }
00188 
00189 
00190 
00191 #pragma mark -
00192 
00193 
00194 - (NSNumber *)minimumNumber {
00195         NSEnumerator    *enumerator;
00196         NSNumber                *number = NULL;
00197         id                              object;
00198         
00199         enumerator = [self objectEnumerator];
00200         
00201         while((object = [enumerator nextObject])) {
00202                 if([object isKindOfClass:[NSNumber class]]) {
00203                         if(!number || [object unsignedLongLongValue] < [number unsignedLongLongValue])
00204                                 number = object;
00205                 }
00206         }
00207         
00208         return number;
00209 }
00210 
00211 
00212 
00213 - (NSNumber *)maximumNumber {
00214         NSEnumerator    *enumerator;
00215         NSNumber                *number = NULL;
00216         id                              object;
00217         
00218         enumerator = [self objectEnumerator];
00219         
00220         while((object = [enumerator nextObject])) {
00221                 if([object isKindOfClass:[NSNumber class]]) {
00222                         if(!number || [object unsignedLongLongValue] > [number unsignedLongLongValue])
00223                                 number = object;
00224                 }
00225         }
00226         
00227         return number;
00228 }
00229 
00230 @end
00231 
00232 
00233 
00234 @implementation NSArray(WIDeepMutableCopying)
00235 
00236 - (NSMutableArray *)deepMutableCopyWithZone:(NSZone *)zone {
00237         NSEnumerator    *enumerator;
00238         NSMutableArray  *array;
00239         id                              object, copy;
00240         
00241         array = [[NSMutableArray allocWithZone:zone] initWithCapacity:[self count]];
00242         enumerator = [self objectEnumerator];
00243         
00244         while((object = [enumerator nextObject])) {
00245                 copy = [object deepMutableCopyWithZone:zone];
00246                 [array addObject:copy];
00247                 [copy release];
00248         }
00249         
00250         return array;
00251 }
00252 
00253 @end
00254 
00255 
00256 
00257 @implementation NSMutableArray(WIFoundation)
00258 
00259 - (void)addObject:(id)object sortedUsingSelector:(SEL)selector {
00260         IMP                                     method;
00261         NSComparisonResult      result;
00262         NSUInteger                      i, count;
00263 
00264         count = [self count];
00265         
00266         if(count == 0) {
00267                 [self addObject:object];
00268         } else {
00269                 method = [object methodForSelector:selector];
00270 
00271                 for(i = 0; i < count; i++) {
00272                         result = (NSComparisonResult) method(object, selector, [self objectAtIndex:i]);
00273                         
00274                         if(result < 0) {
00275                                 [self insertObject:object atIndex:i];
00276                                 
00277                                 return;
00278                         }
00279                 }
00280                         
00281                 [self addObject:object];
00282         }
00283 }
00284 
00285 
00286 
00287 - (NSUInteger)moveObjectAtIndex:(NSUInteger)from toIndex:(NSUInteger)to {
00288         id                      object;
00289         NSUInteger      index;
00290 
00291         if(from == to)
00292                 return from;
00293         
00294         object = [self objectAtIndex:from];
00295         index = (to <= from) ? to : to - 1;
00296 
00297         [object retain];
00298         [self removeObjectAtIndex:from];
00299         [self insertObject:object atIndex:index];
00300         [object release];
00301         
00302         return index;
00303 }
00304 
00305 
00306 
00307 #pragma mark -
00308 
00309 - (void)reverse {
00310         [self setArray:[self reversedArray]];
00311 }
00312 
00313 
00314 
00315 - (void)shuffle {
00316         [self setArray:[self shuffledArray]];
00317 }
00318 
00319 @end
 All Classes