diff --git a/PLATFORM/C/LIB/buffer.lm b/PLATFORM/C/LIB/buffer.lm deleted file mode 100644 index c345ea4bf..000000000 --- a/PLATFORM/C/LIB/buffer.lm +++ /dev/null @@ -1,34 +0,0 @@ - - -type Buffer (Buffer( ptr:U8[] , allocated:U64 , capacity:U64 )); - -new-buffer := λ(: capacity U64). (: ( - (let ptr (malloc capacity)) - (Buffer( (as ptr U8[]) 0_u64 capacity )) -) Buffer); - -.calculate-extension-size := λ(: min-size U64). (: ( - (let try-size 1024_u64) - (while (<( try-size min-size )) ( - (set try-size (*( try-size 4_u64 ))) - )) - try-size -) U64); - -.extend := λ(: buff Buffer)(: sz U64). (: ( - (if (<( (+( (.allocated buff) sz )) (.capacity buff) )) ( - (set buff (Buffer( (.ptr buff) (+( (.allocated buff) sz )) (.capacity buff) ))) - ) ( - (let new-sz (.calculate-extension-size( (+( (.allocated buff) sz )) ))) - (let new-ptr (realloc( (.ptr buff) new-sz ))) - (set buff (Buffer( (as new-ptr U8[]) (+( (.allocated buff) sz )) new-sz ))) - )) - buff -) Buffer); - -.write := λ(: buff Buffer)(: data U8). (: ( - (let out-buff (.extend( buff 1_u64 ))) - (set[]( (as (.ptr out-buff) U8[]) (.allocated buff) data )) - out-buff -) Buffer); - diff --git a/PLATFORM/C/LIB/default.lsts b/PLATFORM/C/LIB/default.lsts index 8c432363c..d454cbdda 100644 --- a/PLATFORM/C/LIB/default.lsts +++ b/PLATFORM/C/LIB/default.lsts @@ -26,7 +26,6 @@ import PLATFORM/C/LIB/u64.lsts; import PLATFORM/C/LIB/i64.lsts; import PLATFORM/C/LIB/f64.lsts; import PLATFORM/C/LIB/usize.lsts; -import PLATFORM/C/LIB/buffer.lm; import PLATFORM/C/LIB/string.lm; import PLATFORM/C/LIB/string.lsts; import PLATFORM/C/LIB/smart-string.lm; diff --git a/PLATFORM/C/LIB/minimal.lsts b/PLATFORM/C/LIB/minimal.lsts index 3bbee732b..1be6ad252 100644 --- a/PLATFORM/C/LIB/minimal.lsts +++ b/PLATFORM/C/LIB/minimal.lsts @@ -20,7 +20,6 @@ import PLATFORM/C/LIB/u64.lsts; import PLATFORM/C/LIB/i32.lsts; import PLATFORM/C/LIB/i64.lsts; import PLATFORM/C/LIB/usize.lsts; -import PLATFORM/C/LIB/buffer.lm; import PLATFORM/C/LIB/string.lm; import PLATFORM/C/LIB/string.lsts; import PLATFORM/C/LIB/smart-string.lm; diff --git a/PLATFORM/C/LIB/string.lm b/PLATFORM/C/LIB/string.lm index bd9a28acf..6a3ec9372 100644 --- a/PLATFORM/C/LIB/string.lm +++ b/PLATFORM/C/LIB/string.lm @@ -9,34 +9,6 @@ tail-string := λ(: x String). (: ( (+( (as x U8[]) 1_u64 )) ) String); -clone-rope := λ(: s S). (: ( - (let buff (new-buffer 64_u64)) - (set buff (clone-rope-impl( buff s ))) - (set buff (.write( buff 0_u8 ))) - (as (.ptr buff) String) -) String); - -clone-rope-impl := λ(: buff Buffer)(: s S). (: ( - (match s ( - () - ( SNil () ) - ( (SCons( l r )) ( - (set buff (clone-rope-impl( buff l ))) - (set buff (clone-rope-impl( buff r ))) - )) - ( (SAtom a) ( - (let ci 0_u64) - (let c ([]( (as a U8[]) ci ))) - (while (!=( c 0_u8 )) ( - (set buff (.write( buff c ))) - (set ci (+( ci 1_u64 ))) - (set c ([]( (as a U8[]) ci ))) - )) - )) - )) - buff -) Buffer); - deep-hash := λ(: key String). (: ( (let i 0_u64) (let hash 0_u64) diff --git a/PLATFORM/C/LIB/string.lsts b/PLATFORM/C/LIB/string.lsts index 38684164e..5f94ad374 100644 --- a/PLATFORM/C/LIB/string.lsts +++ b/PLATFORM/C/LIB/string.lsts @@ -1,4 +1,34 @@ +# TODO: instead do U8 iterator for S + +# TODO: deprecate this in favor of Vector +let clone-rope(s: S): CString = ( + let out = mk-vector(type(U8), 64); + out = clone-rope-impl(s, out); + out.move-to(type(CString)) +); + +let clone-rope-impl(s: S, out: Vector): Vector = ( + match s { + SNil {} => (); + + SCons { l=left, r=right } => ( + out = clone-rope-impl(l, out); + out = clone-rope-impl(r, out); + ); + + SAtom { a=atom } => ( + while non-zero(a) { + out = out.push(head-string(a)); + a = tail-string(a); + }; + ); + + r => (); + }; + out +); + let cmp(l: CString, r: CString): Ord = ( let c = strcmp( (l as U8[]), (r as U8[]) ); if c < 0_i32 { LessThan } diff --git a/PLATFORM/C/LIB/u64.lm b/PLATFORM/C/LIB/u64.lm index 10c65ebb4..340c0b2ce 100644 --- a/PLATFORM/C/LIB/u64.lm +++ b/PLATFORM/C/LIB/u64.lm @@ -13,27 +13,6 @@ min := λ(: x U64)(: y U64). (: ( (if (<=( x y )) x y) ) U64); -to-string := λ(: i U64). (: ( - (let r SNil) - (let c 0_u8) - (while (not(==( i 0_u64 ))) ( - (let ci (as (+( (%( i 10_u64 )) 48_u64 )) U8)) - (set i (/( i 10_u64 ))) - (set r (SCons( - (close(SAtom(clone-rope ci))) - (close r) - ))) - )) - (if (non-zero r) () ( - (set r (SAtom '0_s)) - )) - (clone-rope r) -) String); - -to-smart-string := λ(: i U64). (: ( - (intern(to-string i)) -) SmartString); - deep-hash := λ(: x U64). (: (hash x) U64); to-u64 := λ(: s String). (: ( diff --git a/PLATFORM/C/LIB/u64.lsts b/PLATFORM/C/LIB/u64.lsts index 7d05e3b53..15c2fd82b 100644 --- a/PLATFORM/C/LIB/u64.lsts +++ b/PLATFORM/C/LIB/u64.lsts @@ -24,3 +24,20 @@ declare-unop( not, raw-type(U64), raw-type(U64), ( l"(!"; x; l")"; ) ); declare-unop( into-branch-conditional, raw-type(U64), raw-type(BranchConditional), x ); let cmp(l: U64, r: U64): Ord = if l==r then Equal else if l 0 { + let ci = ((i % 10) + 48) as U8; + i = i / 10; + out = out.push(ci); + }; + + out.reverse-self(); + out.move-to(type(CString)) +); + +let to-smart-string(i: U64): SmartString = ( + intern(to-string(i)) +); diff --git a/PLATFORM/C/LIB/vector.lsts b/PLATFORM/C/LIB/vector.lsts index 67ce7e3ac..494c5174c 100644 --- a/PLATFORM/C/LIB/vector.lsts +++ b/PLATFORM/C/LIB/vector.lsts @@ -33,9 +33,9 @@ let .reserve-additional(v: Vector, additional: U64): Vector = ( let .push(v: Vector, i: t): Vector = ( if v.length >= v.capacity { let new-cap = if v.capacity == 0 { - 4 + 4_u64 } else { # mul 1.5 - (v.capacity << 1) + v.capacity + (v.length >> 1_u64) + v.length }; v = v.realloc(new-cap); }; @@ -55,7 +55,7 @@ let .to-vector(l: List): Vector = ( # shrinks the vector if it has way too many elements. shouldn't be called manually let .shrink(v: Vector): Vector = ( let too-much = v.capacity - v.length; - let minimum = (v.length << 1) + v.length; # mul 1.5 + let minimum = (v.length >> 1) + v.length; # mul 1.5 if too-much > minimum { v = v.realloc(minimum); }; @@ -123,6 +123,17 @@ let $"set[]"( v: Vector, i: U64, val: t ): Nil = ( () ); +let .reverse-self(v: Vector): Nil = ( + let i = 0_u64; + while i < (v.length >> 1) { + let ri = v.length - i - 1; + let temp = v[i]; + v[i] = v[ri]; + v[ri] = temp; + i = i + 1; + }; +); + let .sort(v: Vector): Vector = ( let n = v.length; let i = 0_u64; @@ -166,6 +177,7 @@ type CString => FromVector; # MAKES INPUT VECTOR INVALID let .move-to(self: Vector, res: Type): CString = ( self = self.push(0_u8); + self = self.shrink(); (self.data as U8[]) as CString ); diff --git a/tests/lib/vector-push-pop.lsts b/tests/lib/vector-push-pop.lsts new file mode 100644 index 000000000..92981c62a --- /dev/null +++ b/tests/lib/vector-push-pop.lsts @@ -0,0 +1,38 @@ + +import LIB/minimal.lsts; + +let v = mk-vector(type(String), 4); + +let ai = 0_u64; +while ai < 128 { + v = v.push(to-smart-string(ai)); + ai = ai + 1; +}; + +assert(v.length == 128); +let last-cap = v.capacity; + +ai = 0_u64; +while ai < v.length { + let x = v[ai]; + assert(x == to-smart-string(ai)); + ai = ai + 1; +}; + +ai = 0_u64; +while ai < 100 { + v = v.pop().first; + ai = ai + 1; +}; + +# should have shrunk from the pop() s +assert(v.capacity < last-cap); +last-cap = v.capacity; + +ai = 0_u64; +while ai < v.length { + let x = v[ai]; + assert(x == to-smart-string(ai)); + ai = ai + 1; +}; + diff --git a/tests/lib/vector-push-pop.lsts.out b/tests/lib/vector-push-pop.lsts.out new file mode 100644 index 000000000..e69de29bb