summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/birthday.c5
-rw-r--r--examples/bits.c10
-rw-r--r--examples/heap.c10
-rw-r--r--examples/list.c2
-rw-r--r--examples/priority.c6
-rw-r--r--examples/queue.c4
-rw-r--r--examples/stack.c9
7 files changed, 25 insertions, 21 deletions
diff --git a/examples/birthday.c b/examples/birthday.c
index 59a47bdb..a5cce918 100644
--- a/examples/birthday.c
+++ b/examples/birthday.c
@@ -19,7 +19,7 @@ void repeats(void)
cmap_ic m = cmap_INIT;
cmap_ic_reserve(&m, N);
clock_t now = clock();
- for (size_t i = 0; i < N; ++i) {
+ c_forrange (i, size_t, N) {
uint64_t k = crand_i64(&rng) & mask;
int v = ++cmap_ic_emplace(&m, k, 0).first->second;
if (v > 1) printf("%zu: %llx - %d\n", i, k, v);
@@ -39,7 +39,8 @@ void distribution(void)
cmap_x map = cmap_x_with_capacity(M);
clock_t now = clock();
crand_uniform_i32_t dist = crand_uniform_i32_init(0, M);
- for (size_t i = 0; i < N; ++i) {
+
+ c_forrange (i, size_t, N) {
++cmap_x_emplace(&map, crand_uniform_i32(&rng, &dist), 0).first->second;
}
float diff = (float) (clock() - now) / CLOCKS_PER_SEC;
diff --git a/examples/bits.c b/examples/bits.c
index 08b45669..f59555e4 100644
--- a/examples/bits.c
+++ b/examples/bits.c
@@ -12,7 +12,7 @@ int main() {
cstr_del(&str);
printf("%4zu: ", set.size);
- for (int i=0; i<set.size; ++i)
+ c_forrange (i, int, set.size)
printf("%d", cbitset_test(set, i));
puts("");
@@ -22,7 +22,7 @@ int main() {
cbitset_resize(&set, 102, true);
cbitset_set_value(&set, 99, false);
printf("%4zu: ", set.size);
- for (int i=0; i<set.size; ++i)
+ c_forrange (i, int, set.size)
printf("%d", cbitset_test(set, i));
puts("\nIterator:");
@@ -37,19 +37,19 @@ int main() {
cbitset_set(&s2, 17);
cbitset_set(&s2, 18);
printf(" new: ");
- for (int i=0; i<s2.size; ++i)
+ c_forrange (i, int, s2.size)
printf("%d", cbitset_test(s2, i));
puts("");
printf(" xor: ");
cbitset_xor_with(&set, s2);
- for (int i=0; i<set.size; ++i)
+ c_forrange (i, int, set.size)
printf("%d", cbitset_test(set, i));
puts("");
cbitset_set_all(&set, false);
printf("%4zu: ", set.size);
- for (int i=0; i<set.size; ++i)
+ c_forrange (i, int, set.size)
printf("%d", cbitset_test(set, i));
puts("");
diff --git a/examples/heap.c b/examples/heap.c
index 44e9b05e..e1225027 100644
--- a/examples/heap.c
+++ b/examples/heap.c
@@ -17,27 +17,27 @@ int main()
pcg = crand_rng32_init(seed);
clock_t start = clock();
- for (int i=0; i<N; ++i)
+ c_forrange (i, int, N)
cvec_f_push_back(&pq, (float) crand_i32(&pcg));
cpqueue_f_build(&pq);
printf("Built priority queue: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC);
- for (int i=0; i<M; ++i)
+ c_forrange (i, int, M)
printf("%.0f ", *cpqueue_f_top(&pq)), cpqueue_f_pop(&pq);
start = clock();
- for (int i=M; i<N; ++i)
+ c_forrange (i, int, M, N)
cpqueue_f_pop(&pq);
printf("\n\npopped PQ: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC);
pcg = crand_rng32_init(seed);
start = clock();
- for (int i=0; i<N; ++i)
+ c_forrange (i, int, N)
cpqueue_f_push(&pq, (float) crand_i32(&pcg));
printf("pushed PQ: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC);
- for (int i=0; i<M; ++i)
+ c_forrange (i, int, M)
printf("%.0f ", *cpqueue_f_top(&pq)), cpqueue_f_pop(&pq);
puts("");
diff --git a/examples/list.c b/examples/list.c
index c2bfec55..e3e68ee4 100644
--- a/examples/list.c
+++ b/examples/list.c
@@ -12,7 +12,7 @@ int main() {
crand_rng64_t eng = crand_rng64_init(1234);
crand_uniform_f64_t dist = crand_uniform_f64_init(100.0f, n);
int m = 0;
- for (int i = 0; i < n; ++i)
+ c_forrange (i, int, n)
clist_fx_push_back(&list, crand_uniform_f64(&eng, &dist)), ++m;
double sum = 0.0;
printf("sumarize %d:\n", m);
diff --git a/examples/priority.c b/examples/priority.c
index 722d3164..bce14f49 100644
--- a/examples/priority.c
+++ b/examples/priority.c
@@ -16,18 +16,18 @@ int main() {
cpqueue_i heap = cpqueue_i_init();
// Push ten million random numbers to priority queue
- for (int i=0; i<N; ++i)
+ c_forrange (i, int, N)
cpqueue_i_push(&heap, crand_uniform_i64(&pcg, &dist));
// push some negative numbers too.
c_push_items(&heap, cpqueue_i, {-231, -32, -873, -4, -343});
- for (int i=0; i<N; ++i)
+ c_forrange (i, int, N)
cpqueue_i_push(&heap, crand_uniform_i64(&pcg, &dist));
// Extract the hundred smallest.
- for (int i=0; i<100; ++i) {
+ c_forrange (i, int, 100) {
printf("%zd ", *cpqueue_i_top(&heap));
cpqueue_i_pop(&heap);
}
diff --git a/examples/queue.c b/examples/queue.c
index fc1c2704..e4426884 100644
--- a/examples/queue.c
+++ b/examples/queue.c
@@ -14,12 +14,12 @@ int main() {
cqueue_i queue = cqueue_i_init();
// Push ten million random numbers onto the queue.
- for (int i=0; i<n; ++i)
+ c_forrange (i, int, 0, n)
cqueue_i_push(&queue, crand_uniform_i32(&rng, &dist));
// Push or pop on the queue ten million times
printf("%d\n", n);
- for (int i=n; i>0; --i) {
+ c_forrange (i, int, n, 0, -1) {
int r = crand_uniform_i32(&rng, &dist);
if (r & 1)
++n, cqueue_i_push(&queue, r);
diff --git a/examples/stack.c b/examples/stack.c
index a911e4a2..68519a9b 100644
--- a/examples/stack.c
+++ b/examples/stack.c
@@ -12,13 +12,16 @@ int main() {
cstack_i stack = cstack_i_init();
cstack_c chars = cstack_c_init();
- c_forrange (i, int, 100)
+ c_forrange (i, int, 101)
cstack_i_push(&stack, i*i);
-
+
+ printf("%d\n", *cstack_i_top(&stack));
+
c_forrange (i, int, 90)
cstack_i_pop(&stack);
c_foreach (i, cstack_i, stack)
- printf("%d\n", *cstack_i_itval(i));
+ printf(" %d", *i.get);
+ puts("");
printf("top: %d\n", *cstack_i_top(&stack));
} \ No newline at end of file