]> git.rm.cloudns.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
ArrayList: implement
authorTimePath <andrew.hardaker1995@gmail.com>
Mon, 23 Nov 2015 05:24:12 +0000 (16:24 +1100)
committerTimePath <andrew.hardaker1995@gmail.com>
Mon, 23 Nov 2015 05:25:30 +0000 (16:25 +1100)
List with O(1) access

qcsrc/lib/_all.inc
qcsrc/lib/arraylist.qh [new file with mode: 0644]

index ceb59b780a7db71dd87640d9831cd78d810534b5..97483b518f459c7d96752d7c0b73868def0285db 100644 (file)
@@ -28,6 +28,7 @@
 #include "warpzone/mathlib.qc"
 
 #include "accumulate.qh"
+#include "arraylist.qh"
 #include "bits.qh"
 #include "bool.qh"
 #include "color.qh"
diff --git a/qcsrc/lib/arraylist.qh b/qcsrc/lib/arraylist.qh
new file mode 100644 (file)
index 0000000..c059149
--- /dev/null
@@ -0,0 +1,44 @@
+#ifndef ARRAYLIST_H
+#define ARRAYLIST_H
+
+typedef int ArrayList;
+
+#define AL_declare(this) ArrayList this; int this##_len = (0)
+#define AL_init(this, n, default, T) \
+       do \
+       { \
+               this = buf_create(); \
+               this##_len = n; \
+               for (int i = 0; i < this##_len; ++i) \
+               { \
+                       const _AL_type__##T() it = default; \
+                       AL_set##T(this, i, it); \
+               } \
+       } \
+       while (0)
+
+#define _AL_type__s() string
+#define AL_gets(this, idx) bufstr_get(this, idx)
+#define AL_sets(this, idx, val) bufstr_set(this, idx, val)
+
+#define _AL_type__f() float
+#define AL_getf(this, idx) stof(AL_gets(this, idx))
+#define AL_setf(this, idx, val) AL_sets(this, idx, ftos(val))
+
+#define _AL_type__e() entity
+#define AL_gete(this, idx) ftoe(AL_getf(this, idx))
+#define AL_sete(this, idx, val) AL_setf(this, idx, etof(val))
+
+#define AL_EACH(this, T, cond, body) \
+       do \
+       { \
+               const noref ArrayList _al = this; \
+               for (int i = 0, n = this##_len; i < n; ++i) \
+               { \
+                       const noref _AL_type__##T() it = AL_get##T(_al, i); \
+                       if (cond) { body } \
+               } \
+       } \
+       while (0)
+
+#endif