diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2023-03-23 22:26:15 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2023-03-23 22:26:15 +0000 |
| commit | ac2c8d2a8d71a76611de26bf545df37ceafc09f9 (patch) | |
| tree | c071d5b96ab6786694358681b956be7ee048aeb6 | |
| parent | 19a608784387815b0dac448244689155e0fbd6b1 (diff) | |
| download | perlweeklychallenge-club-ac2c8d2a8d71a76611de26bf545df37ceafc09f9.tar.gz perlweeklychallenge-club-ac2c8d2a8d71a76611de26bf545df37ceafc09f9.tar.bz2 perlweeklychallenge-club-ac2c8d2a8d71a76611de26bf545df37ceafc09f9.zip | |
incomplete
| -rw-r--r-- | challenge-209/paulo-custodio/forth/ch-2.fs | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/challenge-209/paulo-custodio/forth/ch-2.fs b/challenge-209/paulo-custodio/forth/ch-2.fs index 68740dc2e1..769c05f32d 100644 --- a/challenge-209/paulo-custodio/forth/ch-2.fs +++ b/challenge-209/paulo-custodio/forth/ch-2.fs @@ -30,3 +30,91 @@ \ Output: [ ["A", "a1@a.com", "a2@a.com"], \ ["A", "a3@a.com"], \ ["B", "b1@b.com", "b2@b.com"] ] + +\ string list: word with length, dwords with each string pointer and length +: str_list_new ( -- lst ) + CELL ALLOCATE THROW 0 OVER ! +; + +: str_list_delete ( lst -- ) + FREE THROW +; + +: str_list_size ( lst -- size ) + @ +; + +: str_list[] ( lst idx -- addr ) + SWAP CELL + SWAP CELLS 2* + +; + +: str_list_resize ( lst size -- lst ) + 2* CELLS CELL + RESIZE THROW +; + +: str_list_push { addr len lst -- lst } + lst lst str_list_size 1+ str_list_resize TO lst + addr len lst lst str_list_size str_list[] 2! + 1 lst +! + lst +; + +: str_list_type { lst -- } + lst str_list_size 0 ?DO + lst I str_list[] 2@ TYPE SPACE + LOOP +; + +: str_list_sort { lst -- } + lst str_list_size 1 > IF + lst str_list_size 1- 0 ?DO + lst str_list_size I 1+ ?DO + lst J str_list[] 2@ + lst I str_list[] 2@ + 2OVER 2OVER + COMPARE 0> IF + lst J str_list[] 2! + lst I str_list[] 2! + ELSE + 2DROP 2DROP + THEN + LOOP + LOOP + THEN +; + +: str_list_uniq { lst -- } + lst str_list_size 1 > IF + lst str_list_sort + 0 { i } + BEGIN i lst str_list_size 1- < WHILE + lst i str_list[] 2@ + lst i 1+ str_list[] 2@ + COMPARE 0= IF \ remove next entry + lst i 1+ str_list[] + lst i str_list[] + lst str_list_size i - 1- CELLS 2* + MOVE + -1 lst +! + ELSE + i 1+ TO i + THEN + REPEAT + THEN +; + + +str_list_new VALUE lst +lst 64 dump +next-arg lst str_list_push TO lst +next-arg lst str_list_push TO lst +next-arg lst str_list_push TO lst +next-arg lst str_list_push TO lst +next-arg lst str_list_push TO lst +lst 64 dump +lst str_list_type CR +lst str_list_uniq +lst str_list_type CR +lst 64 dump + +~~ bye |
