-
Notifications
You must be signed in to change notification settings - Fork 12
/
set.red
82 lines (74 loc) · 1.28 KB
/
set.red
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
Red[]
set!: object [
data: []
on-deep-change*: func [
owner word target action new index part
/local mark
][
all [
word = 'data
find [poke insert append] action
mark: find data new
remove mark
]
]
]
sorted-set!: object [
data: []
steps: 0
internal?: no
on-deep-change*: func [
owner word target action new index part
/local mark
][
probe action
if word = 'data [
switch action [
insert append [
if any [
not block? new
odd? length? new
][
do make error! "Invalid data"
]
if mark: find data new/1 [
owner/internal?: true
remove mark
]
]
inserted appended [
sort/skip/compare data 2 1 ; sort lexicographically first
sort/skip/compare data 2 2 ; then sort by score
]
remove [
if zero? steps [owner/steps: part * 2]
]
removed [
unless zero? owner/steps [
remove data
owner/steps: owner/steps - 1
]
]
poke [
; do make error! "Action not supported"
print [#poke index new]
]
]
]
]
]
make-set: func [/local value][
value: make set! []
value/data
]
make-sorted-set: func [/local value][
value: make sorted-set! []
value/data
]
test: [
s: make-sorted-set
append s [a 5]
append s [b 3]
append s [c 1]
append s [d 4]
]