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
use std::str::from_utf8;
use std::fmt;
use std::cmp::Ordering;
use memory::*;
use dispatch::*;
use handle::Handle;
pub mod guide;
use self::guide::Guide;
pub struct Symbol_ { }
pub fn prism_unit() -> Unit { mechanism::prism::<Symbol_>() }
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_symbol(h: Handle) -> bool { find_prism(h).is_some() }
pub fn new(name: &[u8], solidus_position: u32) -> Unit {
let guide = {
let byte_count = name.len() as u32;
let content_count = units_for(byte_count);
let needed = 1 + Guide::units() + content_count;
let s = Segment::new(needed);
let prism = s.line_at(0);
prism.set(0, prism_unit());
let g = Guide::new(prism, solidus_position, byte_count);
g.root.set(content_count as i32 - 1, Unit::zero());
g.byte_slice().copy_from_slice(name);
g
};
guide.store().segment().unit()
}
pub fn new_prefix_name(prefix: &[u8], name: &[u8]) -> Unit {
let b = format!("{}/{}", from_utf8(prefix).unwrap(), from_utf8(name).unwrap());
new(b.as_bytes(), prefix.len() as u32)
}
pub fn has_namespace(prism: AnchoredLine) -> bool {
assert!(is_prism(prism));
let guide = Guide::hydrate(prism);
guide.solidus != 0
}
pub fn units_for(byte_count: u32) -> u32 {
let (b, c) = if cfg!(target_pointer_width = "32") { (4, 2) } else { (8, 3) };
(byte_count + b - 1) >> c
}
impl Dispatch for Symbol_ { }
impl Identification for Symbol_ {
fn type_name(&self) -> &'static str { "Symbol" }
}
impl Distinguish for Symbol_ {
fn hash(&self, prism: AnchoredLine) -> u32 {
let guide = Guide::hydrate(prism);
if guide.has_hash() { return guide.hash; }
let h = {
use random::PI;
use hash::{mix_range, end};
let iv: (u64, u64, u64, u64) = (PI[14], PI[15], PI[16], PI[17]);
let unit_count = units_for(guide.count);
let a = mix_range(guide.root.span(unit_count), iv);
let (x, _y) = end(a.0, a.1, a.2, a.3);
x as u32
};
guide.set_hash(h).store_hash().hash
}
fn eq(&self, prism: AnchoredLine, other: Unit) -> bool {
let self_usize = self as *const Symbol_ as usize;
let o = other.handle();
if let Some(o_sym) = find_prism(o) {
let g = Guide::hydrate(prism);
let h = Guide::hydrate(o_sym);
return g.byte_slice() == h.byte_slice()
} else {
false
}
}
fn cmp(&self, prism: AnchoredLine, other: Unit) -> Option<Ordering> {
let o = other.handle();
if let Some(o_sym) = find_prism(o) {
let g = Guide::hydrate(prism);
let h = Guide::hydrate(o_sym);
Some(g.str().cmp(&h.str()))
} else {
if o.is_ref() {
let o_prism_unit = o.logical_value()[0];
Some(prism_unit().cmp(&o_prism_unit))
} else {
Some(Ordering::Greater)
}
}
}
}
impl Aggregate for Symbol_ { }
impl Sequential for Symbol_ { }
impl Associative for Symbol_ { }
impl Reversible for Symbol_ { }
impl Sorted for Symbol_ { }
impl Notation for Symbol_ {
fn edn(&self, prism: AnchoredLine, f: &mut fmt::Formatter) -> fmt::Result {
let guide = Guide::hydrate(prism);
write!(f, "{}", guide.str())
}
}
impl Numeral for Symbol_ { }
impl Callable for Symbol_ { }
#[cfg(test)]
mod tests {
use super::*;
}