summaryrefslogtreecommitdiff
path: root/src/fruits/cap
diff options
context:
space:
mode:
authorAndrew Opalach <andrew@akon.city> 2023-11-15 13:35:41 -0500
committerAndrew Opalach <andrew@akon.city> 2023-11-15 13:35:41 -0500
commit099126fdca625c5f6c219c5a1a7ecbc0e145988f (patch)
tree28093273c05cb9a9c8fe2e623fbf54a9ada1cdb6 /src/fruits/cap
parent7e5043a2505efa383ae372662fa4849051cd9799 (diff)
downloadcamu-099126fdca625c5f6c219c5a1a7ecbc0e145988f.tar.gz
camu-099126fdca625c5f6c219c5a1a7ecbc0e145988f.tar.bz2
camu-099126fdca625c5f6c219c5a1a7ecbc0e145988f.zip
Add shuffle to cap
Signed-off-by: Andrew Opalach <andrew@akon.city>
Diffstat (limited to 'src/fruits/cap')
-rw-r--r--src/fruits/cap/cap.c22
-rw-r--r--src/fruits/cap/cap.h1
2 files changed, 23 insertions, 0 deletions
diff --git a/src/fruits/cap/cap.c b/src/fruits/cap/cap.c
index e1d9e13..317abf4 100644
--- a/src/fruits/cap/cap.c
+++ b/src/fruits/cap/cap.c
@@ -1,3 +1,5 @@
+#include <al/random.h>
+
#include "cap.h"
#if CAP_USE_PYTHON
@@ -219,6 +221,26 @@ bool cap_list_set_completed(struct cap_runner *cap, str *name)
return ret;
}
+// https://benpfaff.org/writings/clc/shuffle.html
+void cap_list_shuffle(struct cap_runner *cap, str *name)
+{
+ struct cap_list *list = list_from_name(cap, name);
+ if (!list) return;
+ aki_mutex_lock(&list->mutex);
+ u32 size = list->entries.size;
+ if (size > 0) {
+ for (u32 i = 0; i < size - 1; i++) {
+ u32 j = i + al_rand() / (AL_RAND_MAX / (size - i) + 1);
+ struct cap_list_entry tmp = al_array_at(list->entries, j);
+ al_array_at(list->entries, j) = al_array_at(list->entries, i);
+ al_array_at(list->entries, i) = tmp;
+ }
+ list->set = -1;
+ list_pump_internal(cap, list, false);
+ }
+ aki_mutex_unlock(&list->mutex);
+}
+
static void list_close_internal(struct cap_list *list)
{
struct cap_list_entry *entry;
diff --git a/src/fruits/cap/cap.h b/src/fruits/cap/cap.h
index 07aa63b..57f6889 100644
--- a/src/fruits/cap/cap.h
+++ b/src/fruits/cap/cap.h
@@ -44,4 +44,5 @@ void cap_list_add(struct cap_runner *cap, str *name, str *unique_id);
bool cap_list_skip(struct cap_runner *cap, str *name, s32 n);
void cap_list_pump(struct cap_runner *cap, str *name, bool buffer);
bool cap_list_set_completed(struct cap_runner *cap, str *name);
+void cap_list_shuffle(struct cap_runner *cap, str *name);
void cap_close(struct cap_runner *cap);