mirror of
https://github.com/urbit/shrub.git
synced 2024-11-28 22:33:06 +03:00
graph-store: add-nodes compiles
This commit is contained in:
parent
e52779a356
commit
8c2d9127f3
@ -1,10 +1,12 @@
|
||||
/+ store=graph-store, default-agent, dbug
|
||||
/+ store=graph-store, *or-map, default-agent, dbug
|
||||
|%
|
||||
+$ card card:agent:gall
|
||||
+$ versioned-state
|
||||
$% state-0
|
||||
==
|
||||
+$ state-0 [%0 network:store]
|
||||
::
|
||||
++ orm ((or-map atom:store node:store) lth)
|
||||
--
|
||||
::
|
||||
=| state-0
|
||||
@ -73,10 +75,10 @@
|
||||
:_ state
|
||||
(give [/all]~ [%add-nodes nodes])
|
||||
=* resource -.i.resource-list
|
||||
=* indexed-nodes +.i.resource-list
|
||||
=/ graph=(unit graph) (~(get by graphs) resource)
|
||||
=/ indexed-nodes=(map index:store node:store) +.i.resource-list
|
||||
=/ graph=(unit graph:store) (~(get by graphs) resource)
|
||||
?~ graph
|
||||
~| "graph {<resource>} does not exist to add a node to!"
|
||||
~& "graph {<resource>} does not exist to add a node to!"
|
||||
$(resource-list t.resource-list)
|
||||
%_ $
|
||||
resource-list t.resource-list
|
||||
@ -107,36 +109,38 @@
|
||||
?~ index graph
|
||||
=* atom i.index
|
||||
:: last index in list
|
||||
::
|
||||
?~ t.index (put:orm graph atom node)
|
||||
:: multiple indices left in list
|
||||
:: TODO: replace normal map function with ordered-map version
|
||||
:: of get. look at find-ducts in behn
|
||||
=/ parent=(unit node) (~(get by graph) atom)
|
||||
::
|
||||
=/ parent=(unit node:store) (get:orm graph atom)
|
||||
?~ parent
|
||||
~| "{<atom>} does not exist to add a node to!"
|
||||
~& "index does not exist to add a node to!"
|
||||
graph
|
||||
?+ -.children.u.parent
|
||||
=/ par=node:store (need parent)
|
||||
?+ -.children.par
|
||||
:: replace empty graph with graph containing one child
|
||||
::
|
||||
%^ put:orm
|
||||
graph
|
||||
atom
|
||||
%_ u.parent
|
||||
%= par
|
||||
children
|
||||
^- internal-graph:store
|
||||
[%graph $(graph (gas:orm ~ ~), index t.index) now.bowl]
|
||||
==
|
||||
::
|
||||
%graph
|
||||
:: recurse into children
|
||||
::
|
||||
%^ put:orm
|
||||
graph
|
||||
atom
|
||||
%_ u.parent
|
||||
p.children $(graph p.children.u.parent, index t.index)
|
||||
%_ par
|
||||
p.children $(graph p.children.par, index t.index)
|
||||
q.children now.bowl
|
||||
==
|
||||
==
|
||||
::
|
||||
++ orm ((ordered-map atom:store node:store) lth)
|
||||
--
|
||||
::
|
||||
++ remove-nodes
|
||||
|
313
pkg/arvo/lib/or-map.hoon
Normal file
313
pkg/arvo/lib/or-map.hoon
Normal file
@ -0,0 +1,313 @@
|
||||
|%
|
||||
::
|
||||
:: $mk-item: constructor for +ordered-map item type
|
||||
::
|
||||
++ mk-item |$ [key val] [key=key val=val]
|
||||
:: +ordered-map: treap with user-specified horizontal order
|
||||
::
|
||||
:: Conceptually smaller items go on the left, so the item with the
|
||||
:: smallest key can be popped off the head. If $key is `@` and
|
||||
:: .compare is +lte, then the numerically smallest item is the head.
|
||||
::
|
||||
++ or-map
|
||||
|* [key=mold val=mold]
|
||||
=> |%
|
||||
+$ item (mk-item key val)
|
||||
--
|
||||
:: +compare: item comparator for horizontal order
|
||||
::
|
||||
|= compare=$-([key key] ?)
|
||||
|%
|
||||
:: +check-balance: verify horizontal and vertical orderings
|
||||
::
|
||||
++ check-balance
|
||||
=| [l=(unit key) r=(unit key)]
|
||||
|= a=(tree item)
|
||||
^- ?
|
||||
:: empty tree is valid
|
||||
::
|
||||
?~ a %.y
|
||||
:: nonempty trees must maintain several criteria
|
||||
::
|
||||
?& :: if .n.a is left of .u.l, assert horizontal comparator
|
||||
::
|
||||
?~(l %.y (compare key.n.a u.l))
|
||||
:: if .n.a is right of .u.r, assert horizontal comparator
|
||||
::
|
||||
?~(r %.y (compare u.r key.n.a))
|
||||
:: if .a is not leftmost element, assert vertical order between
|
||||
:: .l.a and .n.a and recurse to the left with .n.a as right
|
||||
:: neighbor
|
||||
::
|
||||
?~(l.a %.y &((mor key.n.a key.n.l.a) $(a l.a, l `key.n.a)))
|
||||
:: if .a is not rightmost element, assert vertical order
|
||||
:: between .r.a and .n.a and recurse to the right with .n.a as
|
||||
:: left neighbor
|
||||
::
|
||||
?~(r.a %.y &((mor key.n.a key.n.r.a) $(a r.a, r `key.n.a)))
|
||||
==
|
||||
:: +put: ordered item insert
|
||||
::
|
||||
++ put
|
||||
|= [a=(tree item) =key =val]
|
||||
^- (tree item)
|
||||
:: base case: replace null with single-item tree
|
||||
::
|
||||
?~ a [n=[key val] l=~ r=~]
|
||||
:: base case: overwrite existing .key with new .val
|
||||
::
|
||||
?: =(key.n.a key) a(val.n val)
|
||||
:: if item goes on left, recurse left then rebalance vertical order
|
||||
::
|
||||
?: (compare key key.n.a)
|
||||
=/ l $(a l.a)
|
||||
?> ?=(^ l)
|
||||
?: (mor key.n.a key.n.l)
|
||||
a(l l)
|
||||
l(r a(l r.l))
|
||||
:: item goes on right; recurse right then rebalance vertical order
|
||||
::
|
||||
=/ r $(a r.a)
|
||||
?> ?=(^ r)
|
||||
?: (mor key.n.a key.n.r)
|
||||
a(r r)
|
||||
r(l a(r l.r))
|
||||
:: +peek: produce head (smallest item) or null
|
||||
::
|
||||
++ peek
|
||||
|= a=(tree item)
|
||||
^- (unit item)
|
||||
::
|
||||
?~ a ~
|
||||
?~ l.a `n.a
|
||||
$(a l.a)
|
||||
:: +pop: produce .head (smallest item) and .rest or crash if empty
|
||||
::
|
||||
++ pop
|
||||
|= a=(tree item)
|
||||
^- [head=item rest=(tree item)]
|
||||
::
|
||||
?~ a !!
|
||||
?~ l.a [n.a r.a]
|
||||
::
|
||||
=/ l $(a l.a)
|
||||
:- head.l
|
||||
:: load .rest.l back into .a and rebalance
|
||||
::
|
||||
?: |(?=(~ rest.l) (mor key.n.a key.n.rest.l))
|
||||
a(l rest.l)
|
||||
rest.l(r a(r r.rest.l))
|
||||
:: +del: delete .key from .a if it exists, producing value iff deleted
|
||||
::
|
||||
++ del
|
||||
|= [a=(tree item) =key]
|
||||
^- [(unit val) (tree item)]
|
||||
::
|
||||
?~ a [~ ~]
|
||||
:: we found .key at the root; delete and rebalance
|
||||
::
|
||||
?: =(key key.n.a)
|
||||
[`val.n.a (nip a)]
|
||||
:: recurse left or right to find .key
|
||||
::
|
||||
?: (compare key key.n.a)
|
||||
=+ [found lef]=$(a l.a)
|
||||
[found a(l lef)]
|
||||
=+ [found rig]=$(a r.a)
|
||||
[found a(r rig)]
|
||||
:: +nip: remove root; for internal use
|
||||
::
|
||||
++ nip
|
||||
|= a=(tree item)
|
||||
^- (tree item)
|
||||
::
|
||||
?> ?=(^ a)
|
||||
:: delete .n.a; merge and balance .l.a and .r.a
|
||||
::
|
||||
|- ^- (tree item)
|
||||
?~ l.a r.a
|
||||
?~ r.a l.a
|
||||
?: (mor key.n.l.a key.n.r.a)
|
||||
l.a(r $(l.a r.l.a))
|
||||
r.a(l $(r.a l.r.a))
|
||||
:: +traverse: stateful partial inorder traversal
|
||||
::
|
||||
:: Mutates .state on each run of .f. Starts at .start key, or if
|
||||
:: .start is ~, starts at the head (item with smallest key). Stops
|
||||
:: when .f produces .stop=%.y. Traverses from smaller to larger
|
||||
:: keys. Each run of .f can replace an item's value or delete the
|
||||
:: item.
|
||||
::
|
||||
++ traverse
|
||||
|* state=mold
|
||||
|= $: a=(tree item)
|
||||
=state
|
||||
f=$-([state item] [(unit val) ? state])
|
||||
==
|
||||
^+ [state a]
|
||||
:: acc: accumulator
|
||||
::
|
||||
:: .stop: set to %.y by .f when done traversing
|
||||
:: .state: threaded through each run of .f and produced by +abet
|
||||
::
|
||||
=/ acc [stop=`?`%.n state=state]
|
||||
=< abet =< main
|
||||
|%
|
||||
++ abet [state.acc a]
|
||||
:: +main: main recursive loop; performs a partial inorder traversal
|
||||
::
|
||||
++ main
|
||||
^+ .
|
||||
:: stop if empty or we've been told to stop
|
||||
::
|
||||
?~ a .
|
||||
?: stop.acc .
|
||||
:: inorder traversal: left -> node -> right, until .f sets .stop
|
||||
::
|
||||
=> left
|
||||
?: stop.acc .
|
||||
=> node
|
||||
?: stop.acc .
|
||||
right
|
||||
:: +node: run .f on .n.a, updating .a, .state, and .stop
|
||||
::
|
||||
++ node
|
||||
^+ .
|
||||
:: run .f on node, updating .stop.acc and .state.acc
|
||||
::
|
||||
=^ res acc
|
||||
?> ?=(^ a)
|
||||
(f state.acc n.a)
|
||||
:: apply update to .a from .f's product
|
||||
::
|
||||
=. a
|
||||
:: if .f requested node deletion, merge and balance .l.a and .r.a
|
||||
::
|
||||
?~ res (nip a)
|
||||
:: we kept the node; replace its .val; order is unchanged
|
||||
::
|
||||
?> ?=(^ a)
|
||||
a(val.n u.res)
|
||||
::
|
||||
..node
|
||||
:: +left: recurse on left subtree, copying mutant back into .l.a
|
||||
::
|
||||
++ left
|
||||
^+ .
|
||||
?~ a .
|
||||
=/ lef main(a l.a)
|
||||
lef(a a(l a.lef))
|
||||
:: +right: recurse on right subtree, copying mutant back into .r.a
|
||||
::
|
||||
++ right
|
||||
^+ .
|
||||
?~ a .
|
||||
=/ rig main(a r.a)
|
||||
rig(a a(r a.rig))
|
||||
--
|
||||
:: +tap: convert to list, smallest to largest
|
||||
::
|
||||
++ tap
|
||||
|= a=(tree item)
|
||||
^- (list item)
|
||||
::
|
||||
=| b=(list item)
|
||||
|- ^+ b
|
||||
?~ a b
|
||||
::
|
||||
$(a l.a, b [n.a $(a r.a)])
|
||||
:: +gas: put a list of items
|
||||
::
|
||||
++ gas
|
||||
|= [a=(tree item) b=(list item)]
|
||||
^- (tree item)
|
||||
::
|
||||
?~ b a
|
||||
$(b t.b, a (put a i.b))
|
||||
:: +uni: unify two ordered maps
|
||||
::
|
||||
:: .b takes precedence over .a if keys overlap.
|
||||
::
|
||||
++ uni
|
||||
|= [a=(tree item) b=(tree item)]
|
||||
^- (tree item)
|
||||
::
|
||||
?~ b a
|
||||
?~ a b
|
||||
?: =(key.n.a key.n.b)
|
||||
::
|
||||
[n=n.b l=$(a l.a, b l.b) r=$(a r.a, b r.b)]
|
||||
::
|
||||
?: (mor key.n.a key.n.b)
|
||||
::
|
||||
?: (compare key.n.b key.n.a)
|
||||
$(l.a $(a l.a, r.b ~), b r.b)
|
||||
$(r.a $(a r.a, l.b ~), b l.b)
|
||||
::
|
||||
?: (compare key.n.a key.n.b)
|
||||
$(l.b $(b l.b, r.a ~), a r.a)
|
||||
$(r.b $(b r.b, l.a ~), a l.a)
|
||||
::
|
||||
:: +get: get val at key or return ~
|
||||
::
|
||||
++ get
|
||||
|= [a=(tree item) b=key]
|
||||
^- (unit val)
|
||||
?~ a ~
|
||||
?: =(b key.n.a)
|
||||
`val.n.a
|
||||
?: (compare b key.n.a)
|
||||
$(a l.a)
|
||||
$(a r.a)
|
||||
::
|
||||
:: +subset: take a range excluding start and/or end and all elements
|
||||
:: outside the range
|
||||
::
|
||||
++ subset
|
||||
|= $: tre=(tree item)
|
||||
start=(unit key)
|
||||
end=(unit key)
|
||||
==
|
||||
^- (tree item)
|
||||
|^
|
||||
?: ?&(?=(~ start) ?=(~ end))
|
||||
tre
|
||||
?~ start
|
||||
(del-span tre %end end)
|
||||
?~ end
|
||||
(del-span tre %start start)
|
||||
?> (lth u.start u.end)
|
||||
=. tre (del-span tre %start start)
|
||||
(del-span tre %end end)
|
||||
::
|
||||
++ del-span
|
||||
|= [a=(tree item) b=?(%start %end) c=(unit key)]
|
||||
^- (tree item)
|
||||
?~ a a
|
||||
?~ c a
|
||||
?- b
|
||||
%start
|
||||
:: found key
|
||||
?: =(key.n.a u.c)
|
||||
(nip a(l ~))
|
||||
:: traverse to find key
|
||||
?: (compare key.n.a u.c)
|
||||
:: found key to the left of start
|
||||
$(a (nip a(l ~)))
|
||||
:: found key to the right of start
|
||||
a(l $(a l.a))
|
||||
::
|
||||
%end
|
||||
:: found key
|
||||
?: =(u.c key.n.a)
|
||||
(nip a(r ~))
|
||||
:: traverse to find key
|
||||
?: (compare key.n.a u.c)
|
||||
:: found key to the left of end
|
||||
a(r $(a r.a))
|
||||
:: found key to the right of end
|
||||
$(a (nip a(r ~)))
|
||||
==
|
||||
--
|
||||
--
|
||||
--
|
@ -16,12 +16,12 @@
|
||||
::
|
||||
+$ graph ((mop atom node) lth)
|
||||
+$ internal-graph
|
||||
$~ [%not-loaded ~]
|
||||
$~ [%empty ~]
|
||||
$% ::
|
||||
:: a graph and timestamp of when it was last modified
|
||||
[%graph p=graph q=time]
|
||||
[%empty-when-fetched p=time]
|
||||
[%not-loaded ~]
|
||||
[%empty ~]
|
||||
[%empty-at-time p=time]
|
||||
==
|
||||
::
|
||||
+$ node [=post children=internal-graph]
|
||||
|
Loading…
Reference in New Issue
Block a user