Salad  1.0.15
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
vector.h
Go to the documentation of this file.
1 /*
2  * Salad - An assortment of useful C stuff
3  * Copyright (C) 2016 Lloyd Dilley
4  * http://www.dilley.me/
5  *
6  * This file is part of Salad.
7  *
8  * Salad is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version
11  * 3 of the License, or (at your option) any later version.
12  *
13  * Salad is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Salad. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
24 #ifndef VECTOR_H
25 #define VECTOR_H
26 
27 #include "salad/types.h"
28 
36 #define INITIAL_CAPACITY 16
37 
39 #define RESIZE_FACTOR 2
40 
42 typedef struct sld_vector
43 {
44  void **objects;
45  int capacity;
47 } sld_vector;
48 
61 
74 SLD_SSINT sld_vector_init_size(sld_vector *vector, int size);
75 
88 
101 
113 SLD_SSINT sld_vector_resize(sld_vector *vector, int new_size);
114 
126 SLD_SSINT sld_vector_add(sld_vector *vector, void *object);
127 
139 void *sld_vector_get(sld_vector *vector, int index);
140 
152 void sld_vector_set(sld_vector *vector, int index, void *object);
153 
164 void *sld_vector_pop(sld_vector *vector);
165 
176 void sld_vector_delete(sld_vector *vector, SLD_UINT index);
177 
187 void sld_vector_free(sld_vector *vector);
188 
191 #endif /* VECTOR_H */
void * sld_vector_get(sld_vector *vector, int index)
Returns an object from an sld_vector at index.
Definition: vector.c:118
int object_count
Definition: vector.h:46
SLD_SSINT sld_vector_add(sld_vector *vector, void *object)
Adds an object to a sld_vector.
Definition: vector.c:99
void sld_vector_delete(sld_vector *vector, SLD_UINT index)
Removes an object from a sld_vector at the specified index.
Definition: vector.c:142
SLD_UINT sld_vector_size(sld_vector *vector)
Returns the capacity of a sld_vector.
Definition: vector.c:74
struct sld_vector sld_vector
void sld_vector_free(sld_vector *vector)
Frees any memory allocated for an initialized sld_vector.
Definition: vector.c:165
void ** objects
Definition: vector.h:44
signed short int SLD_SSINT
Definition: types.h:47
int capacity
Definition: vector.h:45
SLD_SSINT sld_vector_init_size(sld_vector *vector, int size)
Initializes a sld_vector with the specified size.
Definition: vector.c:51
void * sld_vector_pop(sld_vector *vector)
Returns and removes the last object in a sld_vector.
Definition: vector.c:132
Definition: vector.h:42
SLD_SSINT sld_vector_init(sld_vector *vector)
Initializes a sld_vector.
Definition: vector.c:33
unsigned int SLD_UINT
Definition: types.h:56
void sld_vector_set(sld_vector *vector, int index, void *object)
Sets the data residing at the specified index in a sld_vector.
Definition: vector.c:125
SLD_UINT sld_vector_objects(sld_vector *vector)
Returns the number of objects contained within a sld_vector.
Definition: vector.c:69
SLD_SSINT sld_vector_resize(sld_vector *vector, int new_size)
Resizes a sld_vector to the specified size.
Definition: vector.c:79