-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.mo
179 lines (162 loc) · 5.11 KB
/
main.mo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import Array "mo:base/Array";
import Int "mo:base/Int";
import Iter "mo:base/Iter";
import Text "mo:base/Text";
import Principal "mo:base/Principal";
import Time "mo:base/Time";
import List "mo:base/List";
actor{
private type Time = Time.Time;
private type Message = {
text: Text;
time: Time;
};
private type MyBlog = actor{
follow: shared (Principal) -> async ();
follows: shared query() -> async [Principal];
post: shared (Time) -> async ();
posts: shared query (Time) -> async [Message];
timeline: shared (Time) -> async [Message];
};
stable var followed : List.List<Principal> = List.nil();
stable var messages : List.List<Message> = List.nil();
public shared func follow(id: Principal) : async (){
followed := List.push(id, followed);
};
public shared query func follows() : async [Principal]{
List.toArray(followed)
};
public shared ({caller}) func post(text: Text) : async (){
//Security Check
//assert(Principal.toText(caller) == "...");
var msg : Message = {
text = text;
time = Time.now();
};
messages := List.push(msg, messages);
};
public shared query func posts(since: Time) : async [Message]{
var all : List.List<Message> = List.nil();
for(msg in Iter.fromArray(List.toArray(messages))){
if(msg.time >= since){
all := List.push(msg, all);
};
};
List.toArray(all)
};
public shared func timeline(since: Time) : async [Message]{
var all : List.List<Message> = List.nil();
for(id in Iter.fromList(followed)){
let canister : MyBlog = actor(Principal.toText(id));
let msgs = await canister.posts(since);
for(msg in Iter.fromArray(msgs)){
all := List.push(msg, all);
};
};
List.toArray(all)
};
public type ChunkId = Nat;
public type HeaderField = (Text, Text);
public type StreamingStrategy = {
#Callback : {
token : StreamingCallbackToken;
callback : shared query StreamingCallbackToken -> async StreamingCallbackHttpResponse;
};
};
public type StreamingCallbackToken = {
key : Text;
sha256 : ?[Nat8];
index : Nat;
content_encoding : Text;
};
public type HttpRequest = {
url : Text;
method : Text;
body : [Nat8];
headers : [HeaderField];
};
public type HttpResponse = {
body : Blob;
headers : [HeaderField];
streaming_strategy : ?StreamingStrategy;
status_code : Nat16;
};
public type Key = Text;
public type Path = Text;
public type SetAssetContentArguments = {
key : Key;
sha256 : ?[Nat8];
chunk_ids : [ChunkId];
content_encoding : Text;
};
public type StreamingCallbackHttpResponse = {
token : ?StreamingCallbackToken;
body : [Nat8];
};
stable var currentValue : Nat = 0;
// Increment the counter with the increment function.
public func increment() : async () {
currentValue += 1;
};
// Read the counter value with a get function.
public query func get() : async Nat {
currentValue
};
// Write an arbitrary value with a set function.
public func set(n: Nat) : async () {
currentValue := n;
};
public func greet(name : Text) : async Text {
return "Hello, " # name # "!";
};
public shared query func http_request(request : HttpRequest) : async HttpResponse {
{
body = Text.encodeUtf8("<html><body>"#debug_show(currentValue)#"</body></html>");
headers = [];
streaming_strategy = null;
status_code = 200;
}
};
public shared({caller}) func qsort(arr : [Int]) : async [Int]{
if(Iter.size<Int>(Array.vals<Int>(arr)) == 0){
return [];
};
var re0_arr = Array.make<Int>(arr[0]);
re0_arr := Array.append<Int>(re0_arr, arr);
var tmp_arr : [var Int] = Array.thaw<Int>(re0_arr);
sort(tmp_arr, 1, Iter.size<Int>(Array.vals<Int>(arr)));
var ans_arr : [Int] = [];
for(i in Iter.range(1, Iter.size<Int>(Array.vals<Int>(arr)))){
ans_arr := Array.append<Int>(ans_arr, Array.make<Int>(tmp_arr[i]));
};
ans_arr
};
private func sort(s : [var Int], start : Nat, end : Nat) : (){ //初次为0, length - 1
var i = start;
var j = end;
s[0] := s[start];
while(i < j){
while(i < j and s[0] < s[j]){
j := j - 1;
};
if(i < j){
s[i] := s[j];
i := i + 1;
};
while(i < j and s[i] <= s[0]){
i := i + 1;
};
if(i < j){
s[j] := s[i];
j := j - 1;
};
};
s[i] := s[0];
if(start < i){
sort(s, start, j - 1);
};
if(i < end){
sort(s, j + 1, end);
};
};
}