1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
|
/*
Copyright 2024 Northern.tech AS
This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commercial Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/
#ifndef CFENGINE_SEQUENCE_H
#define CFENGINE_SEQUENCE_H
#include <stddef.h> // size_t
#include <sys/types.h> // ssize_t
#include <assert.h> // assert()
#include <stdio.h> // FILE
#include <stdbool.h>
/**
@brief Sequence data-structure.
This is an array-list loosely modeled on GSequence. It is a managed array of
void pointers and can be used to store arbitrary data. The array list will
auto-expand by a factor of EXPAND_FACTOR (e.g. 2) when necessary, but not
contract. Because sequence is content agnostic, it does not support the
usual copy semantics found in other CFEngine structures, such as
RList. Thus, appending an item to a Sequence may imply a transfer of
ownership. Clients that require copy semantics should therefore make sure
that elements are copied before they are appended. Some Sequence operations
may remove some or all of the elements held. In order to do so safely, it's
incumbent upon the client to supply the necessary item destructor to the
Sequence constructor. If the item destructor argument is NULL, Sequence will
not attempt to free the item memory held.
*/
typedef struct
{
void **data;
size_t length;
size_t capacity;
void (*ItemDestroy) (void *item);
} Seq;
static inline void *SeqAt(const Seq *seq, size_t i)
{
assert(seq != NULL);
assert(i < seq->length);
return seq->data[i];
}
/**
@brief Length of the sequence.
@note On NULL sequence return size 0.
@param seq [in] sequence.
@return Sequence length.
*/
size_t SeqLength(const Seq *seq);
/**
@brief Create a new Sequence
@param [in] initial_capacity Size of initial buffer to allocate for item pointers.
@param [in] ItemDestroy Optional item destructor to clean up memory when needed.
@return A pointer to the created Sequence
*/
Seq *SeqNew(size_t initial_capacity, void (*ItemDestroy) ());
/**
@brief Destroy an existing Sequence
@param [in] seq The Sequence to destroy.
*/
void SeqDestroy(Seq *seq);
/**
@brief Destroy an existing Sequence without destroying its items.
@param [in] seq The Sequence to destroy.
*/
void SeqSoftDestroy(Seq *seq);
/**
@brief
Function to compare two items in a Sequence.
@retval -1 if the first argument is smaller than the second
@retval 0 if the arguments are equal
@retval 1 if the first argument is bigger than the second
*/
typedef int (*SeqItemComparator) (const void *, const void *, void *user_data);
/**
@brief Wrapper of the standard library function strcmp.
Used to avoid cast-function-type compiler warnings when
casting strcmp to (SeqItemComparator) in sequence functions.
@param [in] s1 The string being compared to the s2 string
@param [in] s2 The string that s1 is compared to
@param [in] user_data This parameter is the ignored user_data that the SeqItemComparator
expects
@return 0 if s1 and s2 strings are equal
@return negative if s1 is less than s2
@return positive if s1 is greater than s2
*/
int StrCmpWrapper(const void *s1, const void *s2, void *user_data);
/**
* Replace value at #index.
*
* @warning Destroys the original item at #index! See SeqSoftSet().
*/
void SeqSet(Seq *set, size_t index, void *item);
/**
* Set value at #index.
*
* @note Make sure the original item at #index is destroyed.
*/
void SeqSoftSet(Seq *set, size_t index, void *item);
/**
@brief Append a new item to the Sequence
@param seq [in] The Sequence to append to.
@param item [in] The item to append. Note that this item may be passed to the item destructor specified in the constructor.
*/
void SeqAppend(Seq *seq, void *item);
/**
@brief Append a new item to the Sequence if it's not already present in the Sequence.
@note This calls SeqLookup() and thus linearly searches through the sequence.
@param seq [in] The Sequence to append to.
@param item [in] The item to append. Note that this item will be passed to the item destructor specified in the constructor.
Either immediately if the same item (according to Compare()) is found in the Sequence or once the Sequence
is destroyed with SeqDestroy().
*/
void SeqAppendOnce(Seq *seq, void *item, SeqItemComparator Compare);
/**
* @brief Append a sequence to this sequence. Only copies pointers.
* @param seq Sequence to append to
* @param items Sequence to copy pointers from.
*/
void SeqAppendSeq(Seq *seq, const Seq *items);
/**
@brief Linearly searches through the sequence and return the first item considered equal to the specified key.
@param seq [in] The Sequence to search.
@param key [in] The item to compare against.
@param compare [in] Comparator function to use. An item matches if the function returns 0.
@returns A pointer to the found item, or NULL if not found.
*/
void *SeqLookup(Seq *seq, const void *key, SeqItemComparator Compare);
/**
* @brief Performs a binary search looking for the item matching the given key.
* It is the programmer's responsibility to make sure that the sequence is already sorted.
* @param seq [in] The Sequence to search.
* @param key [in] The item to compare against.
* @param compare [in] Comparator function to use (return value has strcmp semantics).
* @returns A pointer to the found item, or NULL if not found.
*/
void *SeqBinaryLookup(Seq *seq, const void *key, SeqItemComparator Compare);
/**
@brief Linearly searches through the sequence and returns the index of the first matching object, or -1 if it doesn't exist.
*/
ssize_t SeqIndexOf(Seq *seq, const void *key, SeqItemComparator Compare);
/**
* @brief Performs a binary search looking for the item matching the given key.
* It is the programmer's responsibility to make sure that the sequence is already sorted.
* @param seq [in] The Sequence to search.
* @param key [in] The item to compare against.
* @param compare [in] Comparator function to use (return value has strcmp semantics).
* @returns The index of the item, or -1 if it is not found.
*/
ssize_t SeqBinaryIndexOf(Seq *seq, const void *key, SeqItemComparator Compare);
/**
@brief Remove an inclusive range of items in the Sequence. A single item may be removed by specifying start = end.
@param seq [in] The Sequence to remove from.
@param start [in] Index of the first element to remove
@param end [in] Index of the last element to remove.
*/
void SeqRemoveRange(Seq *seq, size_t start, size_t end);
/**
@brief Remove a single item in the sequence
*/
void SeqRemove(Seq *seq, size_t index);
/**
@brief Sort a Sequence according to the given item comparator function
@param compare [in] The comparator function used for sorting.
@param user_data [in] Pointer passed to the comparator function
*/
void SeqSort(Seq *seq, SeqItemComparator compare, void *user_data);
/**
@brief Returns a soft copy of the sequence sorted according to the given item comparator function.
@param compare [in] The comparator function used for sorting.
@param user_data [in] Pointer passed to the comparator function
*/
Seq *SeqSoftSort(const Seq *seq, SeqItemComparator compare, void *user_data);
/**
@brief Remove an inclusive range of item handles in the Sequence. A single item may be removed by specifying start = end.
@param seq [in] The Sequence to remove from.
@param start [in] Index of the first element to remove
@param end [in] Index of the last element to remove.
*/
void SeqSoftRemoveRange(Seq *seq, size_t start, size_t end);
/**
@brief Remove a single item handle from the sequence
*/
void SeqSoftRemove(Seq *seq, size_t index);
/**
@brief Reverses the order of the sequence
*/
void SeqReverse(Seq *seq);
/**
@brief Split a sequence in two at a given index.
Elements before the split are kept in original sequence,
elements after the split are moved to a new sequence,
which is returned. The original, the new, and the modified sequence
may all be empty.
Items in sequence are not reallocated, they are moved and the new
sequnce has the same destroy function as the original.
@param original [in] The Sequence to split in two (will be modified)
@param index [in] Index of split, or how many elements to keep in original
@return New sequence containing the elements removed from original
*/
Seq *SeqSplit(Seq *original, size_t index);
/**
* @brief Shuffle the sequence by randomly switching positions of the pointers
* @param seq
* @param seed Seed value for the PRNG
*/
void SeqShuffle(Seq *seq, unsigned int seed);
/**
* @brief Remove all elements in sequence
* @param seq
*/
void SeqClear(Seq *seq);
/**
@brief Get soft copy of sequence according to specified range
@param [in] seq Sequence select from
@param [in] start Start index of sub sequence.
@param [in] end End index which will be included into.
@return A pointer to sub sequence, NULL on error.
*/
Seq *SeqGetRange(const Seq *seq, size_t start, size_t end);
/**
* @brief Get the data segment of the sequence
* @param [in] seq Sequence to get the data segment of
* @return An array of pointers to data stored in the sequence
* @warning The returned array is not guaranteed to be %NULL-terminated unless %NULL was appended to
* the sequence.
*/
void *const *SeqGetData(const Seq *seq);
/**
* @brief Custom filter callback function used with SeqFilter.
* @param element Element in sequence to filter.
* @return True if element should be filtered, otherwise false.
*/
typedef bool SeqFilterFn(void *element);
/**
* @brief Filter elements on sequence.
* @param seq Sequence to filter elements from.
* @param filter Callback filter function.
* @return A pointer to the sequence itself (for convenience).
*/
Seq *SeqFilter(Seq *seq, SeqFilterFn filter);
/**
* @brief Filter NULL pointers from Sequence.
* @param seq Sequence to filter NULL pointers from.
*/
void SeqRemoveNulls(Seq *seq);
Seq *SeqFromArgv(int argc, const char *const *argv);
#endif
|