libbtc
bitcoinclibrary
Data Structures | Macros | Typedefs | Functions
vector.h File Reference
#include "btc.h"
#include <stdlib.h>
#include <sys/types.h>

Go to the source code of this file.

Data Structures

struct  vector
 

Macros

#define vector_idx(vec, idx)   ((vec)->data[(idx)])
 

Typedefs

typedef struct vector vector
 

Functions

LIBBTC_API vectorvector_new (size_t res, void(*free_f)(void *))
 
LIBBTC_API void vector_free (vector *vec, btc_bool free_array)
 
LIBBTC_API btc_bool vector_add (vector *vec, void *data)
 
LIBBTC_API btc_bool vector_remove (vector *vec, void *data)
 
LIBBTC_API void vector_remove_idx (vector *vec, size_t idx)
 
LIBBTC_API void vector_remove_range (vector *vec, size_t idx, size_t len)
 
LIBBTC_API btc_bool vector_resize (vector *vec, size_t newsz)
 
LIBBTC_API ssize_t vector_find (vector *vec, void *data)
 

Macro Definition Documentation

#define vector_idx (   vec,
  idx 
)    ((vec)->data[(idx)])

Typedef Documentation

typedef struct vector vector

Function Documentation

LIBBTC_API 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
LIBBTC_API 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
LIBBTC_API 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
LIBBTC_API 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
LIBBTC_API 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
LIBBTC_API void vector_remove_idx ( vector vec,
size_t  idx 
)

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
LIBBTC_API void vector_remove_range ( vector vec,
size_t  idx,
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
LIBBTC_API 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