summaryrefslogtreecommitdiff
path: root/fitz/obj_array.c
blob: 75f4a6b5ade6fa5960397d50f929ac67f6dd5179 (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
#include "fitz.h"

fz_obj *
fz_newarray(int initialcap)
{
	fz_obj *obj;
	int i;

	obj = fz_malloc(sizeof (fz_obj));
	obj->refs = 1;
	obj->kind = FZ_ARRAY;

	obj->u.a.len = 0;
	obj->u.a.cap = initialcap > 1 ? initialcap : 6;

	obj->u.a.items = fz_malloc(sizeof (fz_obj*) * obj->u.a.cap);
	for (i = 0; i < obj->u.a.cap; i++)
		obj->u.a.items[i] = nil;

	return obj;
}

fz_obj *
fz_copyarray(fz_obj *obj)
{
	fz_obj *new;
	int i;

	if (!fz_isarray(obj))
		fz_warn("assert: not an array (%s)", fz_objkindstr(obj));

	new = fz_newarray(fz_arraylen(obj));

	for (i = 0; i < fz_arraylen(obj); i++)
	{
		fz_arraypush(new, fz_arrayget(obj, i));
	}

	return new;
}

int
fz_arraylen(fz_obj *obj)
{
	obj = fz_resolveindirect(obj);
	if (!fz_isarray(obj))
		return 0;
	return obj->u.a.len;
}

fz_obj *
fz_arrayget(fz_obj *obj, int i)
{
	obj = fz_resolveindirect(obj);

	if (!fz_isarray(obj))
		return nil;

	if (i < 0 || i >= obj->u.a.len)
		return nil;

	return obj->u.a.items[i];
}

void
fz_arrayput(fz_obj *obj, int i, fz_obj *item)
{
	obj = fz_resolveindirect(obj);

	if (!fz_isarray(obj))
		fz_warn("assert: not an array (%s)", fz_objkindstr(obj));
	else if (i < 0)
		fz_warn("assert: index %d < 0", i);
	else if (i >= obj->u.a.len)
		fz_warn("assert: index %d > length %d", i, obj->u.a.len);
	else
	{
		if (obj->u.a.items[i])
			fz_dropobj(obj->u.a.items[i]);
		obj->u.a.items[i] = fz_keepobj(item);
	}
}

void
fz_arraypush(fz_obj *obj, fz_obj *item)
{
	obj = fz_resolveindirect(obj);

	if (!fz_isarray(obj))
		fz_warn("assert: not an array (%s)", fz_objkindstr(obj));
	else
	{
		if (obj->u.a.len + 1 > obj->u.a.cap)
		{
			int i;
			obj->u.a.cap = (obj->u.a.cap * 3) / 2;
			obj->u.a.items = fz_realloc(obj->u.a.items, sizeof (fz_obj*) * obj->u.a.cap);
			for (i = obj->u.a.len ; i < obj->u.a.cap; i++)
				obj->u.a.items[i] = nil;
		}
		obj->u.a.items[obj->u.a.len] = fz_keepobj(item);
		obj->u.a.len++;
	}
}

void
fz_arrayinsert(fz_obj *obj, fz_obj *item)
{
	obj = fz_resolveindirect(obj);

	if (!fz_isarray(obj))
		fz_warn("assert: not an array (%s)", fz_objkindstr(obj));
	else
	{
		if (obj->u.a.len + 1 > obj->u.a.cap)
		{
			int i;
			obj->u.a.cap = (obj->u.a.cap * 3) / 2;
			obj->u.a.items = fz_realloc(obj->u.a.items, sizeof (fz_obj*) * obj->u.a.cap);
			for (i = obj->u.a.len ; i < obj->u.a.cap; i++)
				obj->u.a.items[i] = nil;
		}
		memmove(obj->u.a.items + 1, obj->u.a.items, obj->u.a.len * sizeof(fz_obj*));
		obj->u.a.items[0] = fz_keepobj(item);
		obj->u.a.len++;
	}
}

void
fz_freearray(fz_obj *obj)
{
	int i;

	assert(obj->kind == FZ_ARRAY);

	for (i = 0; i < obj->u.a.len; i++)
		if (obj->u.a.items[i])
			fz_dropobj(obj->u.a.items[i]);

	fz_free(obj->u.a.items);
	fz_free(obj);
}