Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Hash module and Text.hash() #133

Merged
merged 15 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/Array.mo
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
/// import Array "mo:base/Array";
/// ```

import Iter "type/Iter";
import Order "Order";
import Result "Result";
import VarArray "VarArray";
import Option "Option";
import Types "Types";
import Prim "mo:⛔";
import { todo } "Debug";

Expand Down Expand Up @@ -440,7 +440,7 @@ module {
///
/// Space: O(size)
/// *Runtime and space assumes that `k` runs in O(1) time and space.
public func flatMap<T, R>(array : [T], k : T -> Iter.Iter<R>) : [R] {
public func flatMap<T, R>(array : [T], k : T -> Types.Iter<R>) : [R] {
var flatSize = 0;
let arrays = Prim.Array_tabulate<[R]>(
array.size(),
Expand Down Expand Up @@ -539,7 +539,7 @@ module {
/// Runtime: O(number of elements in array)
///
/// Space: O(number of elements in array)
public func join<T>(arrays : Iter.Iter<[T]>) : [T] {
public func join<T>(arrays : Types.Iter<[T]>) : [T] {
flatten(fromIter(arrays))
};

Expand Down Expand Up @@ -598,7 +598,7 @@ module {
public func isEmpty<T>(array : [T]) : Bool = array.size() == 0;

/// Converts an iterator to an array.
public func fromIter<T>(iter : Iter.Iter<T>) : [T] {
public func fromIter<T>(iter : Types.Iter<T>) : [T] {
todo() // Pending `List` data structure
};

Expand All @@ -622,7 +622,7 @@ module {
/// Runtime: O(1)
///
/// Space: O(1)
public func keys<T>(array : [T]) : Iter.Iter<Nat> = array.keys();
public func keys<T>(array : [T]) : Types.Iter<Nat> = array.keys();

/// Iterator provides a single method `next()`, which returns
/// elements in order, or `null` when out of elements to iterate over.
Expand All @@ -643,7 +643,7 @@ module {
/// Runtime: O(1)
///
/// Space: O(1)
public func values<T>(array : [T]) : Iter.Iter<T> = array.vals();
public func values<T>(array : [T]) : Types.Iter<T> = array.vals();

/// Iterator provides a single method `next()`, which returns
/// pairs of (index, element) in order, or `null` when out of elements to iterate over.
Expand All @@ -661,7 +661,7 @@ module {
/// Runtime: O(1)
///
/// Space: O(1)
public func enumerate<T>(array : [var T]) : Iter.Iter<(Nat, T)> = object {
public func enumerate<T>(array : [var T]) : Types.Iter<(Nat, T)> = object {
let size = array.size();
var index = 0;
public func next() : ?(Nat, T) {
Expand Down Expand Up @@ -832,7 +832,7 @@ module {
/// Runtime: O(1)
///
/// Space: O(1)
public func range<T>(array : [T], fromInclusive : Int, toExclusive : Int) : Iter.Iter<T> {
public func range<T>(array : [T], fromInclusive : Int, toExclusive : Int) : Types.Iter<T> {
let size = array.size();
// Convert negative indices to positive and handle bounds
let startInt = if (fromInclusive < 0) {
Expand Down
3 changes: 2 additions & 1 deletion src/Blob.mo
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
/// }
/// ```

import Types "Types";
import Prim "mo:⛔";

module {
Expand Down Expand Up @@ -84,7 +85,7 @@ module {
/// let blob = "\00\FF\00" : Blob;
/// Blob.hash(blob) // => 1_818_567_776
/// ```
public func hash(blob : Blob) : Nat32 = Prim.hashBlob blob;
public func hash(blob : Blob) : Types.Hash = Prim.hashBlob blob;

/// General purpose comparison function for `Blob` by comparing the value of
/// the bytes. Returns the `Order` (either `#less`, `#equal`, or `#greater`)
Expand Down
25 changes: 0 additions & 25 deletions src/Hash.mo

This file was deleted.

2 changes: 0 additions & 2 deletions src/Int.mo
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@

import Prim "mo:⛔";
import Char "Char";
import Hash "Hash";
import Runtime "Runtime";
import Iter "Iter";
import { todo } "Debug";

module {

Expand Down
3 changes: 2 additions & 1 deletion src/Iter.mo
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Array "Array";
import VarArray "VarArray";
import Prim "mo:prim";
import Runtime "Runtime";
import Types "Types";

module {

Expand All @@ -21,7 +22,7 @@ module {
/// …do something with x…
/// }
/// ```
public type Iter<T> = { next : () -> ?T };
public type Iter<T> = Types.Iter<T>;

public func empty<T>() : Iter<T> {
object {
Expand Down
5 changes: 3 additions & 2 deletions src/List.mo
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import Iter "Iter";
import Order "Order";
import Result "Result";
import Types "Types";
import { todo } "Debug";

module {
public type List<T> = (); // Placeholder
public type List<T> = Types.List<T>;

public func empty<T>() : List<T> {
todo()
Expand Down Expand Up @@ -60,7 +61,7 @@ module {
todo()
};

public func hash<T>(list : List<T>, hash : T -> Nat32) : Nat32 {
public func hash<T>(list : List<T>, hash : T -> Nat32) : Types.Hash {
todo()
};

Expand Down
51 changes: 16 additions & 35 deletions src/Map.mo
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
// Copyright (c) 2022 Byron Becker.
// Distributed under Apache 2.0 license

// import ImmutableMap "immutable/Map";
import IterType "type/Iter";
import Types "Types";
import Order "Order";
import VarArray "VarArray";
import Runtime "Runtime";
Expand All @@ -37,29 +36,11 @@ import Option "Option";
module {
let btreeOrder = 32; // Should be >= 4 and <= 512.

public type Node<K, V> = {
#leaf : Leaf<K, V>;
#internal : Internal<K, V>
};

public type Data<K, V> = {
kvs : [var ?(K, V)];
var count : Nat
};

public type Internal<K, V> = {
data : Data<K, V>;
children : [var ?Node<K, V>]
};

public type Leaf<K, V> = {
data : Data<K, V>
};

public type Map<K, V> = {
var root : Node<K, V>;
var size : Nat
};
public type Node<K, V> = Types.Map.Node<K, V>;
public type Data<K, V> = Types.Map.Data<K, V>;
public type Internal<K, V> = Types.Map.Internal<K, V>;
public type Leaf<K, V> = Types.Map.Leaf<K, V>;
public type Map<K, V> = Types.Map<K, V>;

// /// Convert the mutable key-value map to an immutable key-value map.
// ///
Expand Down Expand Up @@ -676,7 +657,7 @@ module {
/// where `n` denotes the number of key-value entries stored in the map.
///
/// Note: Creates `O(log(n))` temporary objects that will be collected as garbage.
public func entries<K, V>(map : Map<K, V>) : IterType.Iter<(K, V)> {
public func entries<K, V>(map : Map<K, V>) : Types.Iter<(K, V)> {
switch (map.root) {
case (#leaf(leafNode)) { return leafEntries(leafNode) };
case (#internal(internalNode)) { internalEntries(internalNode) }
Expand Down Expand Up @@ -713,7 +694,7 @@ module {
/// where `n` denotes the number of key-value entries stored in the map.
///
/// Note: Creates `O(log(n))` temporary objects that will be collected as garbage.
public func reverseEntries<K, V>(map : Map<K, V>) : IterType.Iter<(K, V)> {
public func reverseEntries<K, V>(map : Map<K, V>) : Types.Iter<(K, V)> {
switch (map.root) {
case (#leaf(leafNode)) { return reverseLeafEntries(leafNode) };
case (#internal(internalNode)) { reverseInternalEntries(internalNode) }
Expand Down Expand Up @@ -747,7 +728,7 @@ module {
/// Cost of iteration over all elements:
/// Runtime: `O(n)`.
/// Space: `O(1)`.
public func keys<K, V>(map : Map<K, V>) : IterType.Iter<K> {
public func keys<K, V>(map : Map<K, V>) : Types.Iter<K> {
object {
let iterator = entries(map);

Expand Down Expand Up @@ -787,7 +768,7 @@ module {
/// Cost of iteration over all elements:
/// Runtime: `O(n)`.
/// Space: `O(1)`.
public func values<K, V>(map : Map<K, V>) : IterType.Iter<V> {
public func values<K, V>(map : Map<K, V>) : Types.Iter<V> {
object {
let iterator = entries(map);

Expand Down Expand Up @@ -818,7 +799,7 @@ module {
/// Space: `O(n)`.
/// where `n` denotes the number of key-value entries returned by the iterator and
/// assuming that the `compare` function implements an `O(1)` comparison.
public func fromIter<K, V>(iter : IterType.Iter<(K, V)>, compare : (K, K) -> Order.Order) : Map<K, V> {
public func fromIter<K, V>(iter : Types.Iter<(K, V)>, compare : (K, K) -> Order.Order) : Map<K, V> {
let map = empty<K, V>();
for ((key, value) in iter) {
add(map, compare, key, value)
Expand Down Expand Up @@ -1152,7 +1133,7 @@ module {
/// Can be used to check that key/value pairs have been inserted with a consistent key comparison function.
/// Traps if the internal map structure is invalid.
public func assertValid<K, V>(map : Map<K, V>, compare : (K, K) -> Order.Order) : () {
func checkIteration(iterator : IterType.Iter<(K, V)>, order : Order.Order) {
func checkIteration(iterator : Types.Iter<(K, V)>, order : Order.Order) {
switch (iterator.next()) {
case null {};
case (?first) {
Expand Down Expand Up @@ -1282,7 +1263,7 @@ module {
}
};

func leafEntries<K, V>({ data } : Leaf<K, V>) : IterType.Iter<(K, V)> {
func leafEntries<K, V>({ data } : Leaf<K, V>) : Types.Iter<(K, V)> {
var i : Nat = 0;
object {
public func next() : ?(K, V) {
Expand All @@ -1297,7 +1278,7 @@ module {
}
};

func reverseLeafEntries<K, V>({ data } : Leaf<K, V>) : IterType.Iter<(K, V)> {
func reverseLeafEntries<K, V>({ data } : Leaf<K, V>) : Types.Iter<(K, V)> {
var i : Nat = data.count;
object {
public func next() : ?(K, V) {
Expand All @@ -1315,7 +1296,7 @@ module {
// Cursor type that keeps track of the current node and the current key-value index in the node
type NodeCursor<K, V> = { node : Node<K, V>; kvIndex : Nat };

func internalEntries<K, V>(internal : Internal<K, V>) : IterType.Iter<(K, V)> {
func internalEntries<K, V>(internal : Internal<K, V>) : Types.Iter<(K, V)> {
object {
// The nodeCursorStack keeps track of the current node and the current key-value index in the node
// We use a stack here to push to/pop off the next node cursor to visit
Expand Down Expand Up @@ -1396,7 +1377,7 @@ module {
}
};

func reverseInternalEntries<K, V>(internal : Internal<K, V>) : IterType.Iter<(K, V)> {
func reverseInternalEntries<K, V>(internal : Internal<K, V>) : Types.Iter<(K, V)> {
object {
// The nodeCursorStack keeps track of the current node and the current key-value index in the node
// We use a stack here to push to/pop off the next node cursor to visit
Expand Down
10 changes: 3 additions & 7 deletions src/Order.mo
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
/// Utilities for `Order` (comparison between two values).

import Iter "type/Iter";
import Types "Types";
import { todo } "Debug";

module {

/// A type to represent an order.
public type Order = {
#less;
#equal;
#greater
};
public type Order = Types.Order;

/// Check if an order is #less.
public func isLess(order : Order) : Bool {
Expand Down Expand Up @@ -46,7 +42,7 @@ module {
}
};

public func allValues() : Iter.Iter<Order> {
public func allValues() : Types.Iter<Order> {
todo()
}

Expand Down
4 changes: 2 additions & 2 deletions src/Principal.mo
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@

import Prim "mo:⛔";
import Blob "Blob";
import Hash "Hash";
import Array "Array";
import VarArray "VarArray";
import Nat8 "Nat8";
import Nat32 "Nat32";
import Nat64 "Nat64";
import Text "Text";
import Types "Types";

module {

Expand Down Expand Up @@ -197,7 +197,7 @@ module {
/// let principal = Principal.fromText("un4fu-tqaaa-aaaab-qadjq-cai");
/// Principal.hash(principal) // => 2_742_573_646
/// ```
public func hash(principal : Principal) : Hash.Hash = Blob.hash(Prim.blobOfPrincipal(principal));
public func hash(principal : Principal) : Types.Hash = Blob.hash(Prim.blobOfPrincipal(principal));

/// General purpose comparison function for `Principal`. Returns the `Order` (
/// either `#less`, `#equal`, or `#greater`) of comparing `principal1` with
Expand Down
3 changes: 2 additions & 1 deletion src/Queue.mo
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
import Iter "Iter";
import Immutable "immutable/Queue";
import Order "Order";
import Types "Types";
import { todo } "Debug";

module {

public type Queue<T> = { var immutable : Immutable.Queue<T> };
public type Queue<T> = Types.Queue<T>;

public func freeze<T>(queue : Queue<T>) : Immutable.Queue<T> = queue.immutable;

Expand Down
2 changes: 1 addition & 1 deletion src/Region.mo
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ module {
public let new : () -> Region = Prim.regionNew;

/// Return a Nat identifying the given region.
/// Maybe be used for equality, comparison and hashing.
/// May be used for equality, comparison and hashing.
/// NB: Regions returned by `new()` are numbered from 16
/// (regions 0..15 are currently reserved for internal use).
/// Allocate a new, isolated Region of size 0.
Expand Down
6 changes: 2 additions & 4 deletions src/Result.mo
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// Error handling with the Result type.

import Order "Order";
import Types "Types";

module {

Expand All @@ -18,10 +19,7 @@ module {
/// case (#err(msg)) { Debug.print("Failed to create user with the error: " # msg) };
/// }
/// ```
public type Result<Ok, Err> = {
#ok : Ok;
#err : Err
};
public type Result<Ok, Err> = Types.Result<Ok, Err>;

// Compares two Result's for equality.
public func equal<Ok, Err>(
Expand Down
Loading