Paste: Simple mmap counter
Author: | tsculpt |
Mode: | factor |
Date: | Sun, 28 Feb 2010 04:44:31 |
Plain Text |
USING: alien.c-types arrays command-line io io.encodings.binary
io.files io.files.temp io.mmap kernel math math.order math.parser
memoize namespaces sequences specialized-arrays system ;
IN: mmap-count
SPECIALIZED-ARRAY: int
MEMO: mmap-count-path ( -- path ) "counts" temp-file ;
CONSTANT: num-counters 10
: ensure-counts-file ( -- )
mmap-count-path exists? [
num-counters <int-array>
mmap-count-path binary set-file-contents
] unless ;
: increment-at ( n -- )
mmap-count-path int [
[ [ 1 + ] change-nth ]
[ nth number>string print ] 2bi
] with-mapped-array ;
: check-args ( -- )
command-line get
[ length 1 = ] [ first string>number f = not ] bi
[ t = ] both? [ "Usage: mmap-count <n>" print 1 exit ] unless ;
: exit-out-of-bounds ( n -- )
number>string write " not in counter range [0," num-counters 1 -
number>string "]" 3array "" join print 1 exit ;
: handle-command ( -- )
check-args command-line get first string>number
dup 0 num-counters 1 - between?
[ ensure-counts-file increment-at ] [ exit-out-of-bounds ] if ;
MAIN: handle-command
Author: | tsculpt |
Mode: | factor |
Date: | Sun, 28 Feb 2010 04:58:44 |
Plain Text |
USING: alien.c-types arrays command-line io io.encodings.binary
io.files io.files.temp io.mmap kernel math math.order math.parser
memoize namespaces sequences specialized-arrays system ;
IN: mmap-count
SPECIALIZED-ARRAY: int
MEMO: mmap-count-path ( -- path ) "counts" temp-file ;
CONSTANT: num-counters 10
: ensure-counts-file ( -- )
mmap-count-path exists? [
num-counters <int-array>
mmap-count-path binary set-file-contents
] unless ;
: increment-at ( n -- )
mmap-count-path int [
[ [ 1 + ] change-nth ]
[ nth number>string print ] 2bi
] with-mapped-array ;
: check-args ( -- )
command-line get
[ length 1 = ] [ first string>number ] bi and
[ "Usage: mmap-count <n>" print 1 exit ] unless ;
: exit-out-of-bounds ( n -- )
number>string write " not in counter range [0," num-counters 1 -
number>string "]" 3array "" join print 1 exit ;
: handle-command ( -- )
check-args command-line get first string>number
dup 0 num-counters 1 - between?
[ ensure-counts-file increment-at ] [ exit-out-of-bounds ] if ;
MAIN: handle-command
New Annotation