aboutsummaryrefslogtreecommitdiff
path: root/include/array.h
blob: c382f1b19811a1cb62c40738d79bbea19deecd37 (about) (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
/*
  ihex
  Copyright (C) 2025 Thomas Albers Raviola <thomas@thomaslabs.org>

  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 <https://www.gnu.org/licenses/>.
 */

#ifndef ARRAY_H
#define ARRAY_H

#include <stddef.h>

struct array {
	size_t length;
	size_t elem_size;
	size_t capacity;
	void *data;
};

void
init_array(struct array *arr, size_t elem_size);

void
free_array(struct array *arr);

void *
array_append(struct array *arr, const void *data);

void
array_remove(struct array *arr, size_t n, void *dest);

void
array_sort(struct array *arr, int (*compar)(const void *, const void *));

void *
array_search(struct array *arr, const void *key,
	     int (*compar)(const void *, const void *));

static inline void *
array_elt(struct array *arr, size_t n)
{
	return (void *)((char *)arr->data + n * arr->elem_size);
}

#define ARRAY_ELT(ARRAY, TYPE, N) ((TYPE *)(ARRAY)->data)[N]

#endif /* ARRAY_H */