Skip to content

Commit

Permalink
update reference
Browse files Browse the repository at this point in the history
  • Loading branch information
TimWhiting committed Dec 2, 2024
1 parent b8b671e commit 357b63a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion std/core-extras.kk
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ pub fun a/unsafe-swap( v : vector<a>, ^idx1 : int, ^idx2 : int ) : vector<a>
v'.unsafe-assign( idx2.ssize_t, item1 )
v'

// This variant mutates inplace
// This variant mutates in-place
pub fun unit/unsafe-swap( v : vector<a>, ^idx1 : int, ^idx2 : int ) : ()
val item1 = v.unsafe-idx( idx1.ssize_t )
v.unsafe-assign( idx1.ssize_t, v.unsafe-idx( idx2.ssize_t ))
Expand Down
4 changes: 2 additions & 2 deletions std/data/deque.kk
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,13 @@ pub fun (==)( xs : deque<a>, ys : deque<a>, ?(==) : (a, a) -> bool ) : bool
Nothing -> True
Just(x) -> x

ref struct something {i: int}
reference struct something {i: int}
fun test-deque()
basic/test("deque push-front")
val deq = unit/deque()
val deq' = deq.push-front(2)
val value = deq'.at(0)
expect(Just(2), { value }, details="Expected Just(2) but got " ++ value.show )
expect(Just(2), { value }, details="Expected Just(2) but got " ++ value.show)
basic/test("deque push-back")
val deq = unit/deque()
val deq' = deq.push-back(2)
Expand Down
6 changes: 4 additions & 2 deletions std/data/rbtree-bu.kk
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,7 @@ pub fip(1) fun bu/set(t: rbtree<k,v>, key: k, value: v, ^?order2: (k,k) -> e ord
pub fip(1) fun bu/add(t: rbtree<k,v>, key: k, value: v, ^?order2: (k,k) -> e order2<k>): e rbtree<k,v>
t.add(key, value, Done)

pub fip(1) fun bu/insert(t: rbtree<k,v>, key: k, f: (maybe<v>) -> e v, ^?order2: (k,k) -> e order2<k>): e rbtree<k,v>
t.insert(key, Done, f)
pub fip(1) fun bu/insert(t: rbtree<k,v>, key: k, ^f: (maybe<v>) -> e v, ^?order2: (k,k) -> e order2<k>): e rbtree<k,v>
t.insert(key, Done, f)


16 changes: 8 additions & 8 deletions std/data/rbtree.kk
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub type dtree<k,v>
DNodeL(color : color, dhole : dhole, key : k, value : v, rchild : rbtree<k,v>)
NodeNone // for convenience

pub ref type dhole // define as a reference type so the derivative tree constructors can reuse with Node's
pub reference type dhole // define as a reference type so the derivative tree constructors can reuse with Node's
Hole

// Common functionality
Expand All @@ -48,10 +48,10 @@ pub fun values(t: rbtree<k,v>) : list<v>
Leaf -> []
Node(_,l,_,v,r) -> Cons(v, l.values ++ r.values)

pub fun empty() : rbtree<k,v>
pub inline fun empty() : rbtree<k,v>
Leaf

pub fip fun set-black( t : rbtree<k,v> ) : rbtree<k,v>
pub inline fip fun set-black( t : rbtree<k,v> ) : rbtree<k,v>
match t
Node(_,l,x,v,r) -> Node(Black,l,x,v,r)
Leaf -> Leaf
Expand All @@ -74,13 +74,13 @@ pub fun lookup(t: rbtree<k,v>, key: k, ^?order2: (k,k) -> e order2<k>) : e maybe
Gt2 -> lookup(r, key)
Eq2 -> Just(v)

pub fip fun rbtree/is-red(^t : rbtree<k,v>) : bool
pub inline fip fun rbtree/is-red(^t : rbtree<k,v>) : bool
match t
Node(Red) -> True
_ -> False

// Helpers for other variants of the tree
pub fip fun dt/is-red( ^t : dtree<k,v> ) : bool
pub inline fip fun dt/is-red( ^t : dtree<k,v> ) : bool
match t
DNodeR(Red) -> True
DNodeL(Red) -> True
Expand All @@ -95,20 +95,20 @@ pub fip fun rebuild(z : zipper<k,v>, t : rbtree<k,v>): rbtree<k,v>


// append a `dtree<k,v>` to a context `acc`.
pub fip fun (++)( acc : ctx<rbtree<k,v>>, t : dtree<k,v> ) : ctx<rbtree<k,v>>
pub inline fip fun (++)( acc : ctx<rbtree<k,v>>, t : dtree<k,v> ) : ctx<rbtree<k,v>>
match t
DNodeR(c,l,x,v,_) -> acc ++ ctx Node(c,l,x,v,_)
DNodeL(c,_,x,v,r) -> acc ++ ctx Node(c,_,x,v,r)
NodeNone -> acc

// plug an `rbtree<k,v>`` into a `dtree<k,v>`
pub fip fun (++.)( d : dtree<k,v>, t : rbtree<k,v> ) : rbtree<k,v>
pub inline fip fun (++.)( d : dtree<k,v>, t : rbtree<k,v> ) : rbtree<k,v>
match d
DNodeR(c,l,x,v,_) -> Node(c,l,x,v,t)
DNodeL(c,_,x,v,r) -> Node(c,t,x,v,r)
NodeNone -> t

pub fip fun to-node( root : root<k,v> ) : rbtree<k,v>
pub inline fip fun to-node( root : root<k,v> ) : rbtree<k,v>
match root
Root(c,l,k,v,r) -> Node(c,l,k,v,r)

Expand Down
5 changes: 4 additions & 1 deletion tests/trees/rb-tree.kk
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,12 @@ pub fun test-main()
val v4 = t.fold(0, Done) fn(l, k, v){ if v then l.inc else l }
v4.show.println

pub fun example-bench()
pub fun example-benchtd()
benchmain(td/access)

pub fun example-benchbu()
benchmain(bu/access)

pub fun check-valid(t1: rbtree<int,v>) : exn ()
fun check(t: rbtree<int,v>, p: color, min: int, max: int) : exn int
match t
Expand Down

0 comments on commit 357b63a

Please sign in to comment.