Loading...
Searching...
No Matches
vector.c File Reference
`#include "arc/std/vector.h"`
`#include "arc/std/bool.h"`
`#include "arc/std/errno.h"`
`#include <stdint.h>`
`#include <stdlib.h>`

Go to the source code of this file.

## Data Structures

struct | ARC_Vector
| a private dynamic array type More...

## Functions

ARC_Bool | ARC_Vector_CompareDataDefaultFn (void *dataA, void *dataB)
void | ARC_Vector_Create (ARC_Vector **vector, ARC_Vector_CompareDataFn *compareDataFn, ARC_Vector_DestroyDataFn *destroyDataFn)
| creates an ARC_Vector which is an "expandable" array
void | ARC_Vector_Destroy (ARC_Vector *vector)
| destroys an ARC_Vector
void | ARC_Vector_Add (ARC_Vector *vector, void *data)
| adds an item to an ARC_Vector
void | ARC_Vector_Remove (ARC_Vector *vector, void *data)
| removes an item from a matching item in an ARC_Vector
void | ARC_Vector_RemoveIndex (ARC_Vector *vector, uint32_t index)
| removes an item from an ARC_Vector at an index
void | ARC_Vector_Clear (ARC_Vector *vector)
| clears all items from a vector
uint32_t | ARC_Vector_GetSize (ARC_Vector *vector)
| gets the current size of an ARC_Vector as an unsigned 32 bit integer
void * | ARC_Vector_Get (ARC_Vector *vector, uint32_t index)
| gets an item from an ARC_Vector at a position index

## Function Documentation

## ARC_Vector_Add()

void ARC_Vector_Add | ( | ARC_Vector * | vector,
|  | void * | data )

adds an item to an ARC_Vector

Note
  this will error if you add more than 4,294,967,295 items (the max value of an unsigned int 32)

Parameters

  [in] | vector | ARC_Vector to add to
  [in] | data | data that is being added

Definition at line 70 of file vector.c.

{
//check to see if the current size is the same as a max uint32_t and if so it will overflow so throw an error
if(vector->currentSize == ~((uint32_t)0)){
arc_errno = ARC_ERRNO_OVERFLOW;
ARC_DEBUG_LOG_ERROR("ARC_Vector_Add(vector, data), vector at max capacity tried adding another value");
return;
}
//check if we are at the max of the current capacity
if(vector->currentSize == vector->currentCapacity){
//increase the current capacity by double
vector->currentCapacity <<= 1;
//if for some reason the capacity is 0, we should set it to one so we do not error on realloc
if(vector->currentCapacity != 0){
vector->currentCapacity++;
}
//resize the vectors array and copy the contents at the same time
vector->data = (void **)realloc(vector->data, sizeof(void *) * vector->currentCapacity);
}
//add to the vectors array and increase its current size
vector->data[vector->currentSize] = data;
vector->currentSize++;
}
arc_errno
int32_t arc_errno
Definition errno.c:5
ARC_ERRNO_OVERFLOW
#define ARC_ERRNO_OVERFLOW
Definition errno.h:10
ARC_DEBUG_LOG_ERROR
#define ARC_DEBUG_LOG_ERROR(STR)
Definition errno.h:39
ARC_Vector::data
void ** data
Definition vector.c:16
ARC_Vector::currentCapacity
uint32_t currentCapacity
Definition vector.c:13
ARC_Vector::currentSize
uint32_t currentSize
Definition vector.c:14

References ARC_DEBUG_LOG_ERROR, arc_errno, ARC_ERRNO_OVERFLOW, ARC_Vector::currentCapacity, ARC_Vector::currentSize, and ARC_Vector::data.

Referenced by ARC_ConsoleBuffer_AddChar(), ARC_ConsoleBuffer_Clear(), ARC_ConsoleBuffer_Create(), ARC_ConsoleShell_AddHistory(), ARC_ConsoleView_AddElement(), ARC_Handler_Add(), ARC_Handler_Remove(), ARC_Handler_RemoveIndex(), ARC_Lexer_LexString(), ARC_Lexer_RegisterTokenRule(), ARC_Parser_ParseTag(), ARC_ParserCSVData_CreateFn(), ARC_ParserCSVData_GetDataTag(), ARC_ParserCSVData_RunLineTag(), ARC_ParserLangParsedData_CreateBodyTag(), ARC_ParserLangParsedData_GetArgumentsTag(), ARC_ParserLangParsedData_GetArgumentTag(), ARC_ParserLangParsedData_RunLineTag(), ARC_TEST(), ARC_TEST(), ARC_TEST(), ARC_TEST(), and ARC_TEST().

## ARC_Vector_Clear()

void ARC_Vector_Clear | ( | ARC_Vector * | vector | ) |

clears all items from a vector

Note
  this function will call ARC_Vector_RemoveIndex, so it's notes are also applicable to this function

Parameters

  [in] | vector | ARC_Vector to clear

Definition at line 142 of file vector.c.

{
//remove each item in the vector untill the vector is empty
while(ARC_Vector_GetSize(vector) != 0){
ARC_Vector_RemoveIndex(vector, 0);
}
}
ARC_Vector_GetSize
uint32_t ARC_Vector_GetSize(ARC_Vector *vector)
gets the current size of an ARC_Vector as an unsigned 32 bit integer
Definition vector.c:149
ARC_Vector_RemoveIndex
void ARC_Vector_RemoveIndex(ARC_Vector *vector, uint32_t index)
removes an item from an ARC_Vector at an index
Definition vector.c:110

References ARC_Vector_GetSize(), and ARC_Vector_RemoveIndex().

Referenced by ARC_Lexer_Clear(), ARC_Parser_ParseTag(), and ARC_Vector_Destroy().

## ARC_Vector_CompareDataDefaultFn()

ARC_Bool ARC_Vector_CompareDataDefaultFn | ( | void * | dataA,
|  | void * | dataB )

Definition at line 23 of file vector.c.

{
if(dataA == dataB){
return ARC_True;
}
return ARC_False;
}
ARC_False
#define ARC_False
Definition bool.h:12
ARC_True
#define ARC_True
Definition bool.h:11

References ARC_False, and ARC_True.

Referenced by ARC_Vector_Create().

## ARC_Vector_Create()

void ARC_Vector_Create | ( | ARC_Vector ** | vector,
|  | ARC_Vector_CompareDataFn * | compareDataFn,
|  | ARC_Vector_DestroyDataFn * | destroyDataFn )

creates an ARC_Vector which is an "expandable" array

Note
  for this basic implementation, the array will double in size every time the capacity is hit
  the array will also half in size when the array is only half filled

Parameters

  [out] | vector | ARC_Vector to initialize
  [in] | compareDataFn | a callback that checks if data stored in the array matches, if set to NULL and ARC_Vector_Remove is called, the pointer addresses will be compared
  [in] | destroyDataFn | a callback that frees an item on remove or clear, can be set to NULL to do nothing

Definition at line 31 of file vector.c.

{
//create the vector
*vector = (ARC_Vector *)malloc(sizeof(ARC_Vector));
//initialize all the values stored in the vector
(*vector)->data = (void **)malloc(sizeof(void *));
(*vector)->currentCapacity = 1;
(*vector)->currentSize = 0;
//set a default for compareDataFn, then override it if it is passed in through parameters
(*vector)->compareDataFn = ARC_Vector_CompareDataDefaultFn;
if(compareDataFn != NULL){
(*vector)->compareDataFn = *compareDataFn;
}
//set NULL as a default for deleteDataFn, then copy the delete data function callback if it exists
(*vector)->destroyDataFn = NULL;
if(destroyDataFn != NULL){
(*vector)->destroyDataFn = (ARC_Vector_DestroyDataFn *)malloc(sizeof(ARC_Vector_DestroyDataFn));
*((*vector)->destroyDataFn) = *destroyDataFn;
}
}
ARC_Vector_CompareDataDefaultFn
ARC_Bool ARC_Vector_CompareDataDefaultFn(void *dataA, void *dataB)
Definition vector.c:23
ARC_Vector
a private dynamic array type
Definition vector.c:12
ARC_Vector_DestroyDataFn
void(* ARC_Vector_DestroyDataFn)(void *data)
a callback that cleans up memory when it is removed from the vector
Definition vector.h:31

References ARC_Vector_CompareDataDefaultFn(), and ARC_Vector::data.

Referenced by ARC_ConsoleBuffer_Clear(), ARC_ConsoleBuffer_Create(), ARC_ConsoleShell_Create(), ARC_ConsoleView_Create(), ARC_Handler_Create(), ARC_Lexer_Create(), ARC_Parser_ParseTag(), ARC_ParserCSVData_CreateFn(), ARC_ParserCSVData_RunLineTag(), ARC_ParserLang_CreateDataFn(), ARC_ParserLangParsedData_CreateBodyTag(), ARC_ParserLangParsedData_GetArgumentsTag(), ARC_TEST(), ARC_TEST(), ARC_TEST(), ARC_TEST(), and ARC_TEST().

## ARC_Vector_Destroy()

void ARC_Vector_Destroy | ( | ARC_Vector * | vector | ) |

destroys an ARC_Vector

Note
  this will not free the items stored in the vector
  please make sure to clear and free the children before destroying an ARC_Vector

Parameters

  [in] | vector | ARC_Vector to free

Definition at line 54 of file vector.c.

{
//remove all the contents before destroying the vector
ARC_Vector_Clear(vector);
//free the delete data function if it exists
if(vector->destroyDataFn){
free(vector->destroyDataFn);
}
//free everything stored in the vector
free(vector->data);
//free the vector
free(vector);
}
ARC_Vector_Clear
void ARC_Vector_Clear(ARC_Vector *vector)
clears all items from a vector
Definition vector.c:142
ARC_Vector::destroyDataFn
ARC_Vector_DestroyDataFn * destroyDataFn
Definition vector.c:19

References ARC_Vector_Clear(), ARC_Vector::data, and ARC_Vector::destroyDataFn.

Referenced by ARC_ConsoleBuffer_Clear(), ARC_ConsoleBuffer_Destroy(), ARC_ConsoleShell_Destroy(), ARC_ConsoleView_Destroy(), ARC_Handler_Destroy(), ARC_Lexer_Destroy(), ARC_Parser_ParseTag(), ARC_ParserCSVData_CreateFn(), ARC_ParserCSVData_VectorDestroyVectorFn(), ARC_ParserLang_DestroyDataFn(), ARC_ParserLang_VectorDestroyVectorFn(), ARC_ParserLangParsedData_CreateBodyTag(), ARC_ParserTagToken_Destroy(), ARC_TEST(), ARC_TEST(), ARC_TEST(), ARC_TEST(), and ARC_TEST().

## ARC_Vector_Get()

void * ARC_Vector_Get | ( | ARC_Vector * | vector,
|  | uint32_t | index )

gets an item from an ARC_Vector at a position index

Note
  this function will error if trying to get an index that is outside the bounds of the ARC_Vector

Parameters

  [in] | vector | ARC_Vector to get data from
  [in] | index | position of data to get

Returns
  a void * item, or NULL on error

Definition at line 153 of file vector.c.

{
//check to make sure the given index is in bounds of the vector
if(index >= vector->currentSize){
arc_errno = ARC_ERRNO_DATA;
ARC_DEBUG_LOG_ERROR_WITH_VARIABLES("ARC_Vector_Get(vector, %u), null value as the index was out of bounds", index);
return NULL;
}
return vector->data[index];
}
ARC_DEBUG_LOG_ERROR_WITH_VARIABLES
#define ARC_DEBUG_LOG_ERROR_WITH_VARIABLES(STR,...)
Definition errno.h:40
ARC_ERRNO_DATA
#define ARC_ERRNO_DATA
Definition errno.h:7

References ARC_DEBUG_LOG_ERROR_WITH_VARIABLES, arc_errno, ARC_ERRNO_DATA, ARC_Vector::currentSize, and ARC_Vector::data.

Referenced by ARC_ConsoleBuffer_AddChar(), ARC_ConsoleBuffer_Clear(), ARC_ConsoleBuffer_Destroy(), ARC_ConsoleBuffer_Render(), ARC_ConsoleBuffer_RenderSection(), ARC_ConsoleShell_Destroy(), ARC_ConsoleShell_GetHistoryAt(), ARC_ConsoleView_GetElement(), ARC_ConsoleView_RenderElements(), ARC_Handler_Clean(), ARC_Handler_Iterate(), ARC_Handler_RemoveIndex(), ARC_Lexer_GetToken(), ARC_Lexer_IsTokenId(), ARC_Lexer_LexString(), ARC_Lexer_PrintTokenRules(), ARC_Lexer_RegisterTokenRule(), ARC_Parser_CreateFromVector(), ARC_ParserCSVData_CreateFn(), ARC_ParserCSVData_GetDataTag(), ARC_ParserCSVData_RunLineTag(), ARC_ParserData_HelperRecurseStringAdd(), ARC_ParserLangParsedData_CreateBodyTag(), ARC_ParserLangParsedData_CreateTagString(), ARC_ParserLangParsedData_GetArgumentsTag(), ARC_ParserLangParsedData_GetArgumentTag(), ARC_ParserLangParsedData_RecurseStringAdd(), ARC_ParserLangParsedData_RunLineTag(), ARC_TEST(), ARC_TEST(), ARC_TEST(), ARC_TEST(), and TEST_ParserData_CreateCharFn().

## ARC_Vector_GetSize()

uint32_t ARC_Vector_GetSize | ( | ARC_Vector * | vector | ) |

gets the current size of an ARC_Vector as an unsigned 32 bit integer

Parameters

  [in] | vector | ARC_Vector to get current size from

Returns
  the current size as a unsigned 32 bit integer

Definition at line 149 of file vector.c.

{
return vector->currentSize;
}

References ARC_Vector::currentSize.

Referenced by ARC_Handler_Clean(), ARC_Handler_Clear(), ARC_Handler_GetSize(), ARC_Handler_Iterate(), ARC_Handler_RemoveIndex(), ARC_Lexer_GetTokensSize(), ARC_Lexer_IsTokenId(), ARC_Lexer_LexString(), ARC_Lexer_PrintTokenRules(), ARC_Lexer_RegisterTokenRule(), ARC_Parser_CreateFromVector(), ARC_ParserCSVData_CreateFn(), ARC_ParserCSVData_GetDataTag(), ARC_ParserCSVData_RunLineTag(), ARC_ParserData_HelperRecurseStringAdd(), ARC_ParserLangParsedData_CreateBodyTag(), ARC_ParserLangParsedData_CreateTagString(), ARC_ParserLangParsedData_GetArgumentsTag(), ARC_ParserLangParsedData_GetArgumentTag(), ARC_ParserLangParsedData_RecurseStringAdd(), ARC_ParserLangParsedData_RunLineTag(), ARC_TEST(), and ARC_Vector_Clear().

## ARC_Vector_Remove()

void ARC_Vector_Remove | ( | ARC_Vector * | vector,
|  | void * | data )

removes an item from a matching item in an ARC_Vector

Note
  this function uses the ARC_Vector_CompareDataFn that the ARC_Vector was created with
  this function will not throw an error if there is no match
  this function will call ARC_Vector_RemoveIndex, so it's notes are also applicable to this function

Parameters

  [in] | vector | ARC_Vector to remove from
  [in] | data | matching data to remove

Definition at line 97 of file vector.c.

{
//iterate through every item to check to see if it exists
for(uint32_t index = 0; index < vector->currentSize; index++){
//keep the code cleaner by pulling the current index data into a temp variable
void *dataB = vector->data[index];
//check if the data matches, and if so remove by index
if(vector->compareDataFn(data, dataB) == ARC_True){
ARC_Vector_RemoveIndex(vector, index);
}
}
}
ARC_Vector::compareDataFn
ARC_Vector_CompareDataFn compareDataFn
Definition vector.c:18

References ARC_True, ARC_Vector_RemoveIndex(), ARC_Vector::compareDataFn, ARC_Vector::currentSize, and ARC_Vector::data.

Referenced by ARC_Handler_Remove(), and ARC_TEST().

## ARC_Vector_RemoveIndex()

void ARC_Vector_RemoveIndex | ( | ARC_Vector * | vector,
|  | uint32_t | index )

removes an item from an ARC_Vector at an index

Note
  this function will error if trying to remove an index that is outside the bounds of the ARC_Vector
  this function will use ARC_Vector_DeleteDataFn if it was set in the ARC_Vector_Create function

Parameters

  [in] | vector | ARC_Vector to remove from
  [in] | index | position of data to remove

Definition at line 110 of file vector.c.

{
//check to make sure the given index is in bounds of the vector
if(index >= vector->currentSize){
arc_errno = ARC_ERRNO_DATA;
ARC_DEBUG_LOG_ERROR("ARC_Vector_Add(vector, data), vector at max capacity tried adding another value");
return;
}
//call delete data to clean up item if delete data function exists
if(vector->destroyDataFn != NULL){
(*(vector->destroyDataFn))(vector->data[index]);
}
//we will be using index to iterate as we will not use it again, so we can skip the first part of the for loop
for(; index + 1 < vector->currentSize; index++){
//override the data from index to the end by shifting it back one
vector->data[index] = vector->data[index + 1];
}
//we have removed the item so we can decrease the current size
vector->currentSize--;
//if the current size is half the current capacity or the current capacity is at the smallest limit, we do not need to do anything else
if(vector->currentSize != vector->currentCapacity >> 1 || vector->currentCapacity == 1){
return;
}
//half the capacity and copy it into a smaller array
vector->currentCapacity >>= 1;
vector->data = (void **)realloc(vector->data, sizeof(void *) * vector->currentCapacity);
}

References ARC_DEBUG_LOG_ERROR, arc_errno, ARC_ERRNO_DATA, ARC_Vector::currentCapacity, ARC_Vector::currentSize, ARC_Vector::data, and ARC_Vector::destroyDataFn.

Referenced by ARC_ConsoleBuffer_AddChar(), ARC_ConsoleView_RemoveElement(), ARC_Handler_Clean(), ARC_Handler_RemoveIndex(), ARC_TEST(), ARC_TEST(), ARC_TEST(), ARC_Vector_Clear(), and ARC_Vector_Remove().