1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
//
// MSparseMatrix.m
// MySQLGUICommon
//
// Created by Alfredo Kojima on 3/24/05.
// Copyright 2005 MySQL AB. All rights reserved.
//
#import "MSparseMatrix.h"
@implementation MSparseMatrix
static unsigned long findRow(MSparseMatrixRow *rows, unsigned long count, unsigned long row,
BOOL approx)
{
unsigned long l, r, m;
if (count == 0 || rows[count-1].row < row)
return approx ? count : (unsigned long)-1;
l= 0;
r= count-1;
while (l <= r && r != (unsigned long)-1)
{
m= (r+l)/2;
if (rows[m].row < row)
l= m+1;
else if (rows[m].row > row)
r= m-1;
else
return m;
}
if (approx)
return m;
else
return (unsigned long)-1;
}
- (id)init
{
self= [super init];
if (self)
{
_zone= NSDefaultMallocZone();
_rowSize= 8;
_rows= NSZoneMalloc(_zone, _rowSize*sizeof(MSparseMatrixRow));
}
return self;
}
- (void)dealloc
{
[self removeAllObjects];
if (_rows)
NSZoneFree(_zone, _rows);
[super dealloc];
}
- (id)objectAtRow:(unsigned long)row
column:(unsigned long)column
{
unsigned long index= findRow(_rows, _rowCount, row, NO);
MSparseMatrixRow *srow= index == (unsigned long)-1?NULL:_rows+index;
if (srow)
{
MSparseMatrixColumn *scolumn= srow->column;
while (scolumn && scolumn->column < column)
scolumn= scolumn->next;
if (scolumn && scolumn->column == column)
return scolumn->data;
}
return nil;
}
- (void)setObject:(id)obj
atRow:(unsigned long)row
column:(unsigned long)column
{
unsigned long index= findRow(_rows, _rowCount, row, YES);
MSparseMatrixRow *srow;
if (index == (unsigned long)-1 || index >= _rowCount || _rows[index].row != row)
{
if (_rowCount+1 == _rowSize)
{
_rowSize+= 8;
_rows= NSZoneRealloc(_zone, _rows, _rowSize*sizeof(MSparseMatrixRow));
}
memmove(_rows+index+1, _rows+index, (_rowCount-index)*sizeof(MSparseMatrixRow));
_rows[index].row= row;
_rows[index].column= NULL;
_rowCount++;
srow= _rows+index;
}
else
srow= _rows+index;
if (!srow->column)
{
srow->column= NSZoneMalloc(_zone, sizeof(MSparseMatrixColumn));
srow->column->column= column;
srow->column->data= [obj retain];
srow->column->next= NULL;
}
else
{
MSparseMatrixColumn *scolumn= srow->column;
if (scolumn->column > column)
{
MSparseMatrixColumn *ncolumn;
ncolumn= NSZoneMalloc(_zone, sizeof(MSparseMatrixColumn));
ncolumn->column= column;
ncolumn->data= [obj retain];
ncolumn->next= scolumn;
srow->column= ncolumn;
}
else
{
MSparseMatrixColumn *prev= NULL;
while (scolumn && scolumn->column < column)
{
prev= scolumn;
scolumn= scolumn->next;
}
if (scolumn && scolumn->column == column)
{
[scolumn->data release];
scolumn->data= [obj retain];
}
else
{
MSparseMatrixColumn *ncolumn;
ncolumn= NSZoneMalloc(_zone, sizeof(MSparseMatrixColumn));
ncolumn->column= column;
ncolumn->data= [obj retain];
ncolumn->next= scolumn;
prev->next= ncolumn;
}
}
}
}
- (void)removeAllObjects
{
unsigned long r;
for (r= 0; r < _rowCount; r++)
{
while (_rows[r].column)
{
MSparseMatrixColumn *next= _rows[r].column->next;
[_rows[r].column->data release];
NSZoneFree(_zone, _rows[r].column);
_rows[r].column= next;
}
}
_rowCount= 0;
}
- (MSparseMatrixRow*)firstRow
{
if (_rowCount == 0)
return NULL;
else
return _rows;
}
- (MSparseMatrixRow*)nextRowAfter:(MSparseMatrixRow*)row
{
if (row < _rows+_rowCount-1)
return row+1;
else
return NULL;
}
- (MSparseMatrixColumn*)firstColumnInRow:(MSparseMatrixRow*)row
{
return row->column;
}
- (MSparseMatrixColumn*)nextColumnInRow:(MSparseMatrixRow*)row
after:(MSparseMatrixColumn*)column
{
return column->next;
}
@end
|