summaryrefslogtreecommitdiff
path: root/MLKArray.m
blob: fb7953af5b65ab6e2dcb0a4fcf7b4fff56009baa (plain)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* -*- mode: objc; coding: utf-8 -*- */
/* Toilet Lisp, a Common Lisp subset for the Étoilé runtime.
 * Copyright (C) 2008  Matthias Andreas Benkard.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#import "MLKArray.h"
#import "functions.h"
#import "util.h"

#import <Foundation/NSArray.h>
#import <Foundation/NSEnumerator.h>
#import <Foundation/NSException.h>

#include <stdio.h>
#include <search.h>
#include <string.h>


@implementation MLKArray
+(id) array
{
  return LAUTORELEASE ([[self alloc]
                         initWithDimensions:
                           [NSArray arrayWithObject:
                                      [MLKInteger integerWithInt:0]]]);
}

-(id) initWithDimensions:(NSArray *)dimensions
{
  NSEnumerator *e;
  id el;
  int size;

  size = 1;
  e = [dimensions objectEnumerator];
  while ((el = [e nextObject]))
    {
      el = denullify (el);
      size *= MLKIntWithInteger (el);
    }

  LASSIGN (_dimensions, [dimensions mutableCopy]);
  _data = [[NSMutableData alloc]
            initWithLength:(size * sizeof(id))];
  _buffer = [_data mutableBytes];
  _fillPointer = -1;
  _displacement = nil;

  return self;
}

-(NSArray *) dimensions
{
  return _dimensions;
}

-(id) idAtIndex:(NSUInteger)index
{
  if (index > [_data length] || (_fillPointer != -1 && index > _fillPointer))
    [NSException raise:@"NSRangeException"
                 format:@"Array index out of bounds"];

  return _buffer[index];
}

-(void) insertId:(id)anObject atIndex:(NSUInteger)index
{
  int size;

  if (_fillPointer != -1)
    _fillPointer++;

  [_data increaseLengthBy:sizeof(id)];
  _buffer = [_data mutableBytes];
  size = [_data length];

  memmove (_buffer+index+1, _buffer+index, size - index*sizeof(id));
  _buffer[index] = anObject;
}

-(void) removeObjectAtIndex:(NSUInteger)index
{
  int size;

  if (_fillPointer != -1)
    _fillPointer--;

  size = [_data length];

  memmove (_buffer+index, _buffer+index+1, size - (index+1)*sizeof(id));
  [_data setLength:((size-1) * sizeof(id))];
  _buffer = [_data mutableBytes];
}

-(void) replaceIdAtIndex:(NSUInteger)index withId:(id)anObject
{
  _buffer[index] = anObject;
}

-(NSUInteger) indexOfObjectIdenticalTo:(id)anObject
{
  return [self indexOfObjectIdenticalTo:anObject
               inRange:NSMakeRange(0, [_data length])];
}

static int eq (const void *x, const void *y)
{
  return (x == y ? 0 : 1);
}

-(NSUInteger) indexOfObjectIdenticalTo:(id)anObject inRange:(NSRange)range
{
  // FIXME: How to treat [NSNull null]?
  size_t length = range.length;
  return ((id*)lfind (anObject, _buffer + range.location, &length, sizeof(id), eq)
          - _buffer) / sizeof(id);
}

-(NSUInteger) indexOfObject:(id)anObject
{
  return [self indexOfObject:anObject inRange:NSMakeRange(0, [_data length])];
}

static int equalp (const void *x, const void *y)
{
  // FIXME: Hmm...  What about fixnums?  What about nil?
  return ([(id)x isEqual:(id)y] ? 0 : 1);
}

-(NSUInteger) indexOfObject:(id)anObject inRange:(NSRange)range
{
  // FIXME: How to treat [NSNull null]?
  size_t length = range.length;
  return ((id*)lfind (anObject, _buffer + range.location, &length, sizeof(id), equalp)
          - _buffer) / sizeof(id);
}


-(void) addId:(id)anObject
{
  [self insertId:anObject
        atIndex:(_fillPointer == -1
                 ? ([_data length]/sizeof(id))-1
                 : _fillPointer-1)];
}

-(void) setSize:(int)size ofDimension:(int)dimension
{
  // I'd love to comment the following code, but I can't find the right
  // words to do so.  There's this picture of jumping pointers in
  // parallel in my head.  How to describe it?  I pass.

  int i;
  int new_size;
  const id *sourcePointer;
  id *destPointer;
  int subblock_length, old_block_length, new_block_length;
  NSEnumerator *e;
  id el;
  NSMutableData *old_data;
  const id *old_buffer;

  subblock_length = 1;
  for (i = dimension + 1; i < [_dimensions count]; i++)
    subblock_length *= [[_dimensions objectAtIndex:i] intValue];

  old_block_length = subblock_length * [[_dimensions objectAtIndex:dimension] intValue];
  new_block_length = subblock_length * size;

  [_dimensions replaceObjectAtIndex:dimension
               withObject:[MLKInteger integerWithInt:size]];

  new_size = 1;
  e = [_dimensions objectEnumerator];
  while ((el = [e nextObject]))
    {
      el = denullify (el);
      new_size *= MLKIntWithInteger (el);
    }

  old_data = _data;
  _data = [[NSMutableData alloc]
            initWithLength:(new_size * sizeof(id))];

  _buffer = [_data mutableBytes];
  old_buffer = [old_data bytes];

  sourcePointer = old_buffer;
  destPointer = _buffer;
  while (destPointer < _buffer + (new_size/sizeof(id)) - 1)
    {
      memmove (destPointer, sourcePointer,
               (old_block_length < new_block_length
                ? old_block_length
                : new_block_length)
               * sizeof(id));
      sourcePointer += old_block_length;
      destPointer += new_block_length;
    }

   LDESTROY (old_data);
}

-(void) setFillPointer:(int)fillPointer
{
  _fillPointer = fillPointer;
}

-(int) fillPointer
{
  return _fillPointer;
}

-(void) setDisplacement:(NSArray *)array
{
  LASSIGN (_displacement, array);
}

-(NSArray *) displacement
{
  return _displacement;
}

-(NSUInteger) count
{
  return (_fillPointer == -1 ? [_data length]/sizeof(id) : _fillPointer);
}

-(id) objectAtIndex:(NSUInteger)index
{
  @try
    {
      return nullify ([self idAtIndex:index]);
    }
  @catch (NSException *e)
    {
      if ([[e name] isEqualToString:NSRangeException])
        return nil;
      else
        @throw;
    }
}

-(void) insertObject:(id)anObject atIndex:(NSUInteger)index
{
  [self insertId:denullify(anObject) atIndex:index];
}

-(void) addObject:(id)anObject
{
  [self addId:denullify(anObject)];  
}

-(void) removeLastObject
{
  if (_fillPointer == -1)
    [self removeObjectAtIndex:([_data length]/sizeof(id) - 1)];
  else if (_fillPointer == 0)
    [NSException raise:@"NSRangeException"
                 format:@"Tried to remove an object from an empty array"];
  else
    _fillPointer--;
}

-(void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject
{
  [self replaceIdAtIndex:index withId:anObject];  
}

-(void) dealloc
{
  LDESTROY (_data);
  LDESTROY (_dimensions);
  LDESTROY (_displacement);
  [super dealloc];
}
@end