Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add inline attributes for Vector and Slice #1377

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions library/slice.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@
(%length (:a -> UFix)))

(define-instance (Sliceable (Vector :a))
(inline)
(define %length vector:length))

(define-instance (Sliceable (Slice :a))
(inline)
(define %length length))

(declare new ((Sliceable (:b :a)) => UFix -> UFix -> :b :a -> Slice :a))
Expand All @@ -67,12 +69,14 @@
:displaced-to v
:displaced-index-offset start)))

(inline)
(declare length (Slice :a -> UFix))
(define (length s)
"Returns the length of `s`."
(lisp UFix (s)
(cl:array-dimension s 0)))

(inline)
(declare set! (UFix -> :a -> (Slice :a) -> Unit))
(define (set! idx item s)
"Set the element at index `idx` in `s` to `item`."
Expand All @@ -87,6 +91,7 @@
None
(Some (index-unsafe idx s))))

(inline)
(declare index-unsafe (UFix -> (Slice :a) -> :a))
(define (index-unsafe idx s)
"Lookup the element at `index` in `s` without bounds checking."
Expand Down Expand Up @@ -199,12 +204,14 @@
:from-end cl:t))))

(define-instance (Into (Slice :a) (Vector :a))
(inline)
(define (into s)
(let v = (vector:with-capacity (length s)))
(vector:extend! v (iter:into-iter s))
v))

(define-instance (Into (Vector :a) (Slice :a))
(inline)
(define (into v)
(new 0 (vector:length v) v)))

Expand Down
27 changes: 26 additions & 1 deletion library/vector.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@
(repr :native (cl:and (cl:vector cl:t) (cl:not cl:simple-vector)))
(define-type (Vector :a))

(inline)
(declare new (Unit -> Vector :a))
(define (new _)
"Create a new empty vector"
(with-capacity 0))

(inline)
(declare with-capacity (UFix -> Vector :a))
(define (with-capacity n)
"Create a new vector with `n` elements preallocated."
Expand All @@ -76,33 +78,39 @@
(extend! v (iter:repeat-for x n))
v)

(inline)
(declare singleton (:a -> Vector :a))
(define (singleton x)
"Create a new vector with a single element equal to `x`"
(with-initial-element 1 x))

(inline)
(declare length (Vector :a -> UFix))
(define (length v)
"Returns the length of `v`."
(lisp UFix (v)
(cl:length v)))

(inline)
(declare capacity (Vector :a -> UFix))
(define (capacity v)
"Returns the number of elements that `v` can store without resizing."
(lisp UFix (v)
(cl:array-dimension v 0)))

(inline)
(declare empty? (Vector :a -> Boolean))
(define (empty? v)
"Is `v` empty?"
(== 0 (length v)))

(inline)
(declare singleton? (Vector :a -> Boolean))
(define (singleton? v)
"Is `v` a singleton?"
(== 1 (length v)))

(inline)
(declare copy (Vector :a -> Vector :a))
(define (copy v)
"Return a new vector containing the same elements as `v`."
Expand All @@ -121,11 +129,13 @@
(cl:adjust-array v new-capacity :fill-pointer shrinking)
Unit))

(inline)
(declare clear! (Vector :a -> Unit))
(define (clear! v)
"Set the capacity of `v` to `0`."
(set-capacity! 0 v))

(inline)
(declare push! (:a -> Vector :a -> UFix))
(define (push! item v)
"Append `item` to `v` and resize `v` if necessary, returning the index of the new item."
Expand All @@ -139,6 +149,7 @@
None
(Some (pop-unsafe! v))))

(inline)
(declare pop-unsafe! (Vector :a -> :a))
(define (pop-unsafe! v)
"Remove and return the last item of `v` without checking if the vector is empty."
Expand All @@ -152,24 +163,28 @@
None
(Some (index-unsafe index v))))

(inline)
(declare index-unsafe (UFix -> Vector :a -> :a))
(define (index-unsafe idx v)
"Return the `idx`th element of `v` without checking if the element exists."
(lisp :a (idx v)
(cl:aref v idx)))

(inline)
(declare set! (UFix -> :a -> Vector :a -> Unit))
(define (set! idx item v)
"Set the `idx`th element of `v` to `item`. This function left intentionally unsafe because it does not have a return value to check."
(lisp Void (idx item v)
(cl:setf (cl:aref v idx) item))
Unit)

(inline)
(declare head (Vector :a -> Optional :a))
(define (head v)
"Return the first item of `v`."
(index 0 v))

(inline)
(declare head-unsafe (Vector :a -> :a))
(define (head-unsafe v)
"Return the first item of `v` without first checking if `v` is empty."
Expand Down Expand Up @@ -235,6 +250,7 @@
(call-coalton-function f a b))))
Unit)

(inline)
(declare sort! (Ord :a => Vector :a -> Unit))
(define (sort! v)
"Sort a vector in-place in ascending order."
Expand Down Expand Up @@ -270,6 +286,7 @@
(iter:every! id (iter:zip-with! == (iter:into-iter v1) (iter:into-iter v2))))))

(define-instance (Semigroup (Vector :a))
(inline)
(define <> append))

(define-instance (Functor Vector)
Expand Down Expand Up @@ -300,16 +317,22 @@
:from-end cl:t))))

(define-instance (ram:RandomAccess (Vector :t) :t)
(inline)
(define (ram:make n x)
(with-initial-element n x))
(inline)
(define (ram:length a)
(length a))
(inline)
(define (ram:readable? _)
True)
(inline)
(define (ram:writable? _)
True)
(inline)
(define (ram:unsafe-aref a n)
(index-unsafe n a))
(inline)
(define (ram:unsafe-set! a n x)
(set! n x a)))

Expand All @@ -329,8 +352,9 @@
out))))

(define-instance (Into (Vector :a) (List :a))
(inline)
(define (into v)
(iter:collect! (iter:into-iter v))))
(iter:collect! (iter:into-iter v))))

(define-instance (Iso (Vector :a) (List :a)))

Expand All @@ -355,6 +379,7 @@
vec))

(define-instance (Default (Vector :a))
(inline)
(define default new)))

(cl:defmacro make (cl:&rest elements)
Expand Down