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
// 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.

use super::*;
use super::conj::*;

// Pop is the inverse operation of conj,
// and naturally contains similar logic around the shape of the tree
pub fn pop(prism: AnchoredLine) -> (Unit, Unit) {
    let guide = Guide::hydrate(unaliased(prism));
    if guide.count <= TAIL_CAP {
        pop_untailed(guide)
    } else {
        pop_tailed(guide)
    }
}

pub fn pop_untailed(guide: Guide) -> (Unit, Unit) {
    if guide.count == 0 {
        (guide.segment().unit(), Unit::from(0))
    } else {
        let popped = guide.root[(guide.count - 1) as i32];
        (guide.dec_count().store().segment().unit(), popped)
    }
}

pub fn pop_tailed(guide: Guide) -> (Unit, Unit) {
    let tail_count = tail_count(guide.count);
    if tail_count != 1 {
        let tail = {
            let tail = guide.root[-1].segment();
            if tail.is_aliased() {
                let s = Segment::new(TAIL_CAP);
                let tails = tail.at(0..tail_count);
                tails.to(s);
                tails.split();
                if tail.unalias() == 0 {
                    tails.retire();
                    Segment::free(tail);
                }
                guide.root.set(-1, s.unit());
                s
            } else {
                tail
            }
        };
        let popped = tail[tail_count - 1];
        (guide.dec_count().store().segment().unit(), popped)
    } else {
        pop_tailed_drained(guide)
    }
}

pub fn pop_tailed_drained(guide: Guide) -> (Unit, Unit) {
    let tail = guide.root[-1].segment();
    let popped = tail[0];
    if tail.is_aliased() {
        popped.handle().split();
        if tail.unalias() == 0 {
            popped.handle().retire();
            Segment::free(tail);
        }
    } else {
        tail.unalias();
        Segment::free(tail);
    }
    let tailoff = tailoff(guide.count);
    let tail_path = tailoff - TAIL_CAP;
    if tail_path == 0 {
        (guide.dec_count().store().segment().unit(), popped)
    } else {
        let last_index = tail_path - 1;
        let path_diff = tail_path ^ last_index;
        let ret = match digit_count(last_index).cmp(&digit_count(path_diff)) {
            Ordering::Less    => { shrink_height(guide, tailoff) },
            Ordering::Equal   => { shrink_root(guide, tailoff) },
            Ordering::Greater => { shrink_child(guide, tailoff) },
        };
        (ret, popped)
    }
}

pub fn unlink_tail(mut s: Segment, height: u32) -> Segment {
    for i in 0..height {
        if !s.is_aliased() {
            let t = s[0].segment();
            s.unalias();
            Segment::free(s);
            s = t;
        } else {
            return unlink_tail_aliased(s, height - i);
        }
    }
    s
}

pub fn unlink_tail_aliased(mut s: Segment, height: u32) -> Segment {
    let tail = {
        let mut t = s;
        for _ in 0..height {
            t = t[0].segment();
        }
        t
    };
    tail.alias();
    for _ in 0..(height + 1) {
        if s.unalias() == 0 {
            let t = s[0].segment();
            Segment::free(s);
            s = t;
        } else {
            break
        }
    }
    tail
}

pub fn shrink_height(guide: Guide, tailoff: u32) -> Unit {
    let last_index = tailoff - 1;
    let tail_path_head = guide.root[1].segment();
    let path_height = trailing_zero_digit_count(last_index >> BITS);
    let tail = unlink_tail(tail_path_head, path_height);
    guide.root.set(-1, tail.unit());
    let g = if guide.root.has_index(TAIL_CAP as i32 - 1) {
        guide
    } else {
        let s = Segment::new(guide.root.index + TAIL_CAP);
        guide.segment().at(0..(guide.root.index + 1)).to(s);
        let mut g = guide;
        g.prism = guide.prism.with_seg(s);
        guide.segment().unalias();
        Segment::free(guide.segment());
        g.reroot()
    };
    let c = g.root[0].segment();

    c.at(0..TAIL_CAP).to_offset(g.segment(), g.root.index);
    if c.is_aliased() {
        let contents = c.at(0..TAIL_CAP);
        contents.split();
        if c.unalias() == 0 {
            contents.retire();
            Segment::free(c);
        }
    } else {
        c.unalias();
        Segment::free(c);
    }
    g.dec_count().store().segment().unit()
}

pub fn shrink_root(guide: Guide, tailoff: u32) -> Unit {
    let last_index = tailoff - 1;
    let root_count = root_content_count(tailoff);
    let tail_path_head = guide.root[(root_count - 1) as i32].segment();
    let path_height = trailing_zero_digit_count(last_index >> BITS);
    let tail = unlink_tail(tail_path_head, path_height);
    guide.root.set(-1, tail.unit());
    guide.dec_count().store().segment().unit()
}

pub fn unalias_edge_path_pop(mut curr: AnchoredLine, d: &mut Digits) -> AnchoredLine {
    let count = d.count as u32;
    for _i in 0..count {
        let s = curr[0].segment();
        let digit = d.pop();
        if !s.is_aliased() {
            curr = s.line_at(digit);
        } else {
            let t = {
                let t = Segment::new(size(digit + 1));
                let range = s.at(0..(digit + 1));
                range.to(t);
                range.alias();
                if s.unalias() == 0 {
                    range.unalias();
                    Segment::free(s);
                }
                t
            };
            curr.set(0, t.unit());
            curr = t.line_at(digit);
        }
    }
    curr
}

pub fn shrink_child(guide: Guide, tailoff: u32) -> Unit {
    let last_index = tailoff - 1;
    let zero_count = trailing_zero_digit_count(last_index >> BITS);
    let digit_count = digit_count(last_index);
    let c = {
        let mut d = Digits::new(last_index, digit_count, digit_count - zero_count - 1);
        unalias_edge_path_pop(guide.root.offset(d.pop() as i32), &mut d)
    };
    let tail = unlink_tail(c[0].segment(), zero_count);
    guide.root.set(-1, tail.unit());
    guide.dec_count().store().segment().unit()
}