]> err.no Git - sope/blob - sope-gdl1/GDLAccess/EOKeySortOrdering.m
added missing inline pathes
[sope] / sope-gdl1 / GDLAccess / EOKeySortOrdering.m
1 /* 
2    EOKeySortOrdering.m
3
4    Copyright (C) 1996 Free Software Foundation, Inc.
5
6    Author: Mircea Oancea <mircea@jupiter.elcom.pub.ro>
7    Date: 1996
8
9    This file is part of the GNUstep Database Library.
10
11    This library is free software; you can redistribute it and/or
12    modify it under the terms of the GNU Library General Public
13    License as published by the Free Software Foundation; either
14    version 2 of the License, or (at your option) any later version.
15
16    This library is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19    Library General Public License for more details.
20
21    You should have received a copy of the GNU Library General Public
22    License along with this library; see the file COPYING.LIB.
23    If not, write to the Free Software Foundation,
24    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
27 #import <Foundation/NSAutoreleasePool.h>
28 #import <Foundation/NSArray.h>
29 #import <Foundation/NSDictionary.h>
30 #import <Foundation/NSString.h>
31 #import "common.h"
32 #import "EOKeySortOrdering.h"
33 #import <EOControl/EOKeyValueCoding.h>
34
35 @implementation EOKeySortOrdering
36
37 + keyOrderingWithKey:(NSString*)aKey ordering:(NSComparisonResult)anOrdering
38 {
39     return AUTORELEASE([[EOKeySortOrdering alloc]
40                            initWithKey:aKey ordering:anOrdering]);
41 }
42
43 - initWithKey:(NSString*)aKey ordering:(NSComparisonResult)anOrdering
44 {
45     ASSIGN(key, aKey);
46     ordering = anOrdering;
47     return self;
48 }
49
50 - (NSString*)key                        {return key;}
51 - (NSComparisonResult)ordering          {return ordering;}
52
53 @end
54
55 // TODO : integrate this function in the two methods above and optimize
56 //        object creation and method calls for objects that provide quick
57 //        access to their values - do not use nested functions
58
59 static NSComparisonResult _keySortCompare(id obj1, id obj2, NSArray* order)
60      __attribute__((unused));
61
62 static NSComparisonResult _keySortCompare(id obj1, id obj2, NSArray* order) {
63     int i, n;
64     
65     for (i = 0, n = [order count]; i < n; i++) {
66         id val1, val2, key, kar;
67         NSComparisonResult ord, vord;
68         EOKeySortOrdering* kso = [order objectAtIndex:i];
69         
70         key = [kso key];
71         ord = [kso ordering];
72         kar = [NSArray arrayWithObject:key];
73         
74         val1 = [[obj1 valuesForKeys:kar] objectForKey:key];
75         val2 = [[obj2 valuesForKeys:kar] objectForKey:key];
76         
77         if (!val1 && !val2)
78             continue;
79         
80         if (!val1 && val2)
81             return ord == NSOrderedAscending ? 
82                    NSOrderedAscending : NSOrderedDescending;
83         
84         if (val1 && !val2)
85             return ord == NSOrderedAscending ? 
86                    NSOrderedDescending : NSOrderedAscending;
87         
88         vord = [(NSString *)val1 compare:val2];
89         
90         if (vord == NSOrderedSame)
91             continue;
92         
93         if (vord == NSOrderedAscending)
94             return ord == NSOrderedAscending ? 
95                    NSOrderedAscending : NSOrderedDescending;
96         else
97             return ord == NSOrderedAscending ? 
98                    NSOrderedDescending : NSOrderedAscending;
99     }
100     
101     return NSOrderedSame;
102 }
103
104 #if 0
105
106 @implementation NSArray(EOKeyBasedSorting)
107
108 - (NSArray*)sortedArrayUsingKeyOrderArray:(NSArray*)orderArray
109 {
110     NSArray* arry;
111     CREATE_AUTORELEASE_POOL(pool);
112     
113     arry = [self sortedArrayUsingFunction:
114                      (int(*)(id, id, void*))_keySortCompare
115                  context:orderArray];
116     RELEASE(pool);
117     return arry;
118 }
119
120 @end
121
122 @implementation NSMutableArray(EOKeyBasedSorting)
123
124 - (void)sortUsingKeyOrderArray:(NSArray*)orderArray
125 {
126     CREATE_AUTORELEASE_POOL(pool);
127
128     [self sortUsingFunction:
129               (int(*)(id, id, void*))_keySortCompare
130           context:orderArray];
131     RELEASE(pool);
132 }
133
134 @end
135
136 #endif
137
138 /*
139   Local Variables:
140   c-basic-offset: 4
141   tab-width: 8
142   End:
143 */
144