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-2015 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 <citrusleaf/alloc.h>
20 #include <citrusleaf/cf_types.h>
21 #include <string.h>
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /******************************************************************************
28  * TYPES
29  *****************************************************************************/
30 
31 /**
32  * A fast, non thread safe dynamic array implementation.
33  * as_vector is not part of the generic as_val family.
34  */
35 typedef struct as_vector_s {
36  /**
37  * The items of the vector.
38  */
39  void* list;
40 
41  /**
42  * The total number items allocated.
43  */
44  uint32_t capacity;
45 
46  /**
47  * The number of items used.
48  */
49  uint32_t size;
50 
51  /**
52  * The size of a single item.
53  */
54  uint32_t item_size;
55 
56  /**
57  * Internal vector flags.
58  */
59  uint32_t flags;
60 } as_vector;
61 
62 /******************************************************************************
63  * MACROS
64  ******************************************************************************/
65 
66 /**
67  * Initialize a stack allocated as_vector, with item storage on the stack.
68  * as_vector_inita() will transfer stack memory to the heap if a resize is
69  * required.
70  */
71 #define as_vector_inita(__vector, __item_size, __capacity)\
72 (__vector)->list = alloca((__capacity) * (__item_size));\
73 (__vector)->capacity = __capacity;\
74 (__vector)->item_size = __item_size;\
75 (__vector)->size = 0;\
76 (__vector)->flags = 0;
77 
78 /*******************************************************************************
79  * INSTANCE FUNCTIONS
80  ******************************************************************************/
81 
82 /**
83  * Initialize a stack allocated as_vector, with item storage on the heap.
84  */
85 void
86 as_vector_init(as_vector* vector, uint32_t item_size, uint32_t capacity);
87 
88 /**
89  * Create a heap allocated as_vector, with item storage on the heap.
90  */
91 as_vector*
92 as_vector_create(uint32_t item_size, uint32_t capacity);
93 
94 /**
95  * Free vector.
96  */
97 void
99 
100 /**
101  * Empty vector without altering data.
102  */
103 static inline void
105 {
106  vector->size = 0;
107 }
108 
109 /**
110  * Get pointer to item given index.
111  */
112 static inline void*
113 as_vector_get(as_vector* vector, uint32_t index)
114 {
115  return (void *) ((byte *)vector->list + (vector->item_size * index));
116 }
117 
118 /**
119  * Get pointer to item pointer given index.
120  */
121 static inline void*
122 as_vector_get_ptr(as_vector* vector, uint32_t index)
123 {
124  return *(void**) ((byte *)vector->list + (vector->item_size * index));
125 }
126 
127 /**
128  * Double vector capacity.
129  */
130 void
132 
133 /**
134  * Set item in vector.
135  */
136 static inline void
137 as_vector_set(as_vector* vector, uint32_t index, void* value)
138 {
139  memcpy((byte *)vector->list + (index * vector->item_size), value, vector->item_size);
140 }
141 
142 /**
143  * Append item to vector.
144  */
145 static inline void
146 as_vector_append(as_vector* vector, void* value)
147 {
148  if (vector->size >= vector->capacity) {
150  }
151  memcpy((byte *)vector->list + (vector->size * vector->item_size), value, vector->item_size);
152  vector->size++;
153 }
154 
155 /**
156  * Append item to vector if it doesn't already exist.
157  */
158 bool
159 as_vector_append_unique(as_vector* vector, void* value);
160 
161 /**
162  * Move item row position in vector.
163  */
164 static inline void
165 as_vector_move(as_vector* vector, uint32_t source, uint32_t target)
166 {
167  memcpy((byte *)vector->list + (target * vector->item_size), (byte *)vector->list + (source * vector->item_size), vector->item_size);
168 }
169 
170 #ifdef __cplusplus
171 } // end extern "C"
172 #endif
void as_vector_init(as_vector *vector, uint32_t item_size, uint32_t capacity)
as_vector * as_vector_create(uint32_t item_size, uint32_t capacity)
static void * as_vector_get_ptr(as_vector *vector, uint32_t index)
Definition: as_vector.h:122
static void as_vector_clear(as_vector *vector)
Definition: as_vector.h:104
static void as_vector_set(as_vector *vector, uint32_t index, void *value)
Definition: as_vector.h:137
static void * as_vector_get(as_vector *vector, uint32_t index)
Definition: as_vector.h:113
static void as_vector_append(as_vector *vector, void *value)
Definition: as_vector.h:146
static void as_vector_move(as_vector *vector, uint32_t source, uint32_t target)
Definition: as_vector.h:165
void as_vector_destroy(as_vector *vector)
void * list
Definition: as_vector.h:39
bool as_vector_append_unique(as_vector *vector, void *value)
uint32_t capacity
Definition: as_vector.h:44
uint32_t item_size
Definition: as_vector.h:54
uint32_t flags
Definition: as_vector.h:59
uint32_t size
Definition: as_vector.h:49
void as_vector_increase_capacity(as_vector *vector)