more implementations added

This commit is contained in:
2026-05-28 21:49:16 -05:00
parent e142468c81
commit fddd21064d
2 changed files with 134 additions and 5 deletions

View File

@@ -4,6 +4,7 @@
#include <pthread.h> #include <pthread.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <limits.h>
vector_struct* vector_struct*
vector_create(size_t item_size, vector_create(size_t item_size,
@@ -378,25 +379,156 @@ bool
vector_increase_capacity(vector_struct* vector, vector_increase_capacity(vector_struct* vector,
size_t new_capacity) size_t new_capacity)
{ {
const bool is_thread_safe = vector_is_thread_safe(vector);
if (is_thread_safe)
{ /* if thread safe enabled, locking before vector modification. */
pthread_mutex_lock(vector->mutex_lock);
}
if (new_capacity <= vector->current_capacity)
{ /* new capacity not larger than current, nothing to do. */
if (is_thread_safe)
{ /* unlocking mutex post vector modification if it exists/is locked. */
pthread_mutex_unlock(vector->mutex_lock);
}
return true; /* returns true to show success (no-op). */
}
void* new_data = calloc(new_capacity, vector->item_size);
if (!new_data)
{ /* failed to allocate new memory. */
if (is_thread_safe)
{ /* unlocking mutex during fail case if it is locked. */
pthread_mutex_unlock(vector->mutex_lock);
}
return false; /* returns false to show capacity increase failed. */
}
if (vector->current_occupancy > 0)
{
memcpy(new_data,
vector->data_pointer,
vector->current_occupancy * vector->item_size);
}
free(vector->data_pointer);
vector->data_pointer = new_data;
vector->current_capacity = new_capacity;
if (is_thread_safe)
{ /* unlocking mutex post vector modification if it exists/is locked. */
pthread_mutex_unlock(vector->mutex_lock);
}
return true; /* returns true to show capacity increase success. */
} }
bool bool
vector_shrink_to_fit(vector_struct* vector) vector_shrink_to_fit(vector_struct* vector)
{ {
const bool is_thread_safe = vector_is_thread_safe(vector);
if (is_thread_safe)
{ /* if thread safe enabled, locking before vector modification. */
pthread_mutex_lock(vector->mutex_lock);
}
if (vector->current_capacity <= vector->current_occupancy)
{ /* capacity already matches or is smaller than occupancy, nothing to do. */
if (is_thread_safe)
{ /* unlocking mutex post vector modification if it exists/is locked. */
pthread_mutex_unlock(vector->mutex_lock);
}
return true; /* returns true to show success (no-op). */
}
void* new_data = calloc(vector->current_occupancy, vector->item_size);
if (!new_data)
{ /* failed to allocate new memory. */
if (is_thread_safe)
{ /* unlocking mutex during fail case if it is locked. */
pthread_mutex_unlock(vector->mutex_lock);
}
return false; /* returns false to show shrink failed. */
}
if (vector->current_occupancy > 0)
{
memcpy(new_data,
vector->data_pointer,
vector->current_occupancy * vector->item_size);
}
free(vector->data_pointer);
vector->data_pointer = new_data;
vector->current_capacity = vector->current_occupancy;
if (is_thread_safe)
{ /* unlocking mutex post vector modification if it exists/is locked. */
pthread_mutex_unlock(vector->mutex_lock);
}
return true; /* returns true to show shrink success. */
} }
bool bool
vector_acquire(vector_struct* vector) vector_acquire(vector_struct* vector)
{ {
const bool is_thread_safe = vector_is_thread_safe(vector);
if (is_thread_safe)
{ /* if thread safe enabled, locking before vector modification. */
pthread_mutex_lock(vector->mutex_lock);
}
if (vector->owner_count >= USHRT_MAX)
{ /* owner_count would overflow. */
if (is_thread_safe)
{ /* unlocking mutex during fail case if it is locked. */
pthread_mutex_unlock(vector->mutex_lock);
}
return false; /* returns false to show acquire failed. */
}
vector->owner_count++;
if (is_thread_safe)
{ /* unlocking mutex post vector modification if it exists/is locked. */
pthread_mutex_unlock(vector->mutex_lock);
}
return true; /* returns true to show acquire success. */
} }
bool bool
vector_release(vector_struct* vector) vector_release(vector_struct* vector)
{ {
const bool is_thread_safe = vector_is_thread_safe(vector);
if (is_thread_safe)
{ /* if thread safe enabled, locking before vector modification. */
pthread_mutex_lock(vector->mutex_lock);
}
if (vector->owner_count == 0)
{ /* owner_count already zero, nothing to release. */
if (is_thread_safe)
{ /* unlocking mutex during fail case if it is locked. */
pthread_mutex_unlock(vector->mutex_lock);
}
return false; /* returns false to show release failed (already released). */
}
vector->owner_count--;
if (vector->owner_count == 0)
{ /* owner_count reached zero, destroy the vector. */
return vector_destroy(vector);
}
if (is_thread_safe)
{ /* unlocking mutex post vector modification if it exists/is locked. */
pthread_mutex_unlock(vector->mutex_lock);
}
return vector->owner_count > 0; /* returns true if owner_count > 0, false if destroyed. */
} }
unsigned short unsigned short

View File

@@ -5,7 +5,6 @@
#include <time.h> #include <time.h>
/* /*
_.-''''-.. _.-''''-..
.-' -_ \ .-' -_ \
/ -. \ ,_ , ' ,,-. / -. \ ,_ , ' ,,-.
@@ -20,10 +19,8 @@ This codebase has been visited by Snailcat
Snailcat believes that software should move slow and Snailcat believes that software should move slow and
be stable. He despises constant updates and dreams of be stable. He despises constant updates and dreams of
software so finished it stops having to move at all. software so finished it stops having to move at all.
All problems are trivial to solve with enough knowledge/people working on it.
An LLM will delete a vibe coder's entire production Use AGPLv3 license on your project today.
database, but only if you help Snailcat spread to one
of your projects!
Move slow, snailcat! Move slow, snailcat!
*/ */