1
1
import List "mo:base/List" ;
2
2
import Option "mo:base/Option" ;
3
- import Trie "mo:base/Trie " ;
3
+ import Map "mo:base/OrderedMap " ;
4
4
import Nat32 "mo:base/Nat32" ;
5
5
6
6
actor Superheroes {
@@ -15,18 +15,19 @@ actor Superheroes {
15
15
// The type of a superhero.
16
16
public type Superhero = {
17
17
name : Text ;
18
- superpowers : List . List < Text > ;
18
+ superpowers : List . List < Text >
19
19
};
20
20
21
21
/**
22
22
* Application State
23
23
*/
24
24
25
25
// The next available superhero identifier.
26
- private stable var next : SuperheroId = 0 ;
26
+ stable var next : SuperheroId = 0 ;
27
27
28
28
// The superhero data store.
29
- private stable var superheroes : Trie . Trie < SuperheroId , Superhero > = Trie . empty();
29
+ let Ops = Map . Make < SuperheroId > (Nat32 . compare);
30
+ stable var map : Map . Map < SuperheroId , Superhero > = Ops . empty();
30
31
31
32
/**
32
33
* High-Level API
@@ -36,57 +37,33 @@ actor Superheroes {
36
37
public func create(superhero : Superhero ) : async SuperheroId {
37
38
let superheroId = next;
38
39
next += 1 ;
39
- superheroes := Trie . replace(
40
- superheroes,
41
- key(superheroId),
42
- Nat32 . equal,
43
- ?superhero,
44
- ). 0 ;
45
- return superheroId;
40
+ map := Ops . put(map, superheroId, superhero);
41
+ return superheroId
46
42
};
47
43
48
44
// Read a superhero.
49
45
public query func read(superheroId : SuperheroId ) : async ?Superhero {
50
- let result = Trie . find(superheroes, key( superheroId), Nat32 . equal );
51
- return result;
46
+ let result = Ops . get(map, superheroId);
47
+ return result
52
48
};
53
49
54
50
// Update a superhero.
55
51
public func update(superheroId : SuperheroId , superhero : Superhero ) : async Bool {
56
- let result = Trie . find(superheroes, key( superheroId), Nat32 . equal );
57
- let exists = Option . isSome(result );
52
+ let ( result, old_value) = Ops . replace(map, superheroId, superhero );
53
+ let exists = Option . isSome(old_value );
58
54
if (exists) {
59
- superheroes := Trie . replace(
60
- superheroes,
61
- key(superheroId),
62
- Nat32 . equal,
63
- ?superhero,
64
- ). 0 ;
55
+ map := result
65
56
};
66
- return exists;
57
+ return exists
67
58
};
68
59
69
60
// Delete a superhero.
70
61
public func delete(superheroId : SuperheroId ) : async Bool {
71
- let result = Trie . find(superheroes, key( superheroId), Nat32 . equal );
72
- let exists = Option . isSome(result );
62
+ let ( result, old_value) = Ops . remove(map, superheroId);
63
+ let exists = Option . isSome(old_value );
73
64
if (exists) {
74
- superheroes := Trie . replace(
75
- superheroes,
76
- key(superheroId),
77
- Nat32 . equal,
78
- null ,
79
- ). 0 ;
65
+ map := result
80
66
};
81
- return exists;
82
- };
83
-
84
- /**
85
- * Utilities
86
- */
87
-
88
- // Create a trie key from a superhero identifier.
89
- private func key(x : SuperheroId ) : Trie . Key < SuperheroId > {
90
- return { hash = x; key = x };
91
- };
92
- };
67
+ return exists
68
+ }
69
+ }
0 commit comments