Skip to content

Commit

Permalink
Bugfixes in C data structures
Browse files Browse the repository at this point in the history
  • Loading branch information
RandyGaul committed Oct 10, 2024
1 parent 874752d commit ad15a45
Show file tree
Hide file tree
Showing 34 changed files with 138 additions and 39 deletions.
1 change: 1 addition & 0 deletions docs/api_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ This is a list of all functions in Cute Framework organized by categories. This
- [acap](/array/acap.md)
- [aclear](/array/aclear.md)
- [acount](/array/acount.md)
- [adel](/array/adel.md)
- [aend](/array/aend.md)
- [afit](/array/afit.md)
- [afree](/array/afree.md)
Expand Down
1 change: 1 addition & 0 deletions docs/array/acap.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ a | The array.
[aset](/array/aset.md)
[arev](/array/arev.md)
[ahash](/array/ahash.md)
[adel](/array/adel.md)
[astatic](/array/astatic.md)
1 change: 1 addition & 0 deletions docs/array/aclear.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ Returns zero.
[aset](/array/aset.md)
[arev](/array/arev.md)
[ahash](/array/ahash.md)
[adel](/array/adel.md)
[astatic](/array/astatic.md)
1 change: 1 addition & 0 deletions docs/array/acount.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ afree(a);
[aset](/array/aset.md)
[arev](/array/arev.md)
[ahash](/array/ahash.md)
[adel](/array/adel.md)
[astatic](/array/astatic.md)
44 changes: 44 additions & 0 deletions docs/array/adel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[//]: # (This file is automatically generated by Cute Framework's docs parser.)
[//]: # (Do not edit this file by hand!)
[//]: # (See: https://github.com/RandyGaul/cute_framework/blob/master/samples/docs_parser.cpp)
[](../header.md ':include')

# adel

Category: [array](/api_reference?id=array)
GitHub: [cute_array.h](https://github.com/RandyGaul/cute_framework/blob/master/include/cute_array.h)
---

Performs an unordered removal of the element at index i.

```cpp
#define adel(a, i) cf_array_del(a)
```
Parameters | Description
--- | ---
a | The array.
i | The index of the element to remove.
## Remarks
The last element of the array is swapped into the index `i`. This is a constant time
operation, but does not preserve order of the array.
## Related Pages
[dyna](/array/dyna.md)
[asize](/array/asize.md)
[acount](/array/acount.md)
[acap](/array/acap.md)
[afit](/array/afit.md)
[apush](/array/apush.md)
[apop](/array/apop.md)
[aend](/array/aend.md)
[alast](/array/alast.md)
[aclear](/array/aclear.md)
[aset](/array/aset.md)
[arev](/array/arev.md)
[ahash](/array/ahash.md)
[afree](/array/afree.md)
[astatic](/array/astatic.md)
1 change: 1 addition & 0 deletions docs/array/aend.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ a | The array. Can be `NULL`.
[aset](/array/aset.md)
[arev](/array/arev.md)
[ahash](/array/ahash.md)
[adel](/array/adel.md)
[astatic](/array/astatic.md)
1 change: 1 addition & 0 deletions docs/array/afit.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ automatically re-assigned to a new pointer if the array was internally regrown.
[aset](/array/aset.md)
[arev](/array/arev.md)
[ahash](/array/ahash.md)
[adel](/array/adel.md)
[astatic](/array/astatic.md)
1 change: 1 addition & 0 deletions docs/array/afree.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ Sets `a` to `NULL`.
[aset](/array/aset.md)
[arev](/array/arev.md)
[ahash](/array/ahash.md)
[adel](/array/adel.md)
[astatic](/array/astatic.md)
1 change: 1 addition & 0 deletions docs/array/ahash.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ a | The array.
[aset](/array/aset.md)
[arev](/array/arev.md)
[afree](/array/afree.md)
[adel](/array/adel.md)
[astatic](/array/astatic.md)
1 change: 1 addition & 0 deletions docs/array/alast.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ a | The array. Can not be `NULL`.
[aset](/array/aset.md)
[arev](/array/arev.md)
[ahash](/array/ahash.md)
[adel](/array/adel.md)
[astatic](/array/astatic.md)
1 change: 1 addition & 0 deletions docs/array/alen.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,6 @@ afree(a);
[aset](/array/aset.md)
[arev](/array/arev.md)
[ahash](/array/ahash.md)
[adel](/array/adel.md)
[astatic](/array/astatic.md)
[afree](/array/afree.md)
1 change: 1 addition & 0 deletions docs/array/apop.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ The last element of the array is fetched and will be returned. The size of the a
[aset](/array/aset.md)
[arev](/array/arev.md)
[ahash](/array/ahash.md)
[adel](/array/adel.md)
[astatic](/array/astatic.md)
1 change: 1 addition & 0 deletions docs/array/apush.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ dynamic array is allocated on-the-spot for you, and assigned back to `a`.
[aset](/array/aset.md)
[arev](/array/arev.md)
[ahash](/array/ahash.md)
[adel](/array/adel.md)
[astatic](/array/astatic.md)
1 change: 1 addition & 0 deletions docs/array/arev.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ a | The array. Can be `NULL`.
[aset](/array/aset.md)
[afree](/array/afree.md)
[ahash](/array/ahash.md)
[adel](/array/adel.md)
[astatic](/array/astatic.md)
1 change: 1 addition & 0 deletions docs/array/aset.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ Returns a pointer to `a`. `a` will automatically be reassigned to any new pointe
[afree](/array/afree.md)
[arev](/array/arev.md)
[ahash](/array/ahash.md)
[adel](/array/adel.md)
[astatic](/array/astatic.md)
1 change: 1 addition & 0 deletions docs/array/asize.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ afree(a);
[aset](/array/aset.md)
[arev](/array/arev.md)
[ahash](/array/ahash.md)
[adel](/array/adel.md)
[astatic](/array/astatic.md)
1 change: 1 addition & 0 deletions docs/array/astatic.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ dynamically grow into the heap.
[aset](/array/aset.md)
[arev](/array/arev.md)
[ahash](/array/ahash.md)
[adel](/array/adel.md)
[afree](/array/afree.md)
1 change: 1 addition & 0 deletions docs/array/dyna.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,5 @@ parameters or struct member definitions. It's saying "Hey, I'm a dynamic array!"
[aset](/array/aset.md)
[arev](/array/arev.md)
[ahash](/array/ahash.md)
[adel](/array/adel.md)
[astatic](/array/astatic.md)
2 changes: 1 addition & 1 deletion docs/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Make sure you have a compiler installed that you're familiar with beforehand. If

1. Download and install CMake (v3.14 or higher, you can just get the latest version). CMake is for easy cross-platform building. Also install [git](https://git-scm.com/downloads). If you're new to git and a Windows user it's highly recommended to use [Github Desktop](https://desktop.github.com/).
2. Copy CMakeLists.txt ([this one here](https://github.com/RandyGaul/cute_framework_project_template/blob/main/CMakeLists.txt)) into the top-level of your project directory.
3. Find + replace "my_project_name".
3. Find + replace "mygame".
4. Make a folder called `src` in the top-level of your project, and place your initial `main.cpp` there.
5. Run CMake on your project folder. If you need help with this step, try reading the [CF + CMake 101 section here](https://github.com/RandyGaul/cute_framework_project_template#cmake-101-walkthrough).

Expand Down
2 changes: 1 addition & 1 deletion docs/hash/hfind.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Fetches the item that `k` maps to.
Parameters | Description
--- | ---
h | The hashtable. Can be `NULL`. Needs to be a pointer to the type of items in the table.
h | The hashtable. Can not be `NULL`. Needs to be a pointer to the type of items in the table.
k | The key for lookups. Each {key, item} pair must be unique. Keys are always typecasted to `uint64_t` e.g. you can use pointers as keys.
## Return Value
Expand Down
2 changes: 1 addition & 1 deletion docs/hash/hget.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Fetches the item that `k` maps to.
Parameters | Description
--- | ---
h | The hashtable. Can be `NULL`. Needs to be a pointer to the type of items in the table.
h | The hashtable. Can not be `NULL`. Needs to be a pointer to the type of items in the table.
k | The key for lookups. Each {key, item} pair must be unique. Keys are always typecasted to `uint64_t` e.g. you can use pointers as keys.
## Return Value
Expand Down
2 changes: 1 addition & 1 deletion docs/string/sfmt.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ GitHub: [cute_string.h](https://github.com/RandyGaul/cute_framework/blob/master/
Printf's into the string using the format string `fmt`.

```cpp
#define sfmt(s, fmt, ...) cf_string_fmt(s, fmt, (__VA_ARGS__))
#define sfmt(s, fmt, ...) cf_string_fmt(s, fmt, __VA_ARGS__)
```
Parameters | Description
Expand Down
2 changes: 1 addition & 1 deletion docs/string/sfmt_append.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ GitHub: [cute_string.h](https://github.com/RandyGaul/cute_framework/blob/master/
Printf's into the end of the string, using the format string `fmt`.

```cpp
#define sfmt_append(s, fmt, ...) cf_string_fmt_append(s, fmt, (__VA_ARGS__))
#define sfmt_append(s, fmt, ...) cf_string_fmt_append(s, fmt, __VA_ARGS__)
```
Parameters | Description
Expand Down
2 changes: 1 addition & 1 deletion docs/time/cf_set_fixed_timestep.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void cf_set_fixed_timestep(int frames_per_second);
Parameters | Description
--- | ---
frames_per_second | The frequency for fixed-timestep updates to occur, e.g. 30 is a good default number.
frames_per_second | The frequency for fixed-timestep updates to occur, e.g. 30 is a good default number. Set to 0 to disable fixed timestep.
## Remarks
Expand Down
10 changes: 5 additions & 5 deletions docs/topics/virtual_file_system.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,21 @@ If you have all your loadable files in a folder called `content` right beside a
```cpp
void mount_content_folder()
{
char* path = spnorm(fs_get_base_dir());
char* path = spnorm(cf_fs_get_base_directory());
int n = 1;
char* dir = spdir_of(path);
if (siequ(dir, "Debug") || siequ(dir, "Release")) {
if (ssuffix(path, "Debug") || ssuffix(path, "Release")) {
// MSVC/XCode places the .exe into one-level deeper /Debug or /Release folders.
n = 2;
}
sfree(dir);
path = sppopn(path, n);
scat(path, "/content");
fs_mount(path, "/");
cf_fs_mount(path, "/", true);
sfree(path);
}
```

Of course, when you release your game for people to install and play on their own machines you will need to modify this function. In all likelihood your content folder would sit next to your executable, instead of a build folder, so the call to `sppopn` wouldn't be necessary.

## The Write Directory

Your application gets a single write directory. You set it with [`cf_fs_set_write_directory`](https://randygaul.github.io/cute_framework/#/file/cf_fs_set_write_directory). This greatly aids security and keeps writing operations locked within a single directory for simplicity. It's highly recommended to setup your write directory as the user directory from [`cf_fs_get_user_directory`](https://randygaul.github.io/cute_framework/#/file/cf_fs_get_user_directory). This directory is guaranteed to be a write-enabled and safe place to store game-specific files for your player.
Expand Down
45 changes: 29 additions & 16 deletions include/cute_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* on typed pointers, there's no actual array struct type. It can get really annoying to sometimes forget if a pointer is an
* array, a hashtable, or just a pointer. This macro can be used to markup the type to make it much more clear for function
* parameters or struct member definitions. It's saying "Hey, I'm a dynamic array!" to mitigate this downside.
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash astatic afree
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash adel astatic afree
*/
#define dyna

Expand All @@ -49,7 +49,7 @@
* CF_ASSERT(alen(a) == 0);
* afree(a);
* @remarks `a` must not by `NULL`. This function returns a proper l-value, so you can assign to it, i.e. increment/decrement can be quite useful.
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash astatic afree
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash adel astatic afree
*/
#define alen(a) cf_array_len(a)

Expand All @@ -64,7 +64,7 @@
* CF_ASSERT(asize(a) == 1);
* afree(a);
* @remarks `a` can be `NULL`.
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash astatic afree
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash adel astatic afree
*/
#define asize(a) cf_array_size(a)

Expand All @@ -79,7 +79,7 @@
* CF_ASSERT(acount(a) == 1);
* afree(a);
* @remarks `a` can be `NULL`.
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash astatic afree
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash adel astatic afree
*/
#define acount(a) cf_array_count(a)

Expand All @@ -90,7 +90,7 @@
* @param a The array.
* @remarks `a` can be `NULL`. The capacity automatically grows if the size of the array grows over the capacity.
* You can use `afit` to ensure a minimum capacity as an optimization.
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash astatic afree
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash adel astatic afree
*/
#define acap(a) cf_array_capacity(a)

Expand All @@ -104,7 +104,7 @@
* @remarks This function does not change the number of live elements, the count/size, of the array. Only the capacity.
* This function is only useful as an optimization to avoid extra/unnecessary internal allocations. `a` is
* automatically re-assigned to a new pointer if the array was internally regrown.
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash astatic afree
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash adel astatic afree
*/
#define afit(a, n) cf_array_fit(a, n)

Expand All @@ -125,7 +125,7 @@
* afree(a);
* @remarks `a` is automatically re-assigned to a new pointer if the array was internally regrown. If `a` is `NULL` a new
* dynamic array is allocated on-the-spot for you, and assigned back to `a`.
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash astatic afree
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash adel astatic afree
*/
#define apush(a, ...) cf_array_push(a, (__VA_ARGS__))

Expand All @@ -136,7 +136,7 @@
* @param a The array. Can not be `NULL`.
* @return Returns the popped element.
* @remarks The last element of the array is fetched and will be returned. The size of the array is decremented by one.
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash astatic afree
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash adel astatic afree
*/
#define apop(a) cf_array_pop(a)

Expand All @@ -145,7 +145,7 @@
* @category array
* @brief Returns a pointer one element beyond the end of the array.
* @param a The array. Can be `NULL`.
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash astatic afree
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash adel astatic afree
*/
#define aend(a) cf_array_end(a)

Expand All @@ -154,7 +154,7 @@
* @category array
* @brief Returns the last element in the array.
* @param a The array. Can not be `NULL`.
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash astatic afree
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash adel astatic afree
*/
#define alast(a) cf_array_last(a)

Expand All @@ -164,7 +164,7 @@
* @brief Sets the array's count to zero. Does not free any resources.
* @param a The array. Can be `NULL`.
* @return Returns zero.
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash astatic afree
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash adel astatic afree
*/
#define aclear(a) cf_array_clear(a)

Expand All @@ -175,7 +175,7 @@
* @param a The array to copy into (destination).
* @param b The array to copy from (source).
* @return Returns a pointer to `a`. `a` will automatically be reassigned to any new pointer.
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash astatic afree
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash adel astatic afree
*/
#define aset(a, b) cf_array_set(a, b)

Expand All @@ -184,7 +184,7 @@
* @category array
* @brief Reverses the elements in an array.
* @param a The array. Can be `NULL`.
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash astatic afree
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash adel astatic afree
*/
#define arev(a) cf_array_reverse(a)

Expand All @@ -193,10 +193,22 @@
* @category array
* @brief Returns the hash of all the bytes in the array.
* @param a The array.
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash astatic afree
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash adel astatic afree
*/
#define ahash(a) cf_array_hash(a)

/**
* @function adel
* @category array
* @brief Performs an *unordered* removal of the element at index i.
* @param a The array.
* @param i The index of the element to remove.
* @remarks The last element of the array is swapped into the index `i`. This is a constant time
* operation, but does *not preserve order* of the array.
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash adel astatic afree
*/
#define adel(a, i) cf_array_del(a)

/**
* @function astatic
* @category array
Expand All @@ -208,7 +220,7 @@
* @remarks This macro is useful as an optimization to avoid any dynamic memory allocation in the common case for implementing
* certain data structures (such as strings or stack vectors). As the array grows too large for the `buffer` it will
* dynamically grow into the heap.
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash astatic afree
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash adel astatic afree
*/
#define astatic(a, buffer, buffer_size) cf_array_static(a, buffer, buffer_size)

Expand All @@ -218,7 +230,7 @@
* @brief Frees up all resources used by the array.
* @param a The array.
* @remarks Sets `a` to `NULL`.
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash astatic afree
* @related dyna asize acount acap afit apush apop aend alast aclear aset arev ahash adel astatic afree
*/
#define afree(a) cf_array_free(a)

Expand All @@ -242,6 +254,7 @@
#define cf_array_set(a, b) (*(void**)&(a) = cf_aset((void*)(a), (void*)(b), sizeof(*a)))
#define cf_array_reverse(a) (a ? cf_arev(a, sizeof(*a)) : NULL)
#define cf_array_hash(a) cf_fnv1a(a, cf_array_size(a))
#define cf_array_del(a) (a[i] = a[alen(a)--])
#define cf_array_static(a, buffer, buffer_size) (*(void**)&(a) = cf_astatic(buffer, buffer_size, sizeof(*a)))
#define cf_array_free(a) do { CF_ACANARY(a); if (a && !CF_AHDR(a)->is_static) cf_free(CF_AHDR(a)); a = NULL; } while (0)

Expand Down
Loading

0 comments on commit ad15a45

Please sign in to comment.