libbtc
bitcoinclibrary
vector.c
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 #include "btc/vector.h"
28 
29 #include <string.h>
30 
31 vector* vector_new(size_t res, void (*free_f)(void*))
32 {
33  vector* vec = calloc(1, sizeof(vector));
34  if (!vec)
35  return NULL;
36 
37  vec->alloc = 8;
38  while (vec->alloc < res)
39  vec->alloc *= 2;
40 
41  vec->elem_free_f = free_f;
42  vec->data = malloc(vec->alloc * sizeof(void*));
43  if (!vec->data) {
44  free(vec);
45  return NULL;
46  }
47 
48  return vec;
49 }
50 
51 static void vector_free_data(vector* vec)
52 {
53  if (!vec->data)
54  return;
55 
56  if (vec->elem_free_f) {
57  unsigned int i;
58  for (i = 0; i < vec->len; i++)
59  if (vec->data[i]) {
60  vec->elem_free_f(vec->data[i]);
61  vec->data[i] = NULL;
62  }
63  }
64 
65  free(vec->data);
66  vec->data = NULL;
67  vec->alloc = 0;
68  vec->len = 0;
69 }
70 
71 void vector_free(vector* vec, btc_bool free_array)
72 {
73  if (!vec)
74  return;
75 
76  if (free_array)
77  vector_free_data(vec);
78 
79  memset(vec, 0, sizeof(*vec));
80  free(vec);
81 }
82 
83 static btc_bool vector_grow(vector* vec, size_t min_sz)
84 {
85  size_t new_alloc = vec->alloc;
86  while (new_alloc < min_sz)
87  new_alloc *= 2;
88 
89  if (vec->alloc == new_alloc)
90  return true;
91 
92  void* new_data = realloc(vec->data, new_alloc * sizeof(void*));
93  if (!new_data)
94  return false;
95 
96  vec->data = new_data;
97  vec->alloc = new_alloc;
98  return true;
99 }
100 
101 ssize_t vector_find(vector* vec, void* data)
102 {
103  if (vec && vec->len) {
104  size_t i;
105  for (i = 0; i < vec->len; i++)
106  if (vec->data[i] == data)
107  return (ssize_t)i;
108  }
109 
110  return -1;
111 }
112 
113 btc_bool vector_add(vector* vec, void* data)
114 {
115  if (vec->len == vec->alloc)
116  if (!vector_grow(vec, vec->len + 1))
117  return false;
118 
119  vec->data[vec->len] = data;
120  vec->len++;
121  return true;
122 }
123 
124 void vector_remove_range(vector* vec, size_t pos, size_t len)
125 {
126  if (!vec || ((pos + len) > vec->len))
127  return;
128 
129  if (vec->elem_free_f) {
130  unsigned int i, count;
131  for (i = pos, count = 0; count < len; i++, count++)
132  vec->elem_free_f(vec->data[i]);
133  }
134 
135  memmove(&vec->data[pos], &vec->data[pos + len], (vec->len - pos - len) * sizeof(void*));
136  vec->len -= len;
137 }
138 
139 void vector_remove_idx(vector* vec, size_t pos)
140 {
141  vector_remove_range(vec, pos, 1);
142 }
143 
144 btc_bool vector_remove(vector* vec, void* data)
145 {
146  ssize_t idx = vector_find(vec, data);
147  if (idx < 0)
148  return false;
149 
150  vector_remove_idx(vec, idx);
151  return true;
152 }
153 
154 btc_bool vector_resize(vector* vec, size_t newsz)
155 {
156  unsigned int i;
157 
158  /* same size */
159  if (newsz == vec->len)
160  return true;
161 
162  /* truncate */
163  else if (newsz < vec->len) {
164  size_t del_count = vec->len - newsz;
165 
166  for (i = (vec->len - del_count); i < vec->len; i++) {
167  if (vec->elem_free_f)
168  vec->elem_free_f(vec->data[i]);
169  vec->data[i] = NULL;
170  }
171 
172  vec->len = newsz;
173  return true;
174  }
175 
176  /* last possibility: grow */
177  if (!vector_grow(vec, newsz))
178  return false;
179 
180  /* set new elements to NULL */
181  for (i = vec->len; i < newsz; i++)
182  vec->data[i] = NULL;
183 
184  return true;
185 }
vector * vector_new(size_t res, void(*free_f)(void *))
Definition: vector.c:31
uint8_t btc_bool
Definition: btc.h:34
size_t len
Definition: buffer.h:22
Definition: vector.h:39
static btc_bool vector_grow(vector *vec, size_t min_sz)
Definition: vector.c:83
void ** data
Definition: vector.h:41
void vector_remove_range(vector *vec, size_t pos, size_t len)
Definition: vector.c:124
size_t alloc
Definition: vector.h:43
ssize_t vector_find(vector *vec, void *data)
Definition: vector.c:101
static void vector_free_data(vector *vec)
Definition: vector.c:51
void(* elem_free_f)(void *)
Definition: vector.h:45
btc_bool vector_add(vector *vec, void *data)
Definition: vector.c:113
btc_bool vector_remove(vector *vec, void *data)
Definition: vector.c:144
size_t len
Definition: vector.h:42
void vector_free(vector *vec, btc_bool free_array)
Definition: vector.c:71
btc_bool vector_resize(vector *vec, size_t newsz)
Definition: vector.c:154
void vector_remove_idx(vector *vec, size_t pos)
Definition: vector.c:139