1//
2//  KTMatrixSparseEnumerator and KTMutableMatrixSparseEnumerator
3//  KTMatrix collection class cluster
4//
5//  Enumerator classes for the sparse implementations of KTMatrix
6//
7//  Copyright (c) 2002 Chris Purcell. All rights reserved.
8//
9//  You may use this code for whatever purposes you wish.
10//  This code comes with no warranties, implied or otherwise.
11//  Using it may damage your data. It shouldn't, but save a copy first.
12//  That's a good idea anyway, actually.
13//
14
15#import "KTMatrixSparseEnumerator.h"
16
17
18@implementation KTMatrixSparseEnumerator
19
20- (id)initWithArray:(const struct KTMatrixSparseElement *)_ar
21              count:(unsigned)_co
22         collection:(id)collection;
23{
24    if ((self = [super init]))
25        {
26        array = _ar;
27        broken = NO;
28        count = _co;
29        offset = 0;
30        source = [collection retain];
31        }
32    return self;
33}
34- (id)nextObject
35{
36    if (broken && (offset < count))
37        offset++;
38    broken = YES;
39    if (offset < count)
40        return array[offset].object;
41    else
42        return NULL;
43}
44- (unsigned)hashedLocation
45{
46    if (offset < count)
47        return array[offset].hashedLocation;
48    else
49        return count;
50}
51
52- (void)dealloc
53{
54    [source release];
55    [super dealloc];
56}
57
58@end
59
60
61@implementation KTMutableMatrixSparseEnumerator
62
63- (id)initWithHashEnumerator:(NSHashEnumerator)enumerator
64                  collection:(id)collection
65{
66    if ((self = [super init]))
67        {
68        source = [collection retain];
69        data = enumerator;
70        }
71    return self;
72}
73- (id)nextObject
74{
75    lastLocation = (struct KTMatrixSparseElement *)
76    NSNextHashEnumeratorItem(&data);
77    if (lastLocation)
78        return lastLocation->object;
79    else
80        return NULL;
81}
82- (unsigned)hashedLocation
83{
84    if (lastLocation)
85        return lastLocation->hashedLocation;
86    else
87        return 0;
88}
89
90- (void)dealloc
91{
92    NSEndHashTableEnumeration(&data);
93    [source release];
94    [super dealloc];
95}
96
97@end
98