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 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 " 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