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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright (c) Cole Frederick. All rights reserved.
// The use and distribution terms for this software are covered by the
// Eclipse Public License 1.0 (https://opensource.org/licenses/eclipse-1.0.php)
// which can be found in the file epl-v10.html at the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by the terms of this license.
// You must not remove this notice, or any other, from this software.

//! Indexed array mapped trie, supporting vectors and lists.

use std::fmt;
use std::cmp::Ordering;
use memory::*;
use dispatch::*;
use value::*;
use handle::Handle;
use transduce::Process;

pub mod guide;
use self::guide::Guide;
pub mod conj;
pub mod pop;
pub mod nth;
pub mod assoc;
pub mod eq;
pub mod tear_down;
pub mod reduce;
pub mod iter;
pub mod harness;
pub mod util;
use self::util::*;

/// Defines branching factor.
///
/// Can be 4, 5 or 6, making for sixteen, thirty-two or sixty-four way branching.
pub const BITS: u32 = 4; // one of 4, 5, 6
pub const ARITY: u32 = 1 << BITS;
pub const TAIL_CAP: u32 = ARITY;
pub const MASK: u32 = ARITY - 1;

/// Vector dispatch.
pub struct Vector_ { }
pub fn prism_unit() -> Unit { mechanism::prism::<Vector_>() }
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_vector(h: Handle) -> bool { find_prism(h).is_some() }

pub fn new() -> Unit {
    //log!("Vector new");
    let guide = {
        let cap = 1 /*prism*/ + Guide::units() + size(1);
        let s = Segment::new(cap);
        let prism = s.line_at(0);
        prism.set(0, prism_unit());
        let mut g = Guide::hydrate_top_bot(prism, 0, 0);
        g.is_compact_bit = 0x1;
        g
    };
    guide.store().segment().unit()
}
pub fn new_value() -> Value { new().handle().value() }

pub fn alias_components(prism: AnchoredLine) {
    let guide = Guide::hydrate(prism);
    if guide.count <= TAIL_CAP {
        guide.root.span(guide.count).split()
    } else {
        let root_count = root_content_count(tailoff(guide.count));
        let tail_and_roots = guide.root.offset(-1).span(root_count + 1);
        tail_and_roots.split()
    }
}
pub fn unaliased(prism: AnchoredLine) -> AnchoredLine {
    let seg = prism.segment();
    if seg.is_aliased() {
        if prism.index() == 0 {
            alias_components(prism);
        } else {
            seg.unit().handle()._alias_components();
        }
        let s = seg.carbon_copy();
        let p = prism.with_seg(s);
        if !seg.is_aliased() {
            //panic!("Race condition here!");
        }
        seg.unit().handle().retire();
        p
    } else {
        prism
    }
}

impl Dispatch for Vector_ {
    fn tear_down(&self, prism: AnchoredLine) {
        //group!("Vector tear_down");
        tear_down::tear_down(prism);
        //group_end!();
    }
    fn alias_components(&self, prism: AnchoredLine) { alias_components(prism); }
}
impl Identification for Vector_ {
    fn type_name(&self) -> &'static str { "Vector" }
}
impl Distinguish for Vector_ {
    fn hash(&self, prism: AnchoredLine) -> u32 {
        let guide = Guide::hydrate(prism);
        if guide.has_hash() {
            return guide.hash;
        }
        //group!("Vector 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 h = v.hash() as u64;
                unsafe {
                    *self.ptr = cycle_abc(34, *self.ptr + h);
                }
                None
            }
            fn last_call(&mut self, _stack: &mut [Box<dyn Process>]) -> Value { Handle::nil().value() }
        }

        let mut y = cycle_abc(7, PI[321].wrapping_add(guide.count as u64));
        let mut procs: [Box<dyn Process>; 1] = [Box::new(Pointer { ptr: (&mut y) as *mut u64 })];
        let _ = reduce::reduce(prism, &mut procs);
        let h = cycle_abc(210, y) as u32;
        //log!("Hash of vector: {:#08X}", h);
        //group_end!();
        guide.set_hash(h).store_hash().hash
    }
    fn eq(&self, prism: AnchoredLine, other: Unit) -> bool {
        let o = other.handle();
        if let Some(v_prism) = find_prism(o) {
            //group!("Vector eq");
            let res = eq::eq(Guide::hydrate(prism), Guide::hydrate(v_prism));
            //group_end!();
            return res
        }
        use list;
        if let Some(l_prism) = list::find_prism(o) {
            let ct = {
                let ct = Guide::hydrate(prism).count;
                if Guide::hydrate(l_prism).count != ct {
                    return false
                }
                ct
            };
            for i in 0..ct {
                let x = nth::nth(prism, i)[0];
                let y = nth::nth(l_prism, ct - 1 - i)[0];
                if x.handle() != y.handle() { return false }
            }
            return true
        }
        false
    }
    fn cmp(&self, prism: AnchoredLine, other: Unit) -> Option<Ordering> {
        let o = other.handle();
        if let Some(v_prism) = find_prism(o) {
            let ct = Guide::hydrate(prism).count;
            let v_ct = Guide::hydrate(v_prism).count;
            let cmp_ct = ct.min(v_ct);
            for i in 0..cmp_ct {
                let x = nth::nth(prism, i)[0];
                let y = nth::nth(v_prism, i)[0];
                let res = x.handle().cmp(y.handle());
                match res {
                    Some(Ordering::Equal) => { },
                    _ => { return res },
                }
            }
            return Some(ct.cmp(&v_ct))
        }
        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 Vector_ {
    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 {
        //group!("Vector conj");
        let res = conj::conj(prism, x);
        //group_end!();
        res
    }
    fn peek(&self, prism: AnchoredLine) -> *const Unit {
        let guide = Guide::hydrate(prism);
        self.nth(prism, guide.count - 1)
    }
    fn pop(&self, prism: AnchoredLine) -> (Unit, Unit) { pop::pop(prism) }
    fn reduce(&self, prism: AnchoredLine, process: &mut [Box<dyn Process>]) -> Value {
        reduce::reduce(prism, process)
    }
}
impl Sequential for Vector_ {
    fn is_sequential(&self, prism: AnchoredLine) -> bool { true }
    fn nth(&self, prism: AnchoredLine, idx: u32) -> *const Unit {
        nth::nth(prism, idx).line().star()
    }
    fn nth_set(&self, prism: AnchoredLine, idx: u32, v: Unit) -> Unit {
        let (c, displaced) = assoc::assoc(prism, idx, v);
        displaced.handle().retire();
        c
    }
    fn swap_idx(&self, prism: AnchoredLine, i: u32, j: u32) -> Unit {
        assoc::swap(prism, i, j)
    }
}

impl Associative for Vector_ {
    fn assoc(&self, prism: AnchoredLine, k: Unit, v: Unit) -> (Unit, Unit) {
        let idx = k.handle().as_i64();
        if idx < 0 || idx > u32::MAX as i64 {
            panic!("Bad index: {} in vector", idx);
        }
        k.handle().retire();
        assoc::assoc(prism, idx as u32, v)
    }
}
impl Reversible for Vector_ {}
impl Sorted for Vector_ {}
impl Notation for Vector_ {
    fn edn(&self, prism: AnchoredLine, f: &mut fmt::Formatter) -> fmt::Result {
        // conversion to and from &Formatter
        // factor out Printer parts
        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 _ = reduce::reduce(prism, &mut procs);
        write!(f, "]")
    }
}
impl Numeral for Vector_ {}
impl Callable for Vector_ {}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn index() {
        let v: Value = "[1 2 3 4]".parse().unwrap();
        assert_eq!(v[2], 3.into());
        assert_eq!(v[6], 3.into());
        assert_eq!(v[-2], 3.into());
        assert_eq!(v[-6], 3.into());
    }
}