cdcontainers  0.1.1
Library of data containers and collections for C programming language.
Data Structures | Functions
array.h File Reference

The cdc_array is a struct and functions that provide a dynamic array. More...

#include <cdcontainers/common.h>
#include <cdcontainers/status.h>
#include <assert.h>
#include <stdarg.h>
#include <stdbool.h>

Go to the source code of this file.

Data Structures

struct  cdc_array
 The cdc_array is service struct. More...
 

Functions

enum cdc_stat cdc_array_ctor (struct cdc_array **v, struct cdc_data_info *info)
 Constructs an empty array. More...
 
enum cdc_stat cdc_array_ctorl (struct cdc_array **v, struct cdc_data_info *info,...)
 Constructs an array, initialized by an variable number of pointers. The last pointer must be CDC_END. More...
 
enum cdc_stat cdc_array_ctorv (struct cdc_array **v, struct cdc_data_info *info, va_list args)
 Constructs an array, initialized by args. The last pointer must be CDC_END. More...
 
void cdc_array_dtor (struct cdc_array *v)
 Destroys the array. More...
 
static void * cdc_array_get (struct cdc_array *v, size_t index)
 Returns an element at index position in the array. More...
 
enum cdc_stat cdc_array_at (struct cdc_array *v, size_t index, void **elem)
 Writes to pointer an element from specified position in the array. Bounds checking is performed. More...
 
static void * cdc_array_front (struct cdc_array *v)
 Returns a first element in the array. More...
 
static void * cdc_array_back (struct cdc_array *v)
 Returns a last element in the array. More...
 
static void ** cdc_array_data (struct cdc_array *v)
 Returns a pointer to the data stored in the array. More...
 
enum cdc_stat cdc_array_reserve (struct cdc_array *v, size_t capacity)
 Attempts to allocate memory for at least size elements. If you know in advance how large the array will be, you should call this function to prevent reallocations and memory fragmentation. More...
 
static bool cdc_array_empty (struct cdc_array *v)
 Checks if the array has no elements. More...
 
static size_t cdc_array_size (struct cdc_array *v)
 Returns the number of elements in the array. More...
 
static size_t cdc_array_capacity (struct cdc_array *v)
 Returns the number of elements that the container has currently allocated space for. More...
 
enum cdc_stat cdc_array_shrink_to_fit (struct cdc_array *v)
 Requests the container to reduce its capacity to fit its size. More...
 
static void cdc_array_set (struct cdc_array *v, size_t index, void *value)
 Sets an element at index position to the value. The function is not called to free memory. More...
 
enum cdc_stat cdc_array_insert (struct cdc_array *v, size_t index, void *value)
 Inserts value at |index| position in the array. If index is 0, the value is prepended to the array. If index is cdc_array_size(), the value is appended to the array. More...
 
void cdc_array_erase (struct cdc_array *v, size_t index)
 Removes an element at index position in the arrray. More...
 
void cdc_array_clear (struct cdc_array *v)
 Removes all the elements from the array. More...
 
enum cdc_stat cdc_array_push_back (struct cdc_array *v, void *value)
 Inserts an element at the end of the array. More...
 
static void cdc_array_pop_back (struct cdc_array *v)
 Removes a last element in the array. More...
 
enum cdc_stat cdc_array_append (struct cdc_array *v, void **data, size_t len)
 Appends elements at the end of array. More...
 
enum cdc_stat cdc_array_append_move (struct cdc_array *v, struct cdc_array *other)
 Appends one array to the end of other array. More...
 
void cdc_array_swap (struct cdc_array *a, struct cdc_array *b)
 Swaps arrays. This operation is very fast and never fails. More...
 

Detailed Description

The cdc_array is a struct and functions that provide a dynamic array.

Example usage array:

// The MIT License (MIT)
// Copyright (c) 2017 Maksim Andrianov
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
// Program to find first n prime numbers.
#define CDC_USE_SHORT_NAMES
#include <stdio.h>
// This function based on the Sieve of Eratosthenes.
// https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
array_t *find_uint_prime_numbers(unsigned int n)
{
array_t *numbers = NULL;
if (array_ctor(&numbers, NULL /* data info */) != CDC_STATUS_OK)
return NULL;
array_reserve(numbers, n + 1);
for (unsigned int i = 0; i < n + 1; ++i)
array_push_back(numbers, CDC_FROM_UINT(i));
array_t *prime_numbers = NULL;
if (array_ctor(&prime_numbers, NULL /* data info */) != CDC_STATUS_OK) {
array_dtor(numbers);
return NULL;
}
for (unsigned int p = 2; p < n + 1; ++p) {
void *val = array_get(numbers, p);
if (CDC_TO_UINT(val) != 0) {
if (array_push_back(prime_numbers, val) != CDC_STATUS_OK) {
array_dtor(prime_numbers);
array_dtor(numbers);
return NULL;
}
for (unsigned int i = p * p; i < n + 1; i += p)
array_set(numbers, i, CDC_FROM_UINT(0));
}
}
array_dtor(numbers);
return prime_numbers;
}
int main(int argc, char **argv)
{
CDC_UNUSED(argc);
CDC_UNUSED(argv);
array_t *prime_numbers = find_uint_prime_numbers(1000);
if (!prime_numbers)
return EXIT_FAILURE;
for (unsigned i = 0; i < array_size(prime_numbers); ++i)
printf("%u ", CDC_TO_UINT(array_get(prime_numbers, i)));
printf("\n");
array_dtor(prime_numbers);
return EXIT_SUCCESS;
}
Author
Maksim Andrianov maksi.nosp@m.mand.nosp@m.riano.nosp@m.v1@y.nosp@m.andex.nosp@m..ru