libbtc
bitcoinclibrary
vector.h
Go to the documentation of this file.
1 /*
2 
3  The MIT License (MIT)
4 
5  Copyright (c) 2015 Jonas Schnelli
6 
7  Permission is hereby granted, free of charge, to any person obtaining
8  a copy of this software and associated documentation files (the "Software"),
9  to deal in the Software without restriction, including without limitation
10  the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  and/or sell copies of the Software, and to permit persons to whom the
12  Software is furnished to do so, subject to the following conditions:
13 
14  The above copyright notice and this permission notice shall be included
15  in all copies or substantial portions of the Software.
16 
17  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
21  OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23  OTHER DEALINGS IN THE SOFTWARE.
24 
25 */
26 
27 #ifndef __LIBBTC_VECTOR_H__
28 #define __LIBBTC_VECTOR_H__
29 
30 #include "btc.h"
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 #include <stdlib.h>
37 #include <sys/types.h>
38 
39 typedef struct vector
40 {
41  void** data; /* array of pointers */
42  size_t len; /* array element count */
43  size_t alloc; /* allocated array elements */
44 
45  void (*elem_free_f)(void*);
46 } vector;
47 
48 LIBBTC_API vector* vector_new(size_t res, void (*free_f)(void*));
49 LIBBTC_API void vector_free(vector* vec, btc_bool free_array);
50 
51 LIBBTC_API btc_bool vector_add(vector* vec, void* data);
52 LIBBTC_API btc_bool vector_remove(vector* vec, void* data);
53 LIBBTC_API void vector_remove_idx(vector* vec, size_t idx);
54 LIBBTC_API void vector_remove_range(vector* vec, size_t idx, size_t len);
55 LIBBTC_API btc_bool vector_resize(vector* vec, size_t newsz);
56 
57 LIBBTC_API ssize_t vector_find(vector* vec, void* data);
58 
59 #define vector_idx(vec, idx) ((vec)->data[(idx)])
60 
61 #ifdef __cplusplus
62 }
63 #endif
64 
65 #endif /* __LIBBTC_VECTOR_H__ */
struct vector vector
uint8_t btc_bool
Definition: btc.h:34
Definition: vector.h:39
LIBBTC_API btc_bool vector_remove(vector *vec, void *data)
Definition: vector.c:144
#define LIBBTC_API
Definition: btc.h:59
void ** data
Definition: vector.h:41
size_t alloc
Definition: vector.h:43
LIBBTC_API void vector_remove_idx(vector *vec, size_t idx)
Definition: vector.c:139
LIBBTC_API btc_bool vector_resize(vector *vec, size_t newsz)
Definition: vector.c:154
LIBBTC_API vector * vector_new(size_t res, void(*free_f)(void *))
Definition: vector.c:31
void(* elem_free_f)(void *)
Definition: vector.h:45
LIBBTC_API void vector_remove_range(vector *vec, size_t idx, size_t len)
Definition: vector.c:124
LIBBTC_API void vector_free(vector *vec, btc_bool free_array)
Definition: vector.c:71
LIBBTC_API btc_bool vector_add(vector *vec, void *data)
Definition: vector.c:113
size_t len
Definition: vector.h:42
LIBBTC_API ssize_t vector_find(vector *vec, void *data)
Definition: vector.c:101