naev 0.12.5
intlist.h
1/*
2 * This code was initially authored by the Stackoverflow user dragon-energy and
3 * posted under following page:
4 * https://stackoverflow.com/questions/41946007/efficient-and-well-explained-implementation-of-a-quadtree-for-2d-collision-det
5 *
6 * As for the license, the author has kindly noted:
7 *
8 * "Oh and feel free to use this code I post however you want, even for
9 * commercial projects. I would really love it if people let me know if they
10 * find it useful, but do as you wish."
11 *
12 * And generally all Stackoverflow-posted code is by default licensed with CC
13 * BY-SA 4.0: https://creativecommons.org/licenses/by-sa/4.0/
14 */
15#pragma once
16
17typedef struct IntList IntList;
18enum { il_fixed_cap = 128 };
19
20struct IntList {
21 // Stores a fixed-size buffer in advance to avoid requiring
22 // a heap allocation until we run out of space.
23 int fixed[il_fixed_cap];
24
25 // Points to the buffer used by the list. Initially this will
26 // point to 'fixed'.
27 int *data;
28
29 // Stores how many integer fields each element has.
30 int num_fields;
31
32 // Stores the number of elements in the list.
33 int num;
34
35 // Stores the capacity of the array.
36 int cap;
37
38 // Stores an index to the free element or -1 if the free list
39 // is empty.
40 int free_element;
41};
42
43// ---------------------------------------------------------------------------------
44// List Interface
45// ---------------------------------------------------------------------------------
46// Creates a new list of elements which each consist of integer fields.
47// 'num_fields' specifies the number of integer fields each element has.
48void il_create( IntList *il, int num_fields );
49
50// Destroys the specified list.
51void il_destroy( IntList *il );
52
53// Returns the number of elements in the list.
54int il_size( const IntList *il );
55
56// Returns the value of the specified field for the nth element.
57int il_get( const IntList *il, int n, int field );
58
59// Sets the value of the specified field for the nth element.
60void il_set( IntList *il, int n, int field, int val );
61
62// Clears the specified list, making it empty.
63void il_clear( IntList *il );
64
65// ---------------------------------------------------------------------------------
66// Stack Interface (do not mix with free list usage; use one or the other)
67// ---------------------------------------------------------------------------------
68// Inserts an element to the back of the list and returns an index to it.
69int il_push_back( IntList *il );
70
71// Removes the element at the back of the list.
72void il_pop_back( IntList *il );
73
74// ---------------------------------------------------------------------------------
75// Free List Interface (do not mix with stack usage; use one or the other)
76// ---------------------------------------------------------------------------------
77// Inserts an element to a vacant position in the list and returns an index to
78// it.
79int il_insert( IntList *il );
80
81// Removes the nth element in the list.
82void il_erase( IntList *il, int n );