tur/sized-buf
SizedBuf: flat contiguous memory layout for sized types.
Since: Phase SZ2
sized-buf-new
(sized-buf-new [n])
allocate a heap SizedBuf of n elements (uninitialized).
| n | number of elements to allocate |
Opaque pointer to the new SizedBuf.
(let [b (sized-buf-new 4)] ...)
Since: Phase SZ2
sized-buf-new-zeroed
(sized-buf-new-zeroed [n])
allocate a heap SizedBuf of n elements, all zero.
| n | number of elements to allocate |
Opaque pointer to the new SizedBuf with all elements set to 0.
(let [b (sized-buf-new-zeroed 4)] ...)
Since: Phase SZ2
sized-buf-free
(sized-buf-free [b])
free a heap-allocated SizedBuf and its data.
| b | SizedBuf pointer from sized-buf-new or sized-buf-new-zeroed |
nil
(sized-buf-free b)
Since: Phase SZ2
sized-buf-len
(sized-buf-len [b])
return the number of elements in a SizedBuf.
| b | SizedBuf pointer |
Element count as :int.
(sized-buf-len (sized-buf-new 8)) ; => 8
Since: Phase SZ2
sized-buf-get
(sized-buf-get [b i])
bounds-checked element read; aborts on out-of-range index.
| b | SizedBuf pointer | |
| 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
sized-buf-set!
(sized-buf-set! [b i v])
bounds-checked element write; aborts on out-of-range index.
| b | SizedBuf pointer | |
| i | zero-based index | |
| v | value to store |
The same buffer pointer b (for chaining).
(sized-buf-set! b 0 42)
Since: Phase SZ2
sized-buf-fill!
(sized-buf-fill! [b v])
set every element to v; returns the same buffer.
| b | SizedBuf pointer | |
| v | value to store in every slot |
The same buffer pointer b.
(sized-buf-fill! b 0) ; zero-fill
Since: Phase SZ2
sized-buf-copy!
(sized-buf-copy! [dst src])
copy all elements from src into dst via memcpy.
| dst | destination SizedBuf | |
| src | source SizedBuf (same length as dst) |
The destination buffer pointer dst.
(sized-buf-copy! dst src)
Since: Phase SZ2
sized-buf-sum
(sized-buf-sum [b])
sum all elements of a SizedBuf.
| b | SizedBuf pointer |
Integer sum of all elements; 0 for an empty buffer.
(sized-buf-sum b) ; => sum of elements
Since: Phase SZ2
sized-buf-min
(sized-buf-min [b])
minimum element of a non-empty SizedBuf; aborts if empty.
| b | SizedBuf pointer (must have at least one element) |
Minimum int64 element value.
(sized-buf-min b) ; => smallest element
Since: Phase SZ2
sized-buf-max
(sized-buf-max [b])
maximum element of a non-empty SizedBuf; aborts if empty.
| b | SizedBuf pointer (must have at least one element) |
Maximum int64 element value.
(sized-buf-max b) ; => largest element
Since: Phase SZ2
sized-buf-size
(sized-buf-size [b])
return the length of a SizedBuf as a Size expression.
| b | SizedBuf pointer |
A (Static n) where n is the buffer length.
(size-eval (sized-buf-size b)) ; => length of b
Since: Phase SZ2
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 buffer |
The integer value returned by f.
(defn sum-init-buf [b] :int
(sized-buf-set! b 0 10)
(sized-buf-set! b 1 20)
(sized-buf-set! b 2 30)
(sized-buf-sum b))
(sized-buf-with-stack 3 sum-init-buf) ; => 60
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 [v])
convert a SizedVec (linked list) to a SizedBuf (flat array).
| v | a (SizedVec Size int) value |
A heap-allocated SizedBuf with the same elements in the same order.
(let [b (unsafe (sized-buf-from-sized-vec (sized-vec-of-3 10 20 30)))]
(println (sized-buf-sum b))) ; => 60
Since: Phase SZ2
Internal definitions
__sized-buf-stack-threshold