Skip to content

Latest commit

 

History

History
243 lines (167 loc) · 4.82 KB

dsc_vector.md

File metadata and controls

243 lines (167 loc) · 4.82 KB

Vector

A vector is a dynamic array data structure that provides flexible storage and efficient insertion and deletion of elements. It automatically manages memory allocation and deallocation, allowing the vector to grow and shrink as needed.

Data Structures

DSCVector

struct DSCVector;

Represents a vector. The internal implementation of the DSCVector struct is hidden and defined in the source file.

Functions

dsc_vector_create

DSCVector *dsc_vector_create();

Creates a new empty vector.

Returns: A pointer to the newly created vector, or NULL if memory allocation fails.

Example:

DSCVector *vector = dsc_vector_create();
if (vector == NULL) {
    // Handle memory allocation failure
}

dsc_vector_free

void dsc_vector_free(DSCVector *vector);

Destroys the vector and frees its memory.

Parameters:

  • vector: The vector to destroy.

Example:

DSCVector *vector = dsc_vector_create();
// Use the vector
dsc_vector_free(vector);

dsc_vector_push_back

void dsc_vector_push_back(DSCVector *vector, int value);

Inserts an element at the end of the vector.

Parameters:

  • vector: The vector to push the element into.
  • value: The value to push.

Example:

DSCVector *vector = dsc_vector_create();
dsc_vector_push_back(vector, 42);
dsc_vector_push_back(vector, 73);

dsc_vector_pop_back

void dsc_vector_pop_back(DSCVector *vector);

Removes the last element from the vector.

Parameters:

  • vector: The vector to pop from.

Example:

DSCVector *vector = dsc_vector_create();
dsc_vector_push_back(vector, 42);
dsc_vector_push_back(vector, 73);
dsc_vector_pop_back(vector); // Removes 73 from the vector

dsc_vector_insert

void dsc_vector_insert(DSCVector *vector, int value);

Inserts an element at the beginning of the vector.

Parameters:

  • vector: The vector to insert the element into.
  • value: The value to insert.

Example:

DSCVector *vector = dsc_vector_create();
dsc_vector_insert(vector, 42);
dsc_vector_insert(vector, 73);

dsc_vector_erase

void dsc_vector_erase(DSCVector *vector);

Removes the first element from the vector.

Parameters:

  • vector: The vector to remove the first element from.

Example:

DSCVector *vector = dsc_vector_create();
dsc_vector_insert(vector, 42);
dsc_vector_insert(vector, 73);
dsc_vector_erase(vector); // Removes 42 from the vector

dsc_vector_at

int dsc_vector_at(const DSCVector *vector, unsigned int index);

Retrieves the value of the element at the specified index.

Parameters:

  • vector: The vector to get the element from.
  • index: The index of the element to retrieve.

Returns: The value of the element at the specified index, or 0 if the index is out of bounds.

Example:

DSCVector *vector = dsc_vector_create();
dsc_vector_push_back(vector, 42);
dsc_vector_push_back(vector, 73);
int value = dsc_vector_at(vector, 1); // value = 73

dsc_vector_empty

bool dsc_vector_empty(const DSCVector *vector);

Checks if the vector is empty.

Parameters:

  • vector: The vector to check.

Returns: true if the vector is empty, false otherwise.

Example:

DSCVector *vector = dsc_vector_create();
if (dsc_vector_empty(vector)) {
    printf("The vector is empty\n");
} else {
    printf("The vector is not empty\n");
}

dsc_vector_size

unsigned int dsc_vector_size(const DSCVector *vector);

Gets the number of elements in the vector.

Parameters:

  • vector: The vector to get the size of.

Returns: The number of elements in the vector.

Example:

DSCVector *vector = dsc_vector_create();
dsc_vector_push_back(vector, 42);
dsc_vector_push_back(vector, 73);
unsigned int size = dsc_vector_size(vector); // size = 2

dsc_vector_capacity

unsigned int dsc_vector_capacity(const DSCVector *vector);

Gets the maximum number of elements the vector can hold before needing to allocate more memory.

Parameters:

  • vector: The vector to get the capacity of.

Returns: The maximum number of elements the vector can hold.

Example:

DSCVector *vector = dsc_vector_create();
dsc_vector_push_back(vector, 42);
dsc_vector_push_back(vector, 73);
unsigned int capacity = dsc_vector_capacity(vector); // capacity = 2 (or more, depending on the initial capacity)

dsc_vector_reserve

void dsc_vector_reserve(DSCVector *vector, unsigned int new_capacity);

Reserves memory for the vector to hold at least the specified number of elements.

Parameters:

  • vector: The vector to reserve memory for.
  • new_capacity: The minimum capacity to reserve.

Example:

DSCVector *vector = dsc_vector_create();
dsc_vector_reserve(vector, 10); // Reserve memory for at least 10 elements