summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2020-07-21 00:20:41 +0200
committerTyge Løvset <[email protected]>2020-07-21 00:20:41 +0200
commit418e8417f3b5d2f005fc94f14165e1cb5123ca8c (patch)
treed6d6dc5ff54f9fd191a359f029f9bd0025f9c513 /examples
parentf8db84e01d704b47f09dde56b6731de7bcca7658 (diff)
downloadSTC-modified-418e8417f3b5d2f005fc94f14165e1cb5123ca8c.tar.gz
STC-modified-418e8417f3b5d2f005fc94f14165e1cb5123ca8c.zip
Updated prime.c example and clist doc/example.
Diffstat (limited to 'examples')
-rw-r--r--examples/prime.c31
1 files changed, 11 insertions, 20 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)