Skip to content

Commit

Permalink
Merge pull request #1306 from alex-s168/vector-rework
Browse files Browse the repository at this point in the history
vector related fixes
  • Loading branch information
andrew-johnson-4 authored Mar 5, 2025
2 parents 18cc1f4 + 0cdad06 commit 754d25e
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 88 deletions.
34 changes: 0 additions & 34 deletions PLATFORM/C/LIB/buffer.lm

This file was deleted.

1 change: 0 additions & 1 deletion PLATFORM/C/LIB/default.lsts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion PLATFORM/C/LIB/minimal.lsts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 0 additions & 28 deletions PLATFORM/C/LIB/string.lm
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions PLATFORM/C/LIB/string.lsts
Original file line number Diff line number Diff line change
@@ -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<U8>): Vector<U8> = (
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 }
Expand Down
21 changes: 0 additions & 21 deletions PLATFORM/C/LIB/u64.lm
Original file line number Diff line number Diff line change
Expand Up @@ -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). (: (
Expand Down
17 changes: 17 additions & 0 deletions PLATFORM/C/LIB/u64.lsts
Original file line number Diff line number Diff line change
Expand Up @@ -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<r then LessThan else GreaterThan;

let to-string(i: U64): CString = (
let out = mk-vector(type(U8), 8);

while i > 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))
);
18 changes: 15 additions & 3 deletions PLATFORM/C/LIB/vector.lsts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ let .reserve-additional(v: Vector<t>, additional: U64): Vector<t> = (
let .push(v: Vector<t>, i: t): Vector<t> = (
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);
};
Expand All @@ -55,7 +55,7 @@ let .to-vector(l: List<t>): Vector<t> = (
# shrinks the vector if it has way too many elements. shouldn't be called manually
let .shrink(v: Vector<t>): Vector<t> = (
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);
};
Expand Down Expand Up @@ -123,6 +123,17 @@ let $"set[]"( v: Vector<t>, i: U64, val: t ): Nil = (
()
);

let .reverse-self(v: Vector<t>): 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<t>): Vector<t> = (
let n = v.length;
let i = 0_u64;
Expand Down Expand Up @@ -166,6 +177,7 @@ type CString => FromVector<U8>;
# MAKES INPUT VECTOR INVALID
let .move-to(self: Vector<U8>, res: Type<CString>): CString = (
self = self.push(0_u8);
self = self.shrink();
(self.data as U8[]) as CString
);

Expand Down
38 changes: 38 additions & 0 deletions tests/lib/vector-push-pop.lsts
Original file line number Diff line number Diff line change
@@ -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;
};

Empty file.

0 comments on commit 754d25e

Please sign in to comment.