-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathISArray.c
98 lines (75 loc) · 1.99 KB
/
ISArray.c
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
/* Created By: Justin Meiners (2013) */
#include "ISArray.h"
struct ISArray
{
/* base must come first */
ISObjectBase base;
/* instance variables */
int count;
ISObjectRef* buffer;
};
/* forward declaration */
struct ISArray* _ISArrayAlloc();
/* class dealloc */
void _ISArrayDealloc(ISObjectRef object)
{
struct ISString* string = (struct ISString*)object;
free(string);
}
ISObjectRef _ISArrayCopy(ISObjectRef object)
{
ISArrayRef array = (ISArrayRef)object;
struct ISArray* newArray = (struct ISArray*)_ISArrayAlloc();
newArray->count = array->count;
newArray->buffer = malloc(array->count * sizeof(ISObjectRef));
memcpy(newArray->buffer, array->buffer, array->count * sizeof(ISObjectRef));
return newArray;
}
/* define class */
static ISObjectClass _ISArrayClass = {
"ISArray", /* class name */
_ISArrayDealloc, /* dealloc */
_ISArrayCopy
};
struct ISArray* _ISArrayAlloc()
{
/* allocate memory */
struct ISArray* array = (struct ISArray*)malloc(sizeof(struct ISArray));
/* setup class */
ISObjectBaseInit(&array->base, &_ISArrayClass);
/* setup ivars */
array->count = 0;
array->buffer = NULL;
return array;
}
ISArrayRef ISArrayCreate(const ISObjectRef* values, int capacity)
{
struct ISArray* array = _ISArrayAlloc();
array->count = capacity;
array->buffer = malloc(capacity & sizeof(ISObjectRef));
for (int i = 0; i < capacity; i ++)
{
array->buffer[i] = values[i];
}
return array;
}
ISObjectRef ISArrayGetValueAtIndex(ISArrayRef array, int index)
{
assert(index > 0 && index < array->count);
return array->buffer[index];
}
int ISArrayCount(ISArrayRef array)
{
return array->count;
}
int ISArrayContainsValue(ISArrayRef array, const ISObjectRef* value)
{
for (int i = 0; i < array->count; i ++)
{
if (ISArrayGetValueAtIndex(array, i) == value)
{
return 1;
}
}
return 0;
}