All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
as_vector.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2018 Aerospike, Inc.
3  *
4  * Portions may be licensed to Aerospike, Inc. under one or more contributor
5  * license agreements.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8  * use this file except in compliance with the License. You may obtain a copy of
9  * the License at http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations under
15  * the License.
16  */
17 #pragma once
18 
19 #include <aerospike/as_std.h>
20 #include <string.h>
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 /******************************************************************************
27  * TYPES
28  *****************************************************************************/
29 
30 /**
31  * A fast, non thread safe dynamic array implementation.
32  * as_vector is not part of the generic as_val family.
33  */
34 typedef struct as_vector_s {
35  /**
36  * The items of the vector.
37  */
38  void* list;
39 
40  /**
41  * The total number items allocated.
42  */
43  uint32_t capacity;
44 
45  /**
46  * The number of items used.
47  */
48  uint32_t size;
49 
50  /**
51  * The size of a single item.
52  */
53  uint32_t item_size;
54 
55  /**
56  * Internal vector flags.
57  */
58  uint32_t flags;
59 } as_vector;
60 
61 /******************************************************************************
62  * MACROS
63  ******************************************************************************/
64 
65 /**
66  * Initialize a stack allocated as_vector, with item storage on the stack.
67  * as_vector_inita() will transfer stack memory to the heap if a resize is
68  * required.
69  */
70 #define as_vector_inita(__vector, __item_size, __capacity)\
71 (__vector)->list = alloca((__capacity) * (__item_size));\
72 (__vector)->capacity = __capacity;\
73 (__vector)->item_size = __item_size;\
74 (__vector)->size = 0;\
75 (__vector)->flags = 0;
76 
77 /*******************************************************************************
78  * INSTANCE FUNCTIONS
79  ******************************************************************************/
80 
81 /**
82  * Initialize a stack allocated as_vector, with item storage on the heap.
83  */
84 AS_EXTERN void
85 as_vector_init(as_vector* vector, uint32_t item_size, uint32_t capacity);
86 
87 /**
88  * Create a heap allocated as_vector, with item storage on the heap.
89  */
91 as_vector_create(uint32_t item_size, uint32_t capacity);
92 
93 /**
94  * Free vector.
95  */
96 AS_EXTERN void
98 
99 /**
100  * Empty vector without altering data.
101  */
102 static inline void
104 {
105  vector->size = 0;
106 }
107 
108 /**
109  * Get pointer to item given index.
110  */
111 static inline void*
112 as_vector_get(as_vector* vector, uint32_t index)
113 {
114  return (void *) ((uint8_t *)vector->list + (vector->item_size * index));
115 }
116 
117 /**
118  * Get pointer to item pointer given index.
119  */
120 static inline void*
121 as_vector_get_ptr(as_vector* vector, uint32_t index)
122 {
123  return *(void**) ((uint8_t *)vector->list + (vector->item_size * index));
124 }
125 
126 /**
127  * Double vector capacity.
128  */
129 AS_EXTERN void
131 
132 /**
133  * Set item in vector.
134  */
135 static inline void
136 as_vector_set(as_vector* vector, uint32_t index, void* value)
137 {
138  memcpy((uint8_t *)vector->list + (index * vector->item_size), value, vector->item_size);
139 }
140 
141 /**
142  * Append item to vector.
143  */
144 static inline void
145 as_vector_append(as_vector* vector, void* value)
146 {
147  if (vector->size >= vector->capacity) {
149  }
150  memcpy((uint8_t *)vector->list + (vector->size * vector->item_size), value, vector->item_size);
151  vector->size++;
152 }
153 
154 /**
155  * Append item to vector if it doesn't already exist.
156  */
157 AS_EXTERN bool
158 as_vector_append_unique(as_vector* vector, void* value);
159 
160 /**
161  * Return shallow heap copy of vector.
162  */
163 AS_EXTERN void*
164 as_vector_to_array(as_vector* vector, uint32_t* size);
165 
166 /**
167  * Reserve a new slot in the vector. Increase capacity if necessary.
168  * Return reference to item. The item is initialized to zeroes.
169  */
170 static inline void*
172 {
173  if (vector->size >= vector->capacity) {
175  }
176  void* item = (uint8_t *)vector->list + (vector->size * vector->item_size);
177  memset(item, 0, vector->item_size);
178  vector->size++;
179  return item;
180 }
181 
182 /**
183  * Remove item from vector.
184  */
185 AS_EXTERN bool
186 as_vector_remove(as_vector* vector, uint32_t index);
187 
188 #ifdef __cplusplus
189 } // end extern "C"
190 #endif
static void * as_vector_reserve(as_vector *vector)
Definition: as_vector.h:171
AS_EXTERN void as_vector_destroy(as_vector *vector)
static void * as_vector_get_ptr(as_vector *vector, uint32_t index)
Definition: as_vector.h:121
static void as_vector_clear(as_vector *vector)
Definition: as_vector.h:103
static void as_vector_set(as_vector *vector, uint32_t index, void *value)
Definition: as_vector.h:136
AS_EXTERN as_vector * as_vector_create(uint32_t item_size, uint32_t capacity)
AS_EXTERN void as_vector_init(as_vector *vector, uint32_t item_size, uint32_t capacity)
static void * as_vector_get(as_vector *vector, uint32_t index)
Definition: as_vector.h:112
static void as_vector_append(as_vector *vector, void *value)
Definition: as_vector.h:145
#define AS_EXTERN
Definition: as_std.h:25
void * list
Definition: as_vector.h:38
AS_EXTERN void * as_vector_to_array(as_vector *vector, uint32_t *size)
AS_EXTERN void as_vector_increase_capacity(as_vector *vector)
uint32_t capacity
Definition: as_vector.h:43
uint32_t item_size
Definition: as_vector.h:53
uint32_t flags
Definition: as_vector.h:58
AS_EXTERN bool as_vector_append_unique(as_vector *vector, void *value)
uint32_t size
Definition: as_vector.h:48
AS_EXTERN bool as_vector_remove(as_vector *vector, uint32_t index)