From 3232effeba2bf5ddc7cce1ac8b4c9650b07c66e1 Mon Sep 17 00:00:00 2001
From: Mitya Selivanov <automainint@guattari.tech>
Date: Fri, 2 Dec 2022 01:11:11 +0100
Subject: Clean up array_ref interface; rename options.h to status.h

---
 source/kit/CMakeLists.txt                | 10 ++++-----
 source/kit/array_ref.h                   | 23 +++++---------------
 source/kit/file.h                        |  2 +-
 source/kit/options.h                     | 16 --------------
 source/kit/status.c                      |  1 +
 source/kit/status.h                      | 16 ++++++++++++++
 source/kit/string_ref.h                  |  4 ++--
 source/kit_test/test.c                   |  1 +
 source/test/unittests/array_ref.test.c   | 12 +++++------
 source/test/unittests/lower_bound.test.c | 36 ++++++++++++++++----------------
 10 files changed, 55 insertions(+), 66 deletions(-)
 delete mode 100644 source/kit/options.h
 create mode 100644 source/kit/status.c
 create mode 100644 source/kit/status.h

(limited to 'source')

diff --git a/source/kit/CMakeLists.txt b/source/kit/CMakeLists.txt
index 1dbaaf8..d6a079a 100644
--- a/source/kit/CMakeLists.txt
+++ b/source/kit/CMakeLists.txt
@@ -1,12 +1,13 @@
 target_sources(
   ${KIT_LIBRARY}
     PRIVATE
-      input_buffer.c threads.win32.c time.c atomic.win32.c
-      threads.posix.c condition_variable.c move_back.c input_stream.c
-      lower_bound.c file.c string_ref.c async_function.c allocator.c
-      array_ref.c dynamic_array.c mutex.c mersenne_twister_64.c
+      input_buffer.c status.c threads.win32.c time.c
+      atomic.win32.c threads.posix.c condition_variable.c move_back.c
+      input_stream.c lower_bound.c file.c string_ref.c async_function.c
+      allocator.c array_ref.c dynamic_array.c mutex.c mersenne_twister_64.c
     PUBLIC
       $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/mutex.h>
+      $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/status.h>
       $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/time.h>
       $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/atomic.h>
       $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/allocator.h>
@@ -20,6 +21,5 @@ target_sources(
       $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/input_buffer.h>
       $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/lower_bound.h>
       $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/file.h>
-      $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/options.h>
       $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/array_ref.h>
       $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/mersenne_twister_64.h>)
diff --git a/source/kit/array_ref.h b/source/kit/array_ref.h
index 9501eba..029c20c 100644
--- a/source/kit/array_ref.h
+++ b/source/kit/array_ref.h
@@ -2,6 +2,7 @@
 #define KIT_ARRAY_REF_H
 
 #include <stddef.h>
+#include <stdint.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -19,17 +20,17 @@ int kit_ar_compare(ptrdiff_t left_element_size, ptrdiff_t left_size,
                    ptrdiff_t right_element_size, ptrdiff_t right_size,
                    void const *right_data, kit_ar_compare_fn compare);
 
-#define KIT_AR_MUT(name_, type_) \
+#define KIT_AR_MUT(type_) \
   struct {                       \
     ptrdiff_t size;              \
     type_    *values;            \
-  } name_
+  } 
 
-#define KIT_AR(name_, type_) \
+#define KIT_AR(type_) \
   struct {                   \
     ptrdiff_t    size;       \
     type_ const *values;     \
-  } name_
+  } 
 
 #define KIT_AR_MUT_WRAP(name_, element_type_, array_)           \
   struct {                                                      \
@@ -45,18 +46,6 @@ int kit_ar_compare(ptrdiff_t left_element_size, ptrdiff_t left_size,
   } name_ = { .size   = (sizeof(array_) / sizeof((array_)[0])), \
               .values = (array_) }
 
-#define KIT_AR_MUT_TYPE(name_, element_type_) \
-  typedef struct {                            \
-    ptrdiff_t      size;                      \
-    element_type_ *values;                    \
-  } name_
-
-#define KIT_AR_TYPE(name_, element_type_) \
-  typedef struct {                        \
-    ptrdiff_t            size;            \
-    element_type_ const *values;          \
-  } name_
-
 #define KIT_AR_EQUAL(left_, right_)                              \
   kit_ar_equal_bytes(sizeof((left_).values[0]), (left_).size,    \
                      (left_).values, sizeof((right_).values[0]), \
@@ -77,8 +66,6 @@ int kit_ar_compare(ptrdiff_t left_element_size, ptrdiff_t left_size,
 #  define AR KIT_AR
 #  define AR_MUT_WRAP KIT_AR_MUT_WRAP
 #  define AR_WRAP KIT_AR_WRAP
-#  define AR_TYPE KIT_AR_TYPE
-#  define AR_TYPE_CONST KIT_AR_TYPE_CONST
 #  define AR_EQUAL KIT_AR_EQUAL
 #  define AR_COMPARE KIT_AR_COMPARE
 #endif
diff --git a/source/kit/file.h b/source/kit/file.h
index 6313c2e..ef58d37 100644
--- a/source/kit/file.h
+++ b/source/kit/file.h
@@ -2,7 +2,7 @@
 #define KIT_FILE_H
 
 #include "dynamic_array.h"
-#include "options.h"
+#include "status.h"
 #include "string_ref.h"
 
 #ifdef __cplusplus
diff --git a/source/kit/options.h b/source/kit/options.h
deleted file mode 100644
index d90ea5b..0000000
--- a/source/kit/options.h
+++ /dev/null
@@ -1,16 +0,0 @@
-#ifndef KIT_OPTIONS_H
-#define KIT_OPTIONS_H
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-enum { KIT_OK, KIT_ERROR };
-
-typedef int kit_status_t;
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif
diff --git a/source/kit/status.c b/source/kit/status.c
new file mode 100644
index 0000000..25f84b8
--- /dev/null
+++ b/source/kit/status.c
@@ -0,0 +1 @@
+#include "status.h"
diff --git a/source/kit/status.h b/source/kit/status.h
new file mode 100644
index 0000000..c898e36
--- /dev/null
+++ b/source/kit/status.h
@@ -0,0 +1,16 @@
+#ifndef KIT_STATUS_H
+#define KIT_STATUS_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+enum { KIT_OK, KIT_ERROR };
+
+typedef int kit_status_t;
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/source/kit/string_ref.h b/source/kit/string_ref.h
index dd82f44..2f285dc 100644
--- a/source/kit/string_ref.h
+++ b/source/kit/string_ref.h
@@ -7,8 +7,8 @@
 extern "C" {
 #endif
 
-KIT_AR_MUT_TYPE(kit_string_mut_t, char);
-KIT_AR_TYPE(kit_string_ref_t, char);
+typedef KIT_AR_MUT(char) kit_string_mut_t;
+typedef KIT_AR(char) kit_string_ref_t;
 
 typedef kit_string_mut_t kit_out_str_t;
 typedef kit_string_ref_t kit_str_t;
diff --git a/source/kit_test/test.c b/source/kit_test/test.c
index a47185e..503a85b 100644
--- a/source/kit_test/test.c
+++ b/source/kit_test/test.c
@@ -304,5 +304,6 @@ int kit_run_tests(int argc, char **argv) {
   }
 
   no_color || print_color(white);
+  quiet || printf("\n");
   return status;
 }
diff --git a/source/test/unittests/array_ref.test.c b/source/test/unittests/array_ref.test.c
index ac572b1..c6083ee 100644
--- a/source/test/unittests/array_ref.test.c
+++ b/source/test/unittests/array_ref.test.c
@@ -30,8 +30,8 @@ TEST("array ref equal") {
   int foo[] = { 1, 2, 3, 4, 5, 6, 7 };
   int bar[] = { 3, 4, 5 };
 
-  AR(foo_ref, int) = { .size = 3, .values = foo + 2 };
-  AR(bar_ref, int) = { .size = 3, .values = bar };
+  AR(int) foo_ref = { .size = 3, .values = foo + 2 };
+  AR(int) bar_ref = { .size = 3, .values = bar };
 
   REQUIRE(AR_EQUAL(foo_ref, bar_ref));
 }
@@ -44,8 +44,8 @@ TEST("array ref compare") {
   int foo[] = { 1, 2, 3, 5 };
   int bar[] = { 1, 2, 4, 5 };
 
-  AR(foo_ref, int) = { .size = 3, .values = foo };
-  AR(bar_ref, int) = { .size = 3, .values = bar };
+  AR(int) foo_ref = { .size = 3, .values = foo };
+  AR(int) bar_ref = { .size = 3, .values = bar };
 
   REQUIRE(AR_COMPARE(foo_ref, bar_ref, compare) < 0);
   REQUIRE(AR_COMPARE(bar_ref, foo_ref, compare) > 0);
@@ -56,8 +56,8 @@ TEST("array ref different element sizes") {
   int  foo[] = { 1, 2, 3 };
   char bar[] = { 1, 2, 3 };
 
-  AR(foo_ref, int)  = { .size = 3, .values = foo };
-  AR(bar_ref, char) = { .size = 3, .values = bar };
+  AR(int) foo_ref  = { .size = 3, .values = foo };
+  AR(char) bar_ref = { .size = 3, .values = bar };
 
   REQUIRE(!AR_EQUAL(foo_ref, bar_ref));
   REQUIRE(AR_COMPARE(foo_ref, bar_ref, compare) > 0);
diff --git a/source/test/unittests/lower_bound.test.c b/source/test/unittests/lower_bound.test.c
index 682b870..24752a6 100644
--- a/source/test/unittests/lower_bound.test.c
+++ b/source/test/unittests/lower_bound.test.c
@@ -13,7 +13,7 @@ static int kit_less_int_ref(int const *left, int const *right) {
 }
 
 TEST("lower bound empty") {
-  AR(ref, int) = { .size = 0, .values = NULL };
+  AR(int) ref = { .size = 0, .values = NULL };
 
   ptrdiff_t index;
   LOWER_BOUND(index, ref, 42, kit_less_int);
@@ -22,7 +22,7 @@ TEST("lower bound empty") {
 
 TEST("lower bound single left") {
   int const v[1] = { 42 };
-  AR(ref, int)   = { .size = 1, .values = v };
+  AR(int) ref    = { .size = 1, .values = v };
 
   ptrdiff_t index;
   LOWER_BOUND(index, ref, 42, kit_less_int);
@@ -31,7 +31,7 @@ TEST("lower bound single left") {
 
 TEST("lower bound single right") {
   int const v[1] = { 42 };
-  AR(ref, int)   = { .size = 1, .values = v };
+  AR(int) ref    = { .size = 1, .values = v };
 
   ptrdiff_t index;
   LOWER_BOUND(index, ref, 43, kit_less_int);
@@ -40,7 +40,7 @@ TEST("lower bound single right") {
 
 TEST("lower bound first of four") {
   int const v[4] = { 1, 2, 3, 4 };
-  AR(ref, int)   = { .size = 4, .values = v };
+  AR(int) ref    = { .size = 4, .values = v };
 
   ptrdiff_t index;
   LOWER_BOUND(index, ref, 1, kit_less_int);
@@ -49,7 +49,7 @@ TEST("lower bound first of four") {
 
 TEST("lower bound second of four") {
   int const v[4] = { 1, 2, 3, 4 };
-  AR(ref, int)   = { .size = 4, .values = v };
+  AR(int) ref    = { .size = 4, .values = v };
 
   ptrdiff_t index;
   LOWER_BOUND(index, ref, 2, kit_less_int);
@@ -58,7 +58,7 @@ TEST("lower bound second of four") {
 
 TEST("lower bound third of four") {
   int const v[4] = { 1, 2, 3, 4 };
-  AR(ref, int)   = { .size = 4, .values = v };
+  AR(int) ref    = { .size = 4, .values = v };
 
   ptrdiff_t index;
   LOWER_BOUND(index, ref, 3, kit_less_int);
@@ -67,7 +67,7 @@ TEST("lower bound third of four") {
 
 TEST("lower bound forth of four") {
   int const v[4] = { 1, 2, 3, 4 };
-  AR(ref, int)   = { .size = 4, .values = v };
+  AR(int) ref    = { .size = 4, .values = v };
 
   ptrdiff_t index;
   LOWER_BOUND(index, ref, 4, kit_less_int);
@@ -76,7 +76,7 @@ TEST("lower bound forth of four") {
 
 TEST("lower bound fifth of four") {
   int const v[4] = { 1, 2, 3, 4 };
-  AR(ref, int)   = { .size = 4, .values = v };
+  AR(int) ref    = { .size = 4, .values = v };
 
   ptrdiff_t index;
   LOWER_BOUND(index, ref, 5, kit_less_int);
@@ -85,7 +85,7 @@ TEST("lower bound fifth of four") {
 
 TEST("lower bound first of five") {
   int const v[5] = { 1, 2, 3, 4, 5 };
-  AR(ref, int)   = { .size = 5, .values = v };
+  AR(int) ref    = { .size = 5, .values = v };
 
   ptrdiff_t index;
   LOWER_BOUND(index, ref, 1, kit_less_int);
@@ -94,7 +94,7 @@ TEST("lower bound first of five") {
 
 TEST("lower bound second of five") {
   int const v[5] = { 1, 2, 3, 4, 5 };
-  AR(ref, int)   = { .size = 5, .values = v };
+  AR(int) ref    = { .size = 5, .values = v };
 
   ptrdiff_t index;
   LOWER_BOUND(index, ref, 2, kit_less_int);
@@ -103,7 +103,7 @@ TEST("lower bound second of five") {
 
 TEST("lower bound third of five") {
   int const v[5] = { 1, 2, 3, 4, 5 };
-  AR(ref, int)   = { .size = 5, .values = v };
+  AR(int) ref    = { .size = 5, .values = v };
 
   ptrdiff_t index;
   LOWER_BOUND(index, ref, 3, kit_less_int);
@@ -112,7 +112,7 @@ TEST("lower bound third of five") {
 
 TEST("lower bound forth of five") {
   int const v[5] = { 1, 2, 3, 4, 5 };
-  AR(ref, int)   = { .size = 5, .values = v };
+  AR(int) ref    = { .size = 5, .values = v };
 
   ptrdiff_t index;
   LOWER_BOUND(index, ref, 4, kit_less_int);
@@ -121,7 +121,7 @@ TEST("lower bound forth of five") {
 
 TEST("lower bound fifth of five") {
   int const v[5] = { 1, 2, 3, 4, 5 };
-  AR(ref, int)   = { .size = 5, .values = v };
+  AR(int) ref    = { .size = 5, .values = v };
 
   ptrdiff_t index;
   LOWER_BOUND(index, ref, 5, kit_less_int);
@@ -130,7 +130,7 @@ TEST("lower bound fifth of five") {
 
 TEST("lower bound sixth of five") {
   int const v[5] = { 1, 2, 3, 4, 5 };
-  AR(ref, int)   = { .size = 5, .values = v };
+  AR(int) ref    = { .size = 5, .values = v };
 
   ptrdiff_t index;
   LOWER_BOUND(index, ref, 6, kit_less_int);
@@ -140,7 +140,7 @@ TEST("lower bound sixth of five") {
 TEST("lower bound ref first of four") {
   int const v[4]  = { 1, 2, 3, 4 };
   int const value = 1;
-  AR(ref, int)    = { .size = 4, .values = v };
+  AR(int) ref     = { .size = 4, .values = v };
 
   ptrdiff_t index;
   LOWER_BOUND_REF(index, ref, &value, kit_less_int_ref);
@@ -150,7 +150,7 @@ TEST("lower bound ref first of four") {
 TEST("lower bound ref second of four") {
   int const v[4]  = { 1, 2, 3, 4 };
   int const value = 2;
-  AR(ref, int)    = { .size = 4, .values = v };
+  AR(int) ref     = { .size = 4, .values = v };
 
   ptrdiff_t index;
   LOWER_BOUND_REF(index, ref, &value, kit_less_int_ref);
@@ -160,7 +160,7 @@ TEST("lower bound ref second of four") {
 TEST("lower bound ref fifth of five") {
   int const v[5]  = { 1, 2, 3, 4, 5 };
   int const value = 5;
-  AR(ref, int)    = { .size = 5, .values = v };
+  AR(int) ref     = { .size = 5, .values = v };
 
   ptrdiff_t index;
   LOWER_BOUND_REF(index, ref, &value, kit_less_int_ref);
@@ -170,7 +170,7 @@ TEST("lower bound ref fifth of five") {
 TEST("lower bound ref sixth of five") {
   int const v[5]  = { 1, 2, 3, 4, 5 };
   int const value = 6;
-  AR(ref, int)    = { .size = 5, .values = v };
+  AR(int) ref     = { .size = 5, .values = v };
 
   ptrdiff_t index;
   LOWER_BOUND_REF(index, ref, &value, kit_less_int_ref);
-- 
cgit v1.2.3