aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMistivia <i@mistivia.com>2025-06-09 14:18:20 +0800
committerMistivia <i@mistivia.com>2025-06-09 14:18:20 +0800
commita690e564d82a46c4e729d88fcc660e4e2f1e6ceb (patch)
treec40c404eb61a73168050d7b57fd5bbd5709084dd
parent17690b812b7d59a7f37c858a55b25be91a02ff4c (diff)
add show trait
-rw-r--r--Makefile2
-rw-r--r--src/basic_traits.c36
-rw-r--r--src/basic_traits.h5
-rw-r--r--src/type_alias.h8
-rw-r--r--src/vec.c2
-rw-r--r--src/vec.h13
-rw-r--r--tests/test_pqueue.c3
-rw-r--r--tests/test_vec.c4
8 files changed, 65 insertions, 8 deletions
diff --git a/Makefile b/Makefile
index 323afd9..994c84f 100644
--- a/Makefile
+++ b/Makefile
@@ -6,7 +6,7 @@ ifeq ($(mode), debug)
-g \
-fsanitize=address
else
- cflags = -O2
+ cflags = -flto -O2
endif
src = $(shell ls src/*.c)
diff --git a/src/basic_traits.c b/src/basic_traits.c
index d46de15..b7c4d3a 100644
--- a/src/basic_traits.c
+++ b/src/basic_traits.c
@@ -15,7 +15,7 @@
} \
uint64_t T##_hash(T x) { \
return mmhash(&x, sizeof(T), 0); \
- }
+ } \
BASIC_TRAITS_IMPL(Char);
BASIC_TRAITS_IMPL(Bool);
@@ -27,6 +27,38 @@ BASIC_TRAITS_IMPL(Double);
BASIC_TRAITS_IMPL(Float);
BASIC_TRAITS_IMPL(VoidPtr);
+void Char_show(Char self, FILE* fp) {
+ fprintf(fp, "%c", self);
+}
+void Bool_show(Bool self, FILE* fp) {
+ if (self) fprintf(fp, "true");
+ else fprintf(fp, "false");
+}
+void Int_show(Int self, FILE* fp) {
+ fprintf(fp, "%d", self);
+}
+void Long_show(Long self, FILE* fp) {
+ fprintf(fp, "%lld", self);
+}
+void UInt_show(UInt self, FILE* fp) {
+ fprintf(fp, "%ud", self);
+}
+void ULong_show(ULong self, FILE* fp) {
+ fprintf(fp, "%llud", self);
+}
+void VoidPtr_show(VoidPtr self, FILE* fp) {
+ fprintf(fp, "%p", self);
+}
+void Double_show(Double self, FILE* fp) {
+ fprintf(fp, "%lf", self);
+}
+void Float_show(Float self, FILE* fp) {
+ fprintf(fp, "%f", self);
+}
+void String_show(String self, FILE* fp) {
+ fprintf(fp, "%s", self);
+}
+
bool String_eq(String lhs, String rhs) {
return strcmp(lhs, rhs) == 0;
}
@@ -35,7 +67,7 @@ int String_cmp(String lhs, String rhs) {
return strcmp(lhs, rhs);
}
-ULong String_hash(String x) {
+uint64_t String_hash(String x) {
size_t len = strlen(x);
return mmhash(x, len, 0);
}
diff --git a/src/basic_traits.h b/src/basic_traits.h
index 700a49a..45aaba9 100644
--- a/src/basic_traits.h
+++ b/src/basic_traits.h
@@ -1,13 +1,16 @@
#ifndef ALGDS_BAISC_TRAITS_H_
#define ALGDS_BAISC_TRAITS_H_
+#include <stdio.h>
+
#include "type_alias.h"
// basic traits
#define BASIC_TRAITS_DEF(T) \
Bool T##_eq(T lhs, T rhs); \
Int T##_cmp(T lhs, T rhs); \
- ULong T##_hash(T x);
+ uint64_t T##_hash(T x); \
+ void T##_show(T x, FILE* fp); \
BASIC_TRAITS_DEF(Int);
BASIC_TRAITS_DEF(Bool);
diff --git a/src/type_alias.h b/src/type_alias.h
index 8f818ed..fbee6ef 100644
--- a/src/type_alias.h
+++ b/src/type_alias.h
@@ -5,10 +5,10 @@
#include <stdbool.h>
typedef bool Bool;
-typedef int32_t Int;
-typedef int64_t Long;
-typedef uint32_t UInt;
-typedef uint64_t ULong;
+typedef int Int;
+typedef long long Long;
+typedef unsigned int UInt;
+typedef unsigned long long ULong;
typedef char Char;
typedef float Float;
typedef double Double;
diff --git a/src/vec.c b/src/vec.c
index da3d9e0..9e9abe5 100644
--- a/src/vec.c
+++ b/src/vec.c
@@ -2,6 +2,8 @@
#include <string.h>
+#include "basic_traits.h"
+
VECTOR_IMPL(Int);
VECTOR_IMPL(Bool);
VECTOR_IMPL(Long);
diff --git a/src/vec.h b/src/vec.h
index ba235e1..9d32136 100644
--- a/src/vec.h
+++ b/src/vec.h
@@ -2,6 +2,7 @@
#define ALGDS_VEC_H_
#include <stdlib.h>
+#include <stdio.h>
#include "type_alias.h"
@@ -23,6 +24,7 @@
T* T##Vector_ref(T##Vector *self, size_t n); \
void T##Vector_swap(T##Vector *self, int i, int j); \
T##Vector T##Vector_move(T##Vector *self); \
+ void T##Vector_show(T##Vector *self, FILE* fp); \
void T##Vector_free(T##Vector *self);
#define VECTOR_IMPL(T) \
@@ -81,6 +83,17 @@
self->cap = 0; \
return dup; \
} \
+ void T##Vector_show(T##Vector *self, FILE* fp) { \
+ fprintf(fp, "["); \
+ for (int i = 0; i < self->size-1; i++) { \
+ T##_show(self->buffer[i], fp); \
+ fprintf(fp, ", "); \
+ } \
+ if (self->size > 1) { \
+ T##_show(self->buffer[self->size - 1], fp); \
+ } \
+ fprintf(fp, "]"); \
+ } \
void T##Vector_free(T##Vector *self) { free(self->buffer); }
VECTOR_DEF(Int);
diff --git a/tests/test_pqueue.c b/tests/test_pqueue.c
index 436a583..3d61552 100644
--- a/tests/test_pqueue.c
+++ b/tests/test_pqueue.c
@@ -11,6 +11,9 @@ typedef Int MinInt;
int MinInt_cmp(Int lhs, Int rhs) {
return -Int_cmp(lhs, rhs);
}
+void MinInt_show(Int self, FILE* fp) {
+ Int_show(self, fp);
+}
VECTOR_DEF(MinInt);
VECTOR_IMPL(MinInt);
diff --git a/tests/test_vec.c b/tests/test_vec.c
index 6abdd78..4aafc72 100644
--- a/tests/test_vec.c
+++ b/tests/test_vec.c
@@ -10,6 +10,10 @@ int main() {
IntVector_init(&vec);
for (int i = 0; i < 1000; i++) {
+ if (i == 10) {
+ IntVector_show(&vec, stdout);
+ puts("");
+ }
assert(vec.size == i);
IntVector_push_back(&vec, i);
assert(*(IntVector_end(&vec) - 1) == i);