1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays accessors sequences sequences.private
4 persistent.sequences assocs persistent.assocs kernel math
5 vectors parser prettyprint.custom ;
9 { length array-capacity read-only }
10 { vector vector read-only } ;
12 : <vlist> ( -- vlist ) 0 V{ } clone vlist boa ; inline
14 M: vlist length length>> ;
16 M: vlist nth-unsafe vector>> nth-unsafe ;
20 : >vlist< ( vlist -- len vec )
21 [ length>> ] [ vector>> ] bi ; inline
23 : unshare ( len vec -- len vec' )
24 clone [ set-length ] 2keep ; inline
30 2dup length = [ unshare ] unless
31 [ [ 1+ swap ] dip push ] keep vlist boa ;
33 ERROR: empty-vlist-error ;
37 [ [ length>> 1- ] [ vector>> ] bi vlist boa ] if-empty ;
40 [ length>> ] [ vector>> >vector ] bi vlist boa ;
43 over vlist? [ sequence= ] [ 2drop f ] if ;
45 : >vlist ( seq -- vlist )
46 [ length ] [ >vector ] bi vlist boa ; inline
49 drop dup vlist? [ >vlist ] unless ;
51 INSTANCE: vlist immutable-sequence
53 : VL{ \ } [ >vlist ] parse-literal ; parsing
55 M: vlist pprint-delims drop \ VL{ \ } ;
56 M: vlist >pprint-sequence ;
57 M: vlist pprint* pprint-object ;
59 TUPLE: valist { vlist vlist read-only } ;
61 : <valist> ( -- valist ) <vlist> valist boa ; inline
63 M: valist assoc-size vlist>> length 2/ ;
65 : valist-at ( key i array -- value ? )
68 [ 1+ ] dip nth-unsafe nip t
72 ] [ 3drop f f ] if ; inline recursive
75 vlist>> >vlist< [ 2 - ] [ underlying>> ] bi* valist-at ;
78 vlist>> ppush ppush valist boa ;
80 M: valist >alist vlist>> ;
82 : >valist ( assoc -- valist )
83 >alist concat >vlist valist boa ; inline
86 drop dup valist? [ >valist ] unless ;
88 INSTANCE: valist assoc
90 : VA{ \ } [ >valist ] parse-literal ; parsing
92 M: valist pprint-delims drop \ VA{ \ } ;
93 M: valist >pprint-sequence >alist ;
94 M: valist pprint* pprint-object ;