Skip to content

Commit

Permalink
Remove Hash module and Text.hash() (#133)
Browse files Browse the repository at this point in the history
* Refactor to use 'Types' module

* Remove unrelated change from diff

* Update API lockfile

* Refactor

* Remove 'Hash' module and refactor type

* Remove 'Text.hash()'

* Update API lockfile
  • Loading branch information
rvanasa authored Feb 6, 2025
1 parent 1d56879 commit 9eac551
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 62 deletions.
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
2 changes: 1 addition & 1 deletion src/List.mo
Original file line number Diff line number Diff line change
Expand Up @@ -61,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
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
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
17 changes: 0 additions & 17 deletions src/Text.mo
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import Char "Char";
import Iter "Iter";
import Hash "Hash";
import Stack "Stack";
import Types "Types";
import Prim "mo:⛔";
Expand Down Expand Up @@ -149,22 +148,6 @@ module {
/// ```
public func size(t : Text) : Nat { t.size() };

/// Returns a hash obtained by using the `djb2` algorithm ([more details](http://www.cse.yorku.ca/~oz/hash.html)).
///
/// ```motoko include=import
/// let hash = Text.hash("abc");
/// ```
///
/// Note: this algorithm is intended for use in data structures rather than as a cryptographic hash function.
public func hash(t : Text) : Hash.Hash {
var x : Nat32 = 5381;
for (char in t.chars()) {
let c : Nat32 = Prim.charToNat32(char);
x := ((x << 5) +% x) +% c
};
return x
};

/// Returns `t1 # t2`, where `#` is the `Text` concatenation operator.
///
/// ```motoko include=import
Expand Down
16 changes: 3 additions & 13 deletions validation/api/api.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"public func fromVarArray(bytes : [var Nat8]) : Blob",
"public func greater(blob1 : Blob, blob2 : Blob) : Bool",
"public func greaterOrEqual(blob1 : Blob, blob2 : Blob) : Bool",
"public func hash(blob : Blob) : Nat32",
"public func hash(blob : Blob) : Types.Hash",
"public func less(blob1 : Blob, blob2 : Blob) : Bool",
"public func lessOrEqual(blob1 : Blob, blob2 : Blob) : Bool",
"public func notEqual(blob1 : Blob, blob2 : Blob) : Bool",
Expand Down Expand Up @@ -187,15 +187,6 @@
"public func identity<A>(x : A) : A"
]
},
{
"name": "Hash",
"exports": [
"public func bit(h : Hash, pos : Nat) : Bool",
"public func equal(ha : Hash, hb : Hash) : Bool",
"public type Hash",
"public let length : Nat"
]
},
{
"name": "Int",
"exports": [
Expand Down Expand Up @@ -505,7 +496,7 @@
"public func fromArray<T>(array : [T]) : List<T>",
"public func fromIter<T>(iter : { next : () -> ?T }) : List<T>",
"public func fromVarArray<T>(array : [var T]) : List<T>",
"public func hash<T>(list : List<T>, hash : T -> Nat32) : Nat32",
"public func hash<T>(list : List<T>, hash : T -> Nat32) : Types.Hash",
"public func indexOf<T>(list : List<T>, element : T, equal : (T, T) -> Bool) : ?Nat",
"public func indexOfList<T>(list : List<T>, subList : List<T>, equal : (T, T) -> Bool) : ?Nat",
"public func isEmpty<T>(list : List<T>) : Bool",
Expand Down Expand Up @@ -853,7 +844,7 @@
"public func fromText(t : Text) : Principal",
"public func greater(principal1 : Principal, principal2 : Principal) : Bool",
"public func greaterOrEqual(principal1 : Principal, principal2 : Principal) : Bool",
"public func hash(principal : Principal) : Hash.Hash",
"public func hash(principal : Principal) : Types.Hash",
"public func isAnonymous(p : Principal) : Bool",
"public func isCanister(p : Principal) : Bool",
"public func isController(p : Principal) : Bool",
Expand Down Expand Up @@ -1076,7 +1067,6 @@
"public func fromVarArray(a : [var Char]) : Text",
"public func greater(t1 : Text, t2 : Text) : Bool",
"public func greaterOrEqual(t1 : Text, t2 : Text) : Bool",
"public func hash(t : Text) : Hash.Hash",
"public func join(sep : Text, ts : Iter.Iter<Text>) : Text",
"public func less(t1 : Text, t2 : Text) : Bool",
"public func lessOrEqual(t1 : Text, t2 : Text) : Bool",
Expand Down

0 comments on commit 9eac551

Please sign in to comment.