tur/sized-buf
SizedBuf: flat contiguous memory layout for sized types.
Since: Phase SZ2; phantom `n` lift Phase SZ8/P3.
SizedBuf
(SizedBuf [n] :int))
sized-buf-new
(sized-buf-new [n])
allocate a heap (SizedBuf n) of n elements (uninitialized).
| k | number of elements to allocate |
A fresh (SizedBuf n) where `n` is polymorphic; pin it via ascription, e.g. `(:: (sized-buf-new 4) (SizedBuf (Static 4)))`.
(let [b (sized-buf-new 4)] ...)
Since: Phase SZ2; (SizedBuf n) lift Phase SZ8/P3
sized-buf-new-zeroed
(sized-buf-new-zeroed [n])
allocate a heap (SizedBuf n), all elements zero.
| k | number of elements to allocate |
A fresh (SizedBuf n) initialised to all zeros.
(let [b (sized-buf-new-zeroed 4)] ...)
Since: Phase SZ2; (SizedBuf n) lift Phase SZ8/P3
sized-buf-free
(sized-buf-free [n])
free a heap-allocated (SizedBuf n) and its data.
| b | (SizedBuf n) from sized-buf-new or sized-buf-new-zeroed |
nil
(sized-buf-free b)
Since: Phase SZ2; (SizedBuf n) lift Phase SZ8/P3
sized-buf-len
(sized-buf-len [n])
return the element count of a (SizedBuf n).
| b | (SizedBuf n) |
Runtime element count as :int. The static `n` index pins the length at compile time when the call site supplies a literal Size.
(sized-buf-len (sized-buf-new 8)) ; => 8
Since: Phase SZ2; (SizedBuf n) lift Phase SZ8/P3
sized-buf-get
(sized-buf-get [n])
bounds-checked element read; aborts on out-of-range index.
| b | (SizedBuf n) | |
| i | zero-based index (0 <= i < sized-buf-len b) |
The int64 value at position i.
(sized-buf-get b 0) ; => first element
Since: Phase SZ2; (SizedBuf n) lift Phase SZ8/P3
sized-buf-set!
(sized-buf-set! [n])
bounds-checked element write; aborts on out-of-range index.
| b | (SizedBuf n) | |
| i | zero-based index | |
| v | value to store |
The same buffer (SizedBuf n) (for chaining).
(sized-buf-set! b 0 42)
Since: Phase SZ2; (SizedBuf n) lift Phase SZ8/P3
sized-buf-fill!
(sized-buf-fill! [n])
set every element to v; returns the same buffer.
| b | (SizedBuf n) | |
| v | value to store in every slot |
The same buffer (SizedBuf n).
(sized-buf-fill! b 0) ; zero-fill
Since: Phase SZ2; (SizedBuf n) lift Phase SZ8/P3
sized-buf-copy!
(sized-buf-copy! [n])
copy all elements from src into dst via memcpy.
| dst | destination (SizedBuf n) | |
| src | source (SizedBuf n) (same n) |
The destination (SizedBuf n).
(sized-buf-copy! dst src)
Since: Phase SZ2; (SizedBuf n) lift Phase SZ8/P3
sized-buf-sum
(sized-buf-sum [n])
sum all elements of a (SizedBuf n).
| b | (SizedBuf n) |
Integer sum of all elements; 0 for an empty buffer.
(sized-buf-sum b) ; => sum of elements
Since: Phase SZ2; (SizedBuf n) lift Phase SZ8/P3
sized-buf-min
(sized-buf-min [n])
minimum element of a non-empty (SizedBuf n); aborts if empty.
| b | (SizedBuf n) (must have at least one element) |
Minimum int64 element value.
(sized-buf-min b) ; => smallest element
Since: Phase SZ2; (SizedBuf n) lift Phase SZ8/P3
sized-buf-max
(sized-buf-max [n])
maximum element of a non-empty (SizedBuf n); aborts if empty.
| b | (SizedBuf n) (must have at least one element) |
Maximum int64 element value.
(sized-buf-max b) ; => largest element
Since: Phase SZ2; (SizedBuf n) lift Phase SZ8/P3
sized-buf-size
(sized-buf-size [n])
return the runtime length of a (SizedBuf n) as a Size expression.
| b | (SizedBuf n) |
A (Static k) where k is the runtime buffer length.
(size-eval (sized-buf-size b)) ; => length of b
Since: Phase SZ2; (SizedBuf n) lift Phase SZ8/P3
sized-buf-with-stack
(sized-buf-with-stack [n f])
scoped computation over a stack-allocated buffer.
| n | number of elements (should be a static/small constant) | |
| f | a non-capturing function (int -> int) that receives the raw | |
| buffer handle |
The integer value returned by f.
(defn sum-init-buf [b] :int
(sized-buf-set! (:: b :SizedBuf) 0 10)
(sized-buf-sum (:: b :SizedBuf)))
(sized-buf-with-stack 3 sum-init-buf)
Since: Phase SZ2
sized-buf-compute
(sized-buf-compute [n])
compute a result from a buffer, choosing stack or heap.
| n | number of elements |
Sum of 0 + 1 + ... + (n-1).
(sized-buf-compute 5) ; => 10 (0+1+2+3+4)
Since: Phase SZ2
sized-buf-from-sized-vec
(sized-buf-from-sized-vec [n])
convert a SizedVec (linked list) to a (SizedBuf n) (flat array).
| v | a SizedVec value (the int64-carried GADT root handle) |
A heap-allocated (SizedBuf n) with the same elements in the same order. Pin `n` via ascription on the result when a static length is known statically.
(let [b (sized-buf-from-sized-vec v)]
(println (sized-buf-sum b)))
Since: Phase SZ2; (SizedBuf n) lift Phase SZ8/P3
Internal definitions
__sized-buf-new-raw__sized-buf-new-zeroed-raw__sized-buf-free-raw__sized-buf-len-raw__sized-buf-get-raw__sized-buf-set!-raw__sized-buf-fill!-raw__sized-buf-copy!-raw__sized-buf-sum-raw__sized-buf-min-raw__sized-buf-max-raw__sized-buf-from-sized-vec-raw__sized-buf-stack-threshold