summaryrefslogtreecommitdiff
path: root/src/cmd/gc/array.c
blob: 611fc9fbd406d2ee408585559d9715b3388384a7 (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
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#include <u.h>
#include <libc.h>
#include "go.h"

enum {
	DEFAULTCAPACITY = 16,
};

struct Array
{
	int32	length;  // number of elements
	int32	size;  // element size
	int32	capacity;  // size of data in elements
	char	*data;  // element storage
};

Array*
arraynew(int32 capacity, int32 size)
{
	Array *result;

	if(capacity < 0)
		fatal("arraynew: capacity %d is not positive", capacity);
	if(size < 0)
		fatal("arraynew: size %d is not positive\n", size);
	result = malloc(sizeof(*result));
	if(result == nil)
		fatal("arraynew: malloc failed\n");
	result->length = 0;
	result->size = size;
	result->capacity = capacity == 0 ? DEFAULTCAPACITY : capacity;
	result->data = malloc(result->capacity * result->size);
	if(result->data == nil)
		fatal("arraynew: malloc failed\n");
	return result;
}

void
arrayfree(Array *array)
{
	if(array == nil)
		return;
	free(array->data);
	free(array);
}

int32
arraylength(Array *array)
{
	return array->length;
}

void*
arrayget(Array *array, int32 index)
{
	if(array == nil)
		fatal("arrayget: array is nil\n");
	if(index < 0 || index >= array->length)
		fatal("arrayget: index %d is out of bounds for length %d\n", index, array->length);
	return array->data + index * array->size;
}

void
arrayset(Array *array, int32 index, void *element)
{
	if(array == nil)
		fatal("arrayset: array is nil\n");
	if(element == nil)
		fatal("arrayset: element is nil\n");
	if(index < 0 || index >= array->length)
		fatal("arrayget: index %d is out of bounds for length %d\n", index, array->length);
	memmove(array->data + index * array->size, element, array->size);
}

static void
ensurecapacity(Array *array, int32 capacity)
{
	int32 newcapacity;
	char *newdata;

	if(array == nil)
		fatal("ensurecapacity: array is nil\n");
	if(capacity < 0)
		fatal("ensurecapacity: capacity %d is not positive", capacity);
	if(capacity >= array->capacity) {
		newcapacity = capacity + (capacity >> 1);
		newdata = realloc(array->data, newcapacity * array->size);
		if(newdata == nil)
			fatal("ensurecapacity: realloc failed\n");
		array->capacity = newcapacity;
		array->data = newdata;
	}
}

void
arrayadd(Array *array, void *element)
{
	if(array == nil)
		fatal("arrayset: array is nil\n");
	if(element == nil)
		fatal("arrayset: element is nil\n");
	ensurecapacity(array, array->length + 1);
	array->length++;
	arrayset(array, array->length - 1, element);
}

void
arraysort(Array *array, int (*cmp)(const void*, const void*))
{
	qsort(array->data, array->length, array->size, cmp);
}