From 83cd586ea304d6f6aa190c65ee796baaba1941a7 Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Thu, 23 Sep 2021 12:35:04 -0700 Subject: feat: improved interface of map macro --- sys/cmd/cc/cc.c | 2 +- sys/cmd/cc/lex.c | 2 +- sys/libbio/fasta.c | 9 +--- sys/libn/fs.c | 18 +------- sys/libn/gz.c | 21 +++++----- sys/libn/io.c | 28 +++++++++++-- sys/libn/memory.c | 10 ++--- sys/libn/random.c | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 8 files changed, 160 insertions(+), 48 deletions(-) (limited to 'sys') diff --git a/sys/cmd/cc/cc.c b/sys/cmd/cc/cc.c index 3602643..8ad0022 100644 --- a/sys/cmd/cc/cc.c +++ b/sys/cmd/cc/cc.c @@ -397,7 +397,7 @@ main(int argc, byte *argv[]) // NOTE: This is just for my comfort during debugging. pushinclude("/home/nolln/root/include"); pushinclude("/home/nolln/root/include/vendor/libc"); - + src = (argc == 0) ? "" : argv[0]; intern(&src); diff --git a/sys/cmd/cc/lex.c b/sys/cmd/cc/lex.c index 0963fb9..33fc5d0 100644 --- a/sys/cmd/cc/lex.c +++ b/sys/cmd/cc/lex.c @@ -813,7 +813,7 @@ static int moresymtab(SymTab *tab, int n) { - MAP_GROW(tab, string, Sym*, n, PTR_HASH, ·calloc, ·free, nil); + MAP_GROW(tab, string, Sym*, n, PTR_HASH, sys·Memory, nil); } static diff --git a/sys/libbio/fasta.c b/sys/libbio/fasta.c index 078325a..0c47199 100644 --- a/sys/libbio/fasta.c +++ b/sys/libbio/fasta.c @@ -190,7 +190,7 @@ readfasta(bio·SeqReader *rdr, bio·Seq *seq, byte hdr, byte stop) { error err; byte *beg; - + if (rdr->eof && rdr->b == rdr->bend-1) return EOF; @@ -236,13 +236,6 @@ SEQLOOP: rdr->b++; } -#if 0 - for(byte *cb = rdr->seq->b+rdr->seq->off; cb != rdr->seq->it; ++cb) { - if(*cb == 0) { - printf("ERROR @ pos=%ld: Found zero byte\n", cb - rdr->seq->b+rdr->seq->off); - } - } -#endif push(&rdr->seq, rdr->b - beg, beg); diff --git a/sys/libn/fs.c b/sys/libn/fs.c index 10cd93e..e89ef07 100644 --- a/sys/libn/fs.c +++ b/sys/libn/fs.c @@ -12,20 +12,6 @@ struct Key dev_t dev; }; -static -void* -xalloc(void *_, int n, uintptr size) -{ - return malloc(n*size); -} - -static -void -xfree(void *_, void *ptr) -{ - return free(ptr); -} - #define hash(k) ((int32)k.ino ^ (int32)k.dev) #define equal(k1, k2) (k1.ino == k2.ino && k1.dev == k2.dev) @@ -38,7 +24,7 @@ static int morehistory(fs·History *h, int n) { - SET_GROW(h, struct Key, n, hash, xalloc, xfree, nil); + SET_GROW(h, struct Key, n, hash, sys·Memory, nil); } static @@ -62,7 +48,7 @@ static void delete(fs·History *h) { - SET_FREE(h, xfree, nil); + SET_FREE(h, sys·Memory, nil); } #undef hash diff --git a/sys/libn/gz.c b/sys/libn/gz.c index 79bc13f..040d25a 100644 --- a/sys/libn/gz.c +++ b/sys/libn/gz.c @@ -7,15 +7,16 @@ // interface implementations /* actual interfaces */ -io·Reader gz·FileReader = (io·Reader){ gz·read }; -io·Peeker gz·FilePeeker = (io·Peeker){ gz·getbyte, gz·ungetbyte }; -io·FullReader gz·FullFileReader = (io·FullReader){ gz·read, gz·getbyte, gz·ungetbyte }; +io·Reader gz·Reader = (io·Reader){ gz·read }; +io·Peeker gz·Peeker = (io·Peeker){ gz·getbyte, gz·ungetbyte }; +io·Seeker gz·Seeker = (io·Seeker){ gz·seek, gz·tell }; +io·PeekReader gz·Peekreader = (io·PeekReader){ gz·read, gz·getbyte, gz·ungetbyte }; -io·Writer gz·FileWriter = (io·Writer){ gz·write }; -io·Putter gz·FilePutter = (io·Putter){ gz·putbyte, gz·putstring }; -io·FullWriter gz·FullFileWriter = (io·FullWriter){ gz·write, gz·putbyte, gz·putstring }; +io·Writer gz·Writer = (io·Writer){ gz·write }; +io·Putter gz·Putter = (io·Putter){ gz·putbyte, gz·putstring }; +io·PutWriter gz·PutWriter = (io·PutWriter){ gz·write, gz·putbyte, gz·putstring }; -io·ReadWriter gz·FileReadWriter = (io·ReadWriter){ gz·read, gz·write }; +io·ReadWriter gz·ReadWriter = (io·ReadWriter){ gz·read, gz·write }; // ----------------------------------------------------------------------- // functions implementations @@ -69,13 +70,13 @@ gz·write(gz·Stream *s, int sz, int n, void* buf) return gzwrite(s, buf, n*sz); } -error +error gz·putbyte(gz·Stream *s, byte c) { return gzputc(s, c); } -error +error gz·putstring(gz·Stream *s, byte *str) { return gzputs(s, str); @@ -100,7 +101,7 @@ gz·flush(gz·Stream *s) return gzflush(s, Z_FINISH); } -vlong +int gz·seek(gz·Stream *s, long off, enum SeekPos whence) { return gzseek(s, off, whence); diff --git a/sys/libn/io.c b/sys/libn/io.c index f0d270e..ff4fe67 100644 --- a/sys/libn/io.c +++ b/sys/libn/io.c @@ -40,21 +40,37 @@ error return io·putbyte((io·Stream *)wtr, c); } -static +static int ·puts(void *wtr, string s) { return io·putstring((io·Stream *)wtr, s); } +static +int +·seek(void *skr, long off, enum SeekPos whence) +{ + return io·seek((io·Stream *)skr, off, whence); +} + +static +long +·tell(void *skr) +{ + return io·tell((io·Stream *)skr); +} + /* actual interfaces */ io·Reader sys·Reader = (io·Reader){ ·read }; +io·Seeker sys·Seeker = (io·Seeker){ ·seek, ·tell }; io·Peeker sys·Peeker = (io·Peeker){ ·get, ·unget }; -io·FullReader sys·FullReader = (io·FullReader){ ·read, ·get, ·unget }; +io·SeekReader sys·SeekReader = (io·SeekReader){ ·seek, ·tell, ·read }; +io·PeekReader sys·PeekReader = (io·PeekReader){ ·read, ·get, ·unget }; io·Writer sys·Writer = (io·Writer){ ·write }; io·Putter sys·Putter = (io·Putter){ ·put, ·puts }; -io·FullWriter sys·FullWriter = (io·FullWriter){ ·write, ·put, ·puts }; +io·PutWriter sys·PutWriter = (io·PutWriter){ ·write, ·put, ·puts }; io·ReadWriter sys·ReadWriter = (io·ReadWriter){ ·read, ·write }; @@ -152,3 +168,9 @@ io·seek(io·Stream *s, long off, enum SeekPos origin) { return fseek(s, off, origin); } + +long +io·tell(io·Stream *s) +{ + return ftell(s); +} diff --git a/sys/libn/memory.c b/sys/libn/memory.c index 1c7ab07..795fe1f 100644 --- a/sys/libn/memory.c +++ b/sys/libn/memory.c @@ -1,19 +1,19 @@ #include #include -static -void +static +void ·free(void* _, void* ptr) { return free(ptr); } -static +static void * ·alloc(void* _, uint n, ulong size) { return malloc(n*size); } -static +static void * ·calloc(void* _, uint n, ulong size) { return calloc(n, size); @@ -26,7 +26,7 @@ void * } mem·Allocator sys·Memory = { - .alloc = ·alloc, + .alloc = ·calloc, .free = ·free }; diff --git a/sys/libn/random.c b/sys/libn/random.c index 551d1e9..237127e 100644 --- a/sys/libn/random.c +++ b/sys/libn/random.c @@ -107,17 +107,127 @@ rng·randi(int max) return r % max; } +/* + * Ahrens, J. H., & Dieter, U. (1982). + * Computer Generation of Poisson Deviates from Modified Normal Distributions. + */ +static double factorial[10] = {1., 1., 2., 6., 24., 120., 720., 5040., 40320., 362880.}; +static double coeffs[9] = { + -.500000000, +.333333333, -.249999856, + +.200011780, -.166684875, +.142187833, + -.124196313, +.125005956, -.114265030, +}; + +static inline +double +log1pmx(double x, double off) +{ + int i; + double r, t; + + if(-0.25 < x && x < 0.25) { + r = 0; + t = 1; + for(i=0;i=L) + return K; +stepS: + U = rng·random(); + if(d*U >= (mu-K)*(mu-K)*(mu-K)) + return K; +stepP: + if(G < 0) + goto stepE; +stepQ: + c = procf(mu, s, K, &px, &py, &fx, &fy); +stepE: + E = rng·exponential(1.0); + U = rng·random(); + U = U + U - 1; + T = 1.8 + copysign(E,U); + if(T < 0.6744) + goto stepE; + K = floor(mu + s*T); + c = procf(mu, s, K, &px, &py, &fx, &fy); +stepH: + if(c*fabs(U) > (py*exp(px + E) - fy*exp(fx + E))) + goto stepE; + return K; +} + uint64 rng·poisson(double mean) { - uint64 n; - double c; + int64 n; + double z; if(mean<10.0) { - for(n=0, c=rng·exponential(1.0); c