1
0
mirror of https://github.com/JustinSDK/dotSCAD.git synced 2025-01-17 06:08:31 +01:00
This commit is contained in:
Justin Lin 2021-03-09 17:11:02 +08:00
parent 7b8f4386e4
commit a2c62a7759
17 changed files with 264 additions and 8 deletions

View File

@ -171,14 +171,14 @@ These examples incubate dotSCAD and dotSCAD refactors these examples. See [examp
- [util/set/hashset_len](https://openhome.cc/eGossip/OpenSCAD/lib3x-hashset_len.html)
- [util/set/hashset_elems](https://openhome.cc/eGossip/OpenSCAD/lib3x-hashset_elems.html)
- map
- `util/map/hashmap`
- `util/map/hashmap_put`
- `util/map/hashmap_get`
- `util/map/hashmap_del`
- `util/map/hashmap_len`
- `util/map/hashmap_keys`
- `util/map/hashmap_values`
- `util/map/hashmap_entries`
- [util/map/hashmap](https://openhome.cc/eGossip/OpenSCAD/lib3x-hashmap.html)
- [util/map/hashmap_put](https://openhome.cc/eGossip/OpenSCAD/lib3x-hashmap_put.html)
- [util/map/hashmap_get](https://openhome.cc/eGossip/OpenSCAD/lib3x-hashmap_get.html)
- [util/map/hashmap_del](https://openhome.cc/eGossip/OpenSCAD/lib3x-hashmap_del.html)
- [util/map/hashmap_len](https://openhome.cc/eGossip/OpenSCAD/lib3x-hashmap_len.html)
- [util/map/hashmap_keys](https://openhome.cc/eGossip/OpenSCAD/lib3x-hashmap_keys.html)
- [util/map/hashmap_values](https://openhome.cc/eGossip/OpenSCAD/lib3x-hashmap_values.html)
- [util/map/hashmap_entries](https://openhome.cc/eGossip/OpenSCAD/lib3x-hashmap_entries.html)
### Matrix
- [matrix/m_determinant](https://openhome.cc/eGossip/OpenSCAD/lib3x-m_determinant.html)

44
docs/lib3x-hashmap.md Normal file
View File

@ -0,0 +1,44 @@
# hashmap
This function maps keys to values. You can use the following to process the returned map.
- [util/map/hashmap_put](https://openhome.cc/eGossip/OpenSCAD/lib3x-hashmap_put.html) : Puts an element to the map. It returns a new map.
- [util/map/hashmap_get](https://openhome.cc/eGossip/OpenSCAD/lib3x-hashmap_get.html) : Returns the value to which the specified key is mapped, or `undef` if the map contains no mapping for the key.
- [util/map/hashmap_del](https://openhome.cc/eGossip/OpenSCAD/lib3x-hashmap_del.html) : Deletes the mapping for the specified key from a map if present.
- [util/map/hashmap_len](https://openhome.cc/eGossip/OpenSCAD/lib3x-hashmap_len.html) : Returns the length of the map.
- [util/map/hashmap_keys](https://openhome.cc/eGossip/OpenSCAD/lib3x-hashmap_keys.html) : Returns a list containing all keys in the map.
- [util/map/hashmap_values](https://openhome.cc/eGossip/OpenSCAD/lib3x-hashmap_values.html) : Returns a list containing all values in the map.
- [util/map/hashmap_entries](https://openhome.cc/eGossip/OpenSCAD/lib3x-hashmap_entries.html) : Returns a list containing all `[key, value]`s in the map.
**Since:** 3.0
## Parameters
- `kv_lt` : Constructs a new map containing the `[key, value]`s in the specified list. It can be ignored if you want an empty map.
- `eq` : A equality function. If it's ignored, use `==` to compare elements.
- `hash` : A hash function. If it's ignored, convert each element to a string and hash it.
- `number_of_buckets` : The function uses a hash table internally. Change the number of buckets if you're trying to do optimization.
## Examples
use <util/map/hashmap.scad>;
use <util/map/hashmap_len.scad>;
use <util/map/hashmap_put.scad>;
use <util/map/hashmap_get.scad>;
use <util/map/hashmap_del.scad>;
use <util/map/hashmap_keys.scad>;
use <util/map/hashmap_values.scad>;
use <util/map/hashmap_entries.scad>;
m1 = hashmap([["k1", 10], ["k2", 20], ["k3", 30]]);
assert(hashmap_len(m1) == 3);
m2 = hashmap_put(m1, "k4", 40);
assert(hashmap_get(m2, "k4") == 40);
m3 = hashmap_del(m2, "k1");
assert(hashmap_get(m3, "k1") == undef);
echo(hashmap_keys(m3)); // a list contains "k2", "k2", "k3"
echo(hashmap_values(m3)); // a list contains 20, 30, 40
echo(hashmap_entries(m3)); // a list contains ["k2", 20], ["k3", 30], ["k4", 40]

22
docs/lib3x-hashmap_del.md Normal file
View File

@ -0,0 +1,22 @@
# hashmap_put
This function deletes the mapping for the specified key from a [util/map/hashmap](https://openhome.cc/eGossip/OpenSCAD/lib3x-hashmap.html) if present. It returns a new map without the key/value.
**Since:** 3.0
## Parameters
- `map` : The original map.
- `key` : Adds the specified element to the specified set
- `eq` : A equality function. If it's ignored, use `==` to compare elements.
- `hash` : A hash function. If it's ignored, convert each element to a string and hash it.
## Examples
use <util/map/hashmap.scad>;
use <util/map/hashmap_get.scad>;
use <util/map/hashmap_del.scad>;
m1 = hashmap([["k1", 10], ["k2", 20], ["k3", 30]]);
m2 = hashmap_del(m1, "k1");
assert(hashmap_get(m2, "k1") == undef);

View File

@ -0,0 +1,17 @@
# hashmap_entries
Returns a list containing all `[key, value]`s in a [util/map/hashmap](https://openhome.cc/eGossip/OpenSCAD/lib3x-hashmap.html).
**Since:** 3.0
## Parameters
- `map` : The map.
## Examples
use <util/map/hashmap.scad>;
use <util/map/hashmap_entries.scad>;
m = hashmap([["k1", 10], ["k2", 20], ["k3", 30]]);
echo(hashmap_entries(m)); // a list contains ["k1", 10], ["k2", 20], ["k3", 30]

20
docs/lib3x-hashmap_get.md Normal file
View File

@ -0,0 +1,20 @@
# hashmap_put
This function gets the value of the specified key from a [util/map/hashmap](https://openhome.cc/eGossip/OpenSCAD/lib3x-hashmap.html).
**Since:** 3.0
## Parameters
- `map` : The original map.
- `key` : Adds the specified element to the specified set
- `eq` : A equality function. If it's ignored, use `==` to compare elements.
- `hash` : A hash function. If it's ignored, convert each element to a string and hash it.
## Examples
use <util/map/hashmap.scad>;
use <util/map/hashmap_get.scad>;
m = hashmap([["k1", 10], ["k2", 20], ["k3", 30]]);
assert(hashmap_get(m, "k2") == 20);

View File

@ -0,0 +1,18 @@
# hashmap_keys
Returns a list containing all keys in a [util/map/hashmap](https://openhome.cc/eGossip/OpenSCAD/lib3x-hashmap.html).
**Since:** 3.0
## Parameters
- `map` : The map.
## Examples
use <util/map/hashmap.scad>;
use <util/map/hashmap_keys.scad>;
m = hashmap([["k1", 10], ["k2", 20], ["k3", 30]]);
echo(hashmap_keys(m)); // a list contains "k1", "k2", "k3"

17
docs/lib3x-hashmap_len.md Normal file
View File

@ -0,0 +1,17 @@
# hashmap_put
This function puts a key/value to a [util/map/hashmap](https://openhome.cc/eGossip/OpenSCAD/lib3x-hashmap.html). It returns a new map containing the key/value.
**Since:** 3.0
## Parameters
- `map` : The map.
## Examples
use <util/map/hashmap.scad>;
use <util/map/hashmap_len.scad>;
m = hashmap([["k1", 10], ["k2", 20], ["k3", 30]]);
assert(hashmap_len(m) == 3);

20
docs/lib3x-hashmap_put.md Normal file
View File

@ -0,0 +1,20 @@
# hashmap_put
This function returns the length of a [util/map/hashmap](https://openhome.cc/eGossip/OpenSCAD/lib3x-hashmap.html).
**Since:** 3.0
## Parameters
- `map` : The original map.
## Examples
use <util/map/hashmap.scad>;
use <util/map/hashmap_put.scad>;
use <util/map/hashmap_get.scad>;
m1 = hashmap([["k1", 10], ["k2", 20], ["k3", 30]]);
m2 = hashmap_put(m1, "k4", 40);
assert(hashmap_get(m2, "k4") == 40);

View File

@ -0,0 +1,18 @@
# hashmap_values
Returns a list containing all values in a [util/map/hashmap](https://openhome.cc/eGossip/OpenSCAD/lib3x-hashmap.html).
**Since:** 3.0
## Parameters
- `map` : The map.
## Examples
use <util/map/hashmap.scad>;
use <util/map/hashmap_values.scad>;
m = hashmap([["k1", 10], ["k2", 20], ["k3", 30]]);
echo(hashmap_values(m)); // a list contains 10, 20, 30

View File

@ -1,3 +1,13 @@
/**
* hashmap.scad
*
* @copyright Justin Lin, 2021
* @license https://opensource.org/licenses/lgpl-3.0.html
*
* @see https://openhome.cc/eGossip/OpenSCAD/lib3x-hashmap.html
*
**/
use <../../__comm__/_str_hash.scad>;
use <_impl/_hashmap_impl.scad>;
use <_impl/_hashmap_put_impl.scad>;

View File

@ -1,3 +1,13 @@
/**
* hashmap_del.scad
*
* @copyright Justin Lin, 2021
* @license https://opensource.org/licenses/lgpl-3.0.html
*
* @see https://openhome.cc/eGossip/OpenSCAD/lib3x-hashmap_del.html
*
**/
use <../../__comm__/_str_hash.scad>;
use <../slice.scad>;
use <../find_index.scad>;

View File

@ -1,3 +1,13 @@
/**
* hashmap_entries.scad
*
* @copyright Justin Lin, 2021
* @license https://opensource.org/licenses/lgpl-3.0.html
*
* @see https://openhome.cc/eGossip/OpenSCAD/lib3x-hashmap_entries.html
*
**/
function hashmap_entries(map) = [
for(bucket = map)
for(kv = bucket)

View File

@ -1,3 +1,13 @@
/**
* hashmap_get.scad
*
* @copyright Justin Lin, 2021
* @license https://opensource.org/licenses/lgpl-3.0.html
*
* @see https://openhome.cc/eGossip/OpenSCAD/lib3x-hashmap_get.html
*
**/
use <../../__comm__/_str_hash.scad>;
use <../slice.scad>;
use <../find_index.scad>;

View File

@ -1,3 +1,13 @@
/**
* hashmap_keys.scad
*
* @copyright Justin Lin, 2021
* @license https://opensource.org/licenses/lgpl-3.0.html
*
* @see https://openhome.cc/eGossip/OpenSCAD/lib3x-hashmap_keys.html
*
**/
function hashmap_keys(map) = [
for(bucket = map)
for(kv = bucket)

View File

@ -1,3 +1,13 @@
/**
* hashmap_len.scad
*
* @copyright Justin Lin, 2021
* @license https://opensource.org/licenses/lgpl-3.0.html
*
* @see https://openhome.cc/eGossip/OpenSCAD/lib3x-hashmap_len.html
*
**/
use <../sum.scad>;
function hashmap_len(map) = sum([

View File

@ -1,3 +1,13 @@
/**
* hashmap_put.scad
*
* @copyright Justin Lin, 2021
* @license https://opensource.org/licenses/lgpl-3.0.html
*
* @see https://openhome.cc/eGossip/OpenSCAD/lib3x-hashmap_put.html
*
**/
use <../../__comm__/_str_hash.scad>;
use <_impl/_hashmap_put_impl.scad>;

View File

@ -1,3 +1,13 @@
/**
* hashmap_values.scad
*
* @copyright Justin Lin, 2021
* @license https://opensource.org/licenses/lgpl-3.0.html
*
* @see https://openhome.cc/eGossip/OpenSCAD/lib3x-hashmap_values.html
*
**/
function hashmap_values(map) = [
for(bucket = map)
for(kv = bucket)