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