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
180
181
use std::fmt;
use memory::*;
use dispatch::*;
use value::*;
use map;
use handle;
use handle::Handle;
use transduce::{Process};
use vector::guide::Guide;
pub struct Set_ { }
pub fn prism_unit() -> Unit { mechanism::prism::<Set_>() }
pub fn is_prism(prism: AnchoredLine) -> bool { prism[0] == prism_unit() }
pub fn find_prism(h: Handle) -> Option<AnchoredLine> { h.find_prism(prism_unit()) }
pub fn is_set(h: Handle) -> bool { find_prism(h).is_some() }
pub fn new() -> Unit {
let s = map::new().segment();
s.set(0, prism_unit());
s.unit()
}
pub fn new_value() -> Value { new().handle().value() }
impl Dispatch for Set_ {
fn tear_down(&self, prism: AnchoredLine) {
map::tear_down::tear_down(prism, 0);
}
fn alias_components(&self, prism: AnchoredLine) { map::alias_components(prism, 0); }
}
impl Identification for Set_ {
fn type_name(&self) -> &'static str { "Set" }
}
impl Distinguish for Set_ {
fn hash(&self, prism: AnchoredLine) -> u32 {
let guide = Guide::hydrate(prism);
if guide.has_hash() {
return guide.hash;
}
use random::{PI, cycle_abc};
struct Pointer {
pub ptr: *mut u64,
}
impl Process for Pointer {
fn inges(&mut self, stack: &mut [Box<dyn Process>], v: &Value) -> Option<Value> {
let vh = v.hash() as u64;
let h = cycle_abc(181, (vh << 32) | vh);
unsafe {
*self.ptr = (*self.ptr).wrapping_add(h);
}
None
}
fn last_call(&mut self, stack: &mut [Box<dyn Process>]) -> Value {
Handle::nil().value()
}
}
let mut y = cycle_abc(97, PI[487] + guide.count as u64);
let mut procs: [Box<dyn Process>; 1] = [Box::new(Pointer { ptr: (&mut y) as *mut u64 })];
let _ = map::reduce::reduce(prism, &mut procs, 0);
let h = cycle_abc(27, y) as u32;
guide.set_hash(h).store_hash().hash
}
fn eq(&self, prism: AnchoredLine, other: Unit) -> bool {
let o = other.handle();
if let Some(s_prism) = find_prism(o) {
let res = map::eq::eq(Guide::hydrate(prism), Guide::hydrate(s_prism), 0);
return res
}
false
}
}
impl Aggregate for Set_ {
fn is_aggregate(&self, prism: AnchoredLine) -> bool { true }
fn count(&self, prism: AnchoredLine) -> u32 {
let guide = Guide::hydrate(prism);
guide.count
}
fn empty(&self, prism: AnchoredLine) -> Unit { new() }
fn conj(&self, prism: AnchoredLine, x: Unit) -> Unit {
let k = x;
let h = k.handle().hash();
let (g, key_slot) = map::assoc::assoc(prism, k, h, 0);
match key_slot {
Ok(new_slot) => {
new_slot.set(0, k);
g.inc_count().store().segment().unit()
},
Err(old_slot) => {
k.handle().retire();
g.store().segment().unit()
},
}
}
fn get(&self, prism: AnchoredLine, k: Unit) -> *const Unit {
let h = k.handle().hash();
if let Some(key_line) = map::get::get(prism, k, h, 0) {
key_line.line().star()
} else {
(& handle::STATIC_NIL) as *const Unit
}
}
fn reduce(&self, prism: AnchoredLine, process: &mut [Box<dyn Process>]) -> Value {
map::reduce::reduce(prism, process, 0)
}
}
impl Sequential for Set_ {}
impl Associative for Set_ {
fn is_set(&self, prism: AnchoredLine) -> bool { true }
fn contains(&self, prism: AnchoredLine, k: Unit) -> bool {
let h = k.handle().hash();
map::get::get(prism, k, h, 0).is_some()
}
fn dissoc(&self, prism: AnchoredLine, k: Unit) -> Unit {
let h = k.handle().hash();
let g = map::dissoc::dissoc(prism, k, h, 0);
g.segment().unit()
}
}
impl Reversible for Set_ {}
impl Sorted for Set_ {}
impl Notation for Set_ {
fn edn(&self, prism: AnchoredLine, f: &mut fmt::Formatter) -> fmt::Result {
struct Printer {
pub is_first: bool,
pub f: usize,
}
impl Printer {
pub fn new(f: &mut fmt::Formatter) -> Printer {
use std::mem::transmute;
unsafe { Printer { is_first: true, f: transmute::<& fmt::Formatter, usize>(f) } }
}
}
impl Process for Printer {
fn inges(&mut self, stack: &mut [Box<dyn Process>], v: &Value) -> Option<Value> {
use std::mem::transmute;
write!(unsafe { transmute::<usize, &mut fmt::Formatter>(self.f) },
"{}{}",
if self.is_first { self.is_first = false; "" } else { " " },
v).unwrap();
None
}
fn last_call(&mut self, stack: &mut [Box<dyn Process>]) -> Value {
Handle::nil().value()
}
}
write!(f, "#{{")?;
let mut procs: [Box<dyn Process>; 1] = [Box::new(Printer::new(f))];
let _ = map::reduce::reduce(prism, &mut procs, 0);
write!(f, "}}")
}
}
impl Numeral for Set_ { }
impl Callable for Set_ { }
#[cfg(test)]
mod tests {
use super::*;
}