Permalink
Browse files

Add example of an Array class using graph as storage

  • Loading branch information...
birchb1024 committed Oct 31, 2018
1 parent f77a754 commit 4585298fee274ee5b59b6a72e1b383b675464eb3
Showing with 78 additions and 0 deletions.
  1. +78 −0 examples/array.g
@@ -0,0 +1,78 @@
#
# Use Graph as a sparse array
#
class Array(Graph)
def .new (&rest args)
var array (graph)
var index 0
for item in args
array
.add
triple item ^_ index
setq index (+ index 1)
tag Array array
def .index (i)
var result nil
def check-triple (s p o)
setq result s
var query (.select nil nil i check-triple)
cond
(not (equal? 1 (query(.length))))
raise ('index not found %s'(.format i))
result
def .setq (i v)
.select nil nil i
function (s p o)
.remove (triple s p o)
.put v ^_ i
# TODO
# def .next ()
# # iterator for use in
# # for v in (.next)
# # print v
#
def .assoc ()
var result ()
def mk-assoc (s p o)
setq result (cons (cons o s) result)
var query (.select nil nil nil mk-assoc)
result
defmethod .list ()
var result ()
def mk-list (s p o)
setq result (cons s result)
for i in (range 0 (this!length))
.select nil nil i mk-list
reverse result # TODO rewrite using a .next iterator
define squareBracket Array!new
# Tests
var an-array [1 2 3 4]
assertEqual 1
an-array(.index 0)
an-array(.setq 0 99)
assertEqual 99
an-array(.index 0)
assertEqual (an-array(.list)) ^(99 2 3 4)
var a [^(a=3) 33 'eee']
assertEqual ^(a=3)
a(.index 0)
assertEqual 33
a(.index 1)
assertEqual (a(.list)) ^((a=3) 33 'eee')
[1 2 nil 3](.assoc)

0 comments on commit 4585298

Please sign in to comment.