libbtc
bitcoinclibrary
Functions
vector.c File Reference
#include "btc/vector.h"
#include <string.h>

Go to the source code of this file.

Functions

vectorvector_new (size_t res, void(*free_f)(void *))
 
static void vector_free_data (vector *vec)
 
void vector_free (vector *vec, btc_bool free_array)
 
static btc_bool vector_grow (vector *vec, size_t min_sz)
 
ssize_t vector_find (vector *vec, void *data)
 
btc_bool vector_add (vector *vec, void *data)
 
void vector_remove_range (vector *vec, size_t pos, size_t len)
 
void vector_remove_idx (vector *vec, size_t pos)
 
btc_bool vector_remove (vector *vec, void *data)
 
btc_bool vector_resize (vector *vec, size_t newsz)
 

Function Documentation

btc_bool vector_add ( vector vec,
void *  data 
)

Definition at line 113 of file vector.c.

References vector::alloc, vector::data, vector::len, and vector_grow().

Referenced by btc_script_get_ops(), btc_tx_add_p2pkh_hash160_out(), btc_tx_add_p2sh_hash160_out(), btc_tx_copy(), btc_tx_deserialize(), and deser_u256_vector().

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 }
static btc_bool vector_grow(vector *vec, size_t min_sz)
Definition: vector.c:83
void ** data
Definition: vector.h:41
size_t alloc
Definition: vector.h:43
size_t len
Definition: vector.h:42
ssize_t vector_find ( vector vec,
void *  data 
)

Definition at line 101 of file vector.c.

References vector::data, and vector::len.

Referenced by vector_remove().

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 }
void ** data
Definition: vector.h:41
size_t len
Definition: vector.h:42
void vector_free ( vector vec,
btc_bool  free_array 
)

Definition at line 71 of file vector.c.

References vector_free_data().

Referenced by btc_tx_copy(), btc_tx_free(), btc_tx_sighash(), and deser_u256_vector().

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 }
static void vector_free_data(vector *vec)
Definition: vector.c:51
static void vector_free_data ( vector vec)
static

Definition at line 51 of file vector.c.

References vector::alloc, vector::data, vector::elem_free_f, and vector::len.

Referenced by vector_free().

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 }
void ** data
Definition: vector.h:41
size_t alloc
Definition: vector.h:43
void(* elem_free_f)(void *)
Definition: vector.h:45
size_t len
Definition: vector.h:42
static btc_bool vector_grow ( vector vec,
size_t  min_sz 
)
static

Definition at line 83 of file vector.c.

References vector::alloc, and vector::data.

Referenced by vector_add(), and vector_resize().

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 }
void ** data
Definition: vector.h:41
size_t alloc
Definition: vector.h:43
vector* vector_new ( size_t  res,
void(*)(void *)  free_f 
)

Definition at line 31 of file vector.c.

References vector::alloc, vector::data, and vector::elem_free_f.

Referenced by btc_tx_copy(), btc_tx_new(), btc_tx_sighash(), and deser_u256_vector().

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 }
Definition: vector.h:39
void ** data
Definition: vector.h:41
size_t alloc
Definition: vector.h:43
void(* elem_free_f)(void *)
Definition: vector.h:45
btc_bool vector_remove ( vector vec,
void *  data 
)

Definition at line 144 of file vector.c.

References vector_find(), and vector_remove_idx().

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 }
ssize_t vector_find(vector *vec, void *data)
Definition: vector.c:101
void vector_remove_idx(vector *vec, size_t pos)
Definition: vector.c:139
void vector_remove_idx ( vector vec,
size_t  pos 
)

Definition at line 139 of file vector.c.

References vector_remove_range().

Referenced by vector_remove().

140 {
141  vector_remove_range(vec, pos, 1);
142 }
void vector_remove_range(vector *vec, size_t pos, size_t len)
Definition: vector.c:124
void vector_remove_range ( vector vec,
size_t  pos,
size_t  len 
)

Definition at line 124 of file vector.c.

References vector::data, vector::elem_free_f, const_buffer::len, and vector::len.

Referenced by btc_tx_sighash(), and vector_remove_idx().

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 }
size_t len
Definition: buffer.h:22
void ** data
Definition: vector.h:41
void(* elem_free_f)(void *)
Definition: vector.h:45
size_t len
Definition: vector.h:42
btc_bool vector_resize ( vector vec,
size_t  newsz 
)

Definition at line 154 of file vector.c.

References vector::data, vector::elem_free_f, const_buffer::len, vector::len, and vector_grow().

Referenced by btc_tx_sighash().

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 }
size_t len
Definition: buffer.h:22
static btc_bool vector_grow(vector *vec, size_t min_sz)
Definition: vector.c:83
void ** data
Definition: vector.h:41
void(* elem_free_f)(void *)
Definition: vector.h:45
size_t len
Definition: vector.h:42