-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvements in maps, sets, and parsing
- Loading branch information
1 parent
c6e3926
commit fe1fe57
Showing
12 changed files
with
152 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# MIT License, Copyright (c) 2024 Marvin Borner | ||
# Generic map implementation using AVL trees | ||
# the key-value pair is stored in the tree as a Church pair | ||
# some functions require a hash function! | ||
# TODO: what about hash collisions?? | ||
|
||
:import std/Tree/Balanced T | ||
:import std/Option O | ||
:import std/Number N | ||
:import std/Combinator . | ||
:import std/List . | ||
|
||
<?>‣ &[[[[[N.compare-case 4 3 2 ^1 ^0]]]]] ⧗ (Compare Number) | ||
|
||
# key to element (for searching) | ||
↑‣ [0 : i] ⧗ k → (Pair k v) | ||
|
||
# empty map | ||
empty T.empty ⧗ (Map k v) | ||
|
||
# returns true if a value is in a map | ||
has? [[<?>T.has? ↑(1 0)]] ⧗ (k → Number) → k → (Map k v) → Boolean | ||
|
||
# counts the key-value pairs in a map | ||
size T.size ⧗ (Map k v) → Number | ||
|
||
# returns the value of a key (or none) | ||
lookup (O.map &ki) ∘∘∘ [[<?>T.find ↑(1 0)]] ⧗ (k → Number) → k → (Map k v) → (Option v) | ||
|
||
# inserts (or replaces) a key with a value in a map | ||
insert [[[<?>T.insert ((2 1) : 0)]]] ⧗ (k → Number) → k → v → (Map k v) → (Map k v) | ||
|
||
:test (has? i (+2) (insert i (+2) "two" empty)) ([[1]]) | ||
:test (lookup i (+2) (insert i (+2) "two" empty)) (O.some "two") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters