All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 void
82 as_vector_init(as_vector* vector, uint32_t item_size, uint32_t capacity);
83 
84 /**
85  * Create a heap allocated as_vector, with item storage on the heap.
86  */
87 as_vector*
88 as_vector_create(uint32_t item_size, uint32_t capacity);
89 
90 /**
91  * Free vector.
92  */
93 void
95 
96 /**
97  * Empty vector without altering data.
98  */
99 static inline void
101 {
102  vector->size = 0;
103 }
104 
105 /**
106  * Get pointer to item given index.
107  */
108 static inline void*
109 as_vector_get(as_vector* vector, uint32_t index)
110 {
111  return (void *) ((byte *)vector->list + (vector->item_size * index));
112 }
113 
114 /**
115  * Get pointer to item pointer given index.
116  */
117 static inline void*
118 as_vector_get_ptr(as_vector* vector, uint32_t index)
119 {
120  return *(void**) ((byte *)vector->list + (vector->item_size * index));
121 }
122 
123 /**
124  * Double vector capacity.
125  */
126 void
128 
129 /**
130  * Set item in vector.
131  */
132 static inline void
133 as_vector_set(as_vector* vector, uint32_t index, void* value)
134 {
135  memcpy((byte *)vector->list + (index * vector->item_size), value, vector->item_size);
136 }
137 
138 /**
139  * Append item to vector.
140  */
141 static inline void
142 as_vector_append(as_vector* vector, void* value)
143 {
144  if (vector->size >= vector->capacity) {
146  }
147  memcpy((byte *)vector->list + (vector->size * vector->item_size), value, vector->item_size);
148  vector->size++;
149 }
150 
151 /**
152  * Append item to vector if it doesn't already exist.
153  */
154 bool
155 as_vector_append_unique(as_vector* vector, void* value);
156 
157 /**
158  * Move item row position in vector.
159  */
160 static inline void
161 as_vector_move(as_vector* vector, uint32_t source, uint32_t target)
162 {
163  memcpy((byte *)vector->list + (target * vector->item_size), (byte *)vector->list + (source * vector->item_size), vector->item_size);
164 }
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:118
static void as_vector_clear(as_vector *vector)
Definition: as_vector.h:100
static void as_vector_set(as_vector *vector, uint32_t index, void *value)
Definition: as_vector.h:133
static void * as_vector_get(as_vector *vector, uint32_t index)
Definition: as_vector.h:109
static void as_vector_append(as_vector *vector, void *value)
Definition: as_vector.h:142
static void as_vector_move(as_vector *vector, uint32_t source, uint32_t target)
Definition: as_vector.h:161
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)