diff options
| author | Tyge Løvset <[email protected]> | 2020-07-21 00:20:41 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2020-07-21 00:20:41 +0200 |
| commit | 418e8417f3b5d2f005fc94f14165e1cb5123ca8c (patch) | |
| tree | d6d6dc5ff54f9fd191a359f029f9bd0025f9c513 | |
| parent | f8db84e01d704b47f09dde56b6731de7bcca7658 (diff) | |
| download | STC-modified-418e8417f3b5d2f005fc94f14165e1cb5123ca8c.tar.gz STC-modified-418e8417f3b5d2f005fc94f14165e1cb5123ca8c.zip | |
Updated prime.c example and clist doc/example.
| -rw-r--r-- | examples/prime.c | 31 | ||||
| -rw-r--r-- | stc/clist.h | 24 |
2 files changed, 25 insertions, 30 deletions
diff --git a/examples/prime.c b/examples/prime.c index f327fb4e..803c772a 100644 --- a/examples/prime.c +++ b/examples/prime.c @@ -3,29 +3,20 @@ #include <stdlib.h>
#include <stdio.h>
#include <string.h>
-#include "stc/cvec.h"
-
-static inline void setBit(uint32_t* a, const size_t i) { a[i >> 5] |= 1u << (i & 31); }
-static inline void clearBit(uint32_t* a, const size_t i) { a[i >> 5] &= ~(1u << (i & 31)); }
-static inline bool testBit(uint32_t* a, const size_t i) { return (a[i >> 5] & (1u << (i & 31))) != 0; }
-
-declare_CVec(i, uint32_t);
+#include <stc/cbitvec.h>
static inline void sieveOfEratosthenes(size_t n)
{
- CVec_i prime = cvec_i_make((n >> 5) + 1, 0xffffffff);
-
- printf("n: %zu, ints: %zu\n", n, cvec_size(prime));
- clearBit(prime.data, 0);
- clearBit(prime.data, 1);
+ CBitVec prime = cbitvec_make(n + 1, true);
+ printf("computing primes up to %zu\n", n);
+ cbitvec_unset(&prime, 0);
+ cbitvec_unset(&prime, 1);
- for (size_t i = 2; i <= n; ++i)
- {
+ for (size_t i = 2; i <= n; ++i) {
// If prime[i] is not changed, then it is a prime
- if (testBit(prime.data, i) && i*i <= n)
- {
+ if (cbitvec_value(&prime, i) && i*i <= n) {
for (size_t j = i*i; j <= n; j += i) {
- clearBit(prime.data, j);
+ cbitvec_unset(&prime, j);
}
}
}
@@ -33,10 +24,10 @@ static inline void sieveOfEratosthenes(size_t n) // Print all prime numbers
size_t count = 0;
for (size_t i = 1; i <= n; ++i)
- if (testBit(prime.data, i)) ++count; // printf("%zu\n", i);
+ if (cbitvec_value(&prime, i)) ++count; // printf("%zu\n", i);
- printf("primes: %zu\n", count);
- cvec_i_destroy(&prime);
+ printf("number of primes: %zu\n", count);
+ cbitvec_destroy(&prime);
}
int main(void)
diff --git a/stc/clist.h b/stc/clist.h index bfd90e34..ee7960c1 100644 --- a/stc/clist.h +++ b/stc/clist.h @@ -32,23 +32,27 @@ it also support push* and splice* at both ends of the list. This makes it ideal
for being used as a queue, unlike std::forward_list. Basic usage is similar to CVec:
- #include "stc/clist.h"
- declare_CList(i, int64_t);
+ #include <stdio.h>
+ #include <stc/clist.h>
+ #include <stc/crandom.h>
+ declare_CList(ix, int64_t);
int main() {
- CList_i list = clist_init;
+ CList_ix list = clist_init;
+ pcg32_random_t pcg = pcg32_seed(123, 0);
int n;
for (int i=0; i<1000000; ++i) // one million
- clist_i_pushBack(&list, rand() * rand());
+ clist_ix_pushBack(&list, pcg32_random(&pcg));
n = 0;
- c_foreach (i, clist_i, list)
- if (++n % 10000 == 0) printf("%d: %zd\n", n, i.item->value);
+ c_foreach (i, clist_ix, list)
+ if (++n % 10000 == 0) printf("%8d: %10zd\n", n, i.item->value);
// Sort them...
- clist_i_sort(&list); // mergesort O(n*log n)
+ clist_ix_sort(&list); // mergesort O(n*log n)
n = 0;
- c_foreach (i, clist_i, list)
- if (++n % 10000 == 0) printf("%d: %zd\n", n, i.item->value);
- clist_i_destroy(&list);
+ puts("sorted");
+ c_foreach (i, clist_ix, list)
+ if (++n % 10000 == 0) printf("%8d: %10zd\n", n, i.item->value);
+ clist_ix_destroy(&list);
}
*/
|
