The R Project SVN R-packages

Rev

Rev 1878 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
714 iacus 1
/*
2
 *  R.app : a Cocoa front end to: "R A Computer Language for Statistical Data Analysis"
3
 *  
4
 *  R.app Copyright notes:
1254 iacus 5
 *                     Copyright (C) 2004-5  The R Foundation
714 iacus 6
 *                     written by Stefano M. Iacus and Simon Urbanek
7
 *
8
 *                  
9
 *  R Copyright notes:
10
 *                     Copyright (C) 1995-1996   Robert Gentleman and Ross Ihaka
11
 *                     Copyright (C) 1998-2001   The R Development Core Team
12
 *                     Copyright (C) 2002-2004   The R Foundation
13
 *
14
 *  This program is free software; you can redistribute it and/or modify
15
 *  it under the terms of the GNU General Public License as published by
16
 *  the Free Software Foundation; either version 2 of the License, or
17
 *  (at your option) any later version.
18
 *
19
 *  This program is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  A copy of the GNU General Public License is available via WWW at
25
 *  http://www.gnu.org/copyleft/gpl.html.  You can also obtain it by
26
 *  writing to the Free Software Foundation, Inc., 59 Temple Place,
27
 *  Suite 330, Boston, MA  02111-1307  USA.
28
 */
700 urbaneks 29
 
30
#import "SortableDataSource.h"
31
 
32
@implementation SortableDataSource
33
 
34
- (void) addColumn: (NSArray*) colCont withName: (NSString*) name
35
{
36
    [col addObject: colCont];
37
    [colNames addObject: name];
38
    if ([col count]==1) {
39
		int i=0;
40
		rows=[colCont count];
41
		sortMap=(int*) malloc(sizeof(int)*rows);
42
		invSortMap=(int*) malloc(sizeof(int)*rows);
43
		while (i<rows) { sortMap[i]=invSortMap[i]=i; i++; }
44
    } else if ([colCont count]!=rows) {
45
		NSLog(@"SortableDataSource: column %@ has %d rows, but the data source has %d rows! Bad things may happen...", name, rows);
46
    }
47
}
48
 
49
- (void) addColumnOfLength: (int) clen withCStrings: (char**) cstr name: (NSString*) name
50
{
51
	NSString **ca = (NSString**) malloc(sizeof(NSString*)*clen);
52
	int i=0;
53
	while (i<clen) {
54
		ca[i] = [NSString stringWithCString: cstr[i]];
55
		i++;
56
	}
57
	[self addColumn: [NSArray arrayWithObjects:ca count:clen] withName:name];
58
	free(ca);
59
}
60
 
926 urbaneks 61
- (void) addColumnOfLength: (int) clen withUTF8Strings: (char**) cstr name: (NSString*) name
62
{
63
	NSString **ca = (NSString**) malloc(sizeof(NSString*)*clen);
64
	int i=0;
65
	while (i<clen) {
66
		ca[i] = [NSString stringWithUTF8String: cstr[i]];
67
		i++;
68
	}
69
	[self addColumn: [NSArray arrayWithObjects:ca count:clen] withName:name];
70
	free(ca);
71
}
72
 
700 urbaneks 73
- (id) init
74
{
75
    self = [super init];
76
    if (self) {
77
		col=[[NSMutableArray alloc] init];
78
		colNames=[[NSMutableArray alloc] init];
79
		rows=0;
80
		sortMap=invSortMap=0;
1878 urbaneks 81
		filter=0; filterLen=0;
700 urbaneks 82
    }
83
    return self;
84
}
85
 
86
- (void) dealloc
87
{
88
    [self reset];
89
    [col release];
90
    [colNames release];
91
    [super dealloc];
92
}
93
 
94
- (int*) sortMap
95
{
96
    return sortMap;
97
}
98
 
99
- (int*) inverseSortMap
100
{
101
    return invSortMap;
102
}
103
 
104
- (void) reset
105
{
1878 urbaneks 106
	[self resetFilter];
700 urbaneks 107
    [col removeAllObjects];
108
    [colNames removeAllObjects];
109
    rows=0;
110
    if (sortMap) free(sortMap); sortMap=0;
111
    if (invSortMap) free(invSortMap); invSortMap=0;
112
}
113
 
114
- (int)numberOfRowsInTableView:(NSTableView *)tableView
115
{
1878 urbaneks 116
    return (filter)?filterLen:rows;
700 urbaneks 117
}
118
 
119
- (unsigned) count
120
{
121
	return rows;
122
}
123
 
1878 urbaneks 124
- (unsigned) rows
700 urbaneks 125
{
1878 urbaneks 126
    return (filter)?filterLen:rows;
127
}
128
 
129
- (void) setFilter: (int*) f length: (int) fl
130
{
131
	[self resetFilter];
132
	filter = (int*) malloc(sizeof(int)*(fl+1));
133
	filterLen=fl;
134
	memcpy(filter, f, sizeof(int)*fl);
135
}
136
 
137
- (void) resetFilter
138
{
139
	if (filter) free(filter);
140
	filter=0;
141
	filterLen=0;
142
}
143
 
144
- (id) objectAtColumn: (NSString*) name index: (int) row
145
{
700 urbaneks 146
    if (row<0 || row>=rows || !name)
147
		return nil;
148
    else {
149
		unsigned c = [colNames indexOfObject:name];
150
		if (c==NSNotFound) return nil;
151
		else {
152
			NSArray *cc = (NSArray*) [col objectAtIndex: c];
153
			return (!cc)?nil:[cc objectAtIndex: sortMap[row]];
154
		}
155
    }
156
}
157
 
1878 urbaneks 158
- (id) objectAtColumn: (NSString*) name row: (int) row
159
{
160
	return (filter)?
161
	((row<filterLen)?[self objectAtColumn: name index: filter[row]]:nil)
162
	:[self objectAtColumn: name index: row];
163
}
164
 
700 urbaneks 165
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row
166
{
167
	return [self objectAtColumn: [tableColumn identifier] row: row];
168
}
169
 
170
- (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(int)row
171
{
172
}
173
 
174
- (void)tableView:(NSTableView *)tableView sortDescriptorsDidChange:(NSArray *) oldDescriptors
175
{
176
}
177
 
178
@end