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