summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Guschin <saintruler@gmail.com>2021-09-13 16:10:57 +0400
committerAndrew Guschin <saintruler@gmail.com>2021-09-13 16:10:57 +0400
commit11048808cbe3dc77bb2bee6d2643a33a45574509 (patch)
tree0896fe3a242366161b2e5328827bdf66a193a824
parent5438fe938f6a15f4eadcc0bb2fd09a02130aafb4 (diff)
Added bitset
-rw-r--r--.ccls2
-rw-r--r--src/bitset.c85
-rw-r--r--src/bitset.h44
-rw-r--r--src/main.c15
-rw-r--r--src/storage.h4
5 files changed, 149 insertions, 1 deletions
diff --git a/.ccls b/.ccls
index 246a3c6..61bd606 100644
--- a/.ccls
+++ b/.ccls
@@ -1,2 +1,2 @@
-tcc
+gcc
-Iinclude
diff --git a/src/bitset.c b/src/bitset.c
new file mode 100644
index 0000000..0d2f66a
--- /dev/null
+++ b/src/bitset.c
@@ -0,0 +1,85 @@
+#include "bitset.h"
+
+void
+bitarena_init(bitarena_t *arena, int size)
+{
+ arena->size = size;
+ size_t memSize = sizeof(char) * BITSET_BYTES * size;
+ arena->arena = malloc(memSize);
+ memset(arena->arena, 0, memSize);
+}
+
+bitset_t
+bitset_create()
+{
+ bitset_t tmp = (bitset_t) malloc(sizeof(char) * BITSET_BYTES);
+ memset(tmp, 0, sizeof(char) * BITSET_BYTES);
+ return tmp;
+}
+
+bitset_t
+bitarena_at(bitarena_t *arena, int idx)
+{
+ return (arena->arena) + (idx * BITSET_BYTES);
+}
+
+bitset_t
+bitset_and(bitset_t b1, bitset_t b2)
+{
+ bitset_t bitAnd = bitset_create();
+
+ for (int i = 0; i < BITSET_BYTES; ++i)
+ {
+ bitAnd[i] = b1[i] & b2[i];
+ }
+
+ return bitAnd;
+}
+
+int
+bitset_isZero(bitset_t bitset)
+{
+ for (int i = 0; i < BITSET_BYTES; ++i)
+ {
+ if (bitset[i] != 0)
+ return 0;
+ }
+ return 1;
+}
+
+void
+bitset_set(bitset_t bitset, int position)
+{
+ int byte = position / (sizeof(char) * 8);
+ int shift = position % (sizeof(char) * 8);
+ bitset[byte] = bitset[byte] | (1 << shift);
+}
+
+void
+bitset_unset(bitset_t bitset, int position)
+{
+ int byte = position / (sizeof(char) * 8);
+ int shift = position % (sizeof(char) * 8);
+ bitset[byte] = bitset[byte] & ~(1 << shift);
+}
+
+int
+bitset_equals(bitset_t b1, bitset_t b2)
+{
+ for (int i = 0; i < BITSET_BYTES; ++i)
+ {
+ if (b1[i] != b2[i])
+ return 0;
+ }
+ return 1;
+}
+
+void
+bitset_print(bitset_t bitset)
+{
+ for (int i = 3; i >= 0; --i)
+ {
+ printf("%i ", bitset[i]);
+ }
+ printf("\n");
+}
diff --git a/src/bitset.h b/src/bitset.h
new file mode 100644
index 0000000..0c5092d
--- /dev/null
+++ b/src/bitset.h
@@ -0,0 +1,44 @@
+#ifndef bitset_h_INCLUDED
+#define bitset_h_INCLUDED
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#ifndef BITSET_BYTES
+#define BITSET_BYTES 4
+#endif
+
+typedef struct
+{
+ int size;
+ char *arena;
+} bitarena_t;
+
+typedef char* bitset_t;
+
+void
+bitarena_init(bitarena_t *arena, int size);
+
+bitset_t
+bitarena_at(bitarena_t *arena, int idx);
+
+bitset_t
+bitset_and(bitset_t b1, bitset_t b2);
+
+int
+bitset_isZero(bitset_t bitset);
+
+void
+bitset_set(bitset_t bitset, int position);
+
+void
+bitset_unset(bitset_t bitset, int position);
+
+int
+bitset_equals(bitset_t b1, bitset_t b2);
+
+void
+bitset_print(bitset_t bitset);
+
+#endif // bitset_h_INCLUDED
diff --git a/src/main.c b/src/main.c
index ad50fd5..e09cd18 100644
--- a/src/main.c
+++ b/src/main.c
@@ -3,6 +3,7 @@
#include <string.h>
#include "storage.h"
+#include "bitset.h"
typedef struct
{
@@ -29,6 +30,7 @@ initStorage(storage_t *storage)
storage->componentsStored = 0;
storage->objectsStored = 0;
storage->objects = (component_pair_t *) malloc(MAX_COMPONENTS * sizeof(component_pair_t));
+ bitarena_init(&storage->objectSignatures, MAX_OBJECTS);
REGISTER_COMPONENT(storage, transform_t);
REGISTER_COMPONENT(storage, collider_t);
@@ -37,6 +39,19 @@ initStorage(storage_t *storage)
void
loadScene(storage_t *storage)
{
+ bitset_t s1 = bitarena_at(&storage->objectSignatures, 0);
+ bitset_t s2 = bitarena_at(&storage->objectSignatures, 1);
+
+ bitset_set(s1, 9);
+ bitset_set(s2, 9);
+
+ bitset_print(s1);
+ bitset_print(s2);
+
+ bitset_t sAnd = bitset_and(s1, s2);
+ printf("Bitset is zero: %i\n", bitset_isZero(sAnd));
+ printf("Bitset is equals: %i\n", bitset_equals(s1, s2));
+
entity_t e1 = createEntity(storage);
transform_t *t1 = GET_COMPONENT(storage, transform_t, e1);
t1->x = 13;
diff --git a/src/storage.h b/src/storage.h
index 3989c4a..f7f5cd9 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -12,6 +12,9 @@
#define MAX_COMPONENTS 32
#endif
+#define BITSET_BYTES (MAX_COMPONENTS / 8)
+#include "bitset.h"
+
#define GET_COMPONENT(storage, componentType, idx) (componentType *) internal_getComponent(storage, idx, #componentType)
#define REGISTER_COMPONENT(storage, componentType) internal_registerComponent(storage, #componentType, sizeof(componentType))
#define ADD_COMPONENT(storage, idx, componentType) internal_addComponent(storage, idx, #componentType)
@@ -30,6 +33,7 @@ typedef struct
unsigned int componentsStored;
unsigned int objectsStored;
component_pair_t *objects;
+ bitarena_t objectSignatures;
} storage_t;
component_pair_t * // NULL if pair is not found