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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
|
// Package vector implements persistent vector.
package vector
import (
"bytes"
"encoding/json"
"fmt"
)
const (
chunkBits = 5
nodeSize = 1 << chunkBits
tailMaxLen = nodeSize
chunkMask = nodeSize - 1
)
// Vector is a persistent sequential container for arbitrary values. It supports
// O(1) lookup by index, modification by index, and insertion and removal
// operations at the end. Being a persistent variant of the data structure, it
// is immutable, and provides O(1) operations to create modified versions of the
// vector that shares the underlying data structure, making it suitable for
// concurrent access. The empty value is a valid empty vector.
type Vector interface {
json.Marshaler
// Len returns the length of the vector.
Len() int
// Index returns the i-th element of the vector, if it exists. The second
// return value indicates whether the element exists.
Index(i int) (interface{}, bool)
// Assoc returns an almost identical Vector, with the i-th element
// replaced. If the index is smaller than 0 or greater than the length of
// the vector, it returns nil. If the index is equal to the size of the
// vector, it is equivalent to Cons.
Assoc(i int, val interface{}) Vector
// Cons returns an almost identical Vector, with an additional element
// appended to the end.
Cons(val interface{}) Vector
// Pop returns an almost identical Vector, with the last element removed. It
// returns nil if the vector is already empty.
Pop() Vector
// SubVector returns a subvector containing the elements from i up to but
// not including j.
SubVector(i, j int) Vector
// Iterator returns an iterator over the vector.
Iterator() Iterator
}
// Iterator is an iterator over vector elements. It can be used like this:
//
// for it := v.Iterator(); it.HasElem(); it.Next() {
// elem := it.Elem()
// // do something with elem...
// }
type Iterator interface {
// Elem returns the element at the current position.
Elem() interface{}
// HasElem returns whether the iterator is pointing to an element.
HasElem() bool
// Next moves the iterator to the next position.
Next()
}
type vector struct {
count int
// height of the tree structure, defined to be 0 when root is a leaf.
height uint
root node
tail []interface{}
}
// Empty is an empty Vector.
var Empty Vector = &vector{}
// node is a node in the vector tree. It is always of the size nodeSize.
type node *[nodeSize]interface{}
func newNode() node {
return node(&[nodeSize]interface{}{})
}
func clone(n node) node {
a := *n
return node(&a)
}
func nodeFromSlice(s []interface{}) node {
var n [nodeSize]interface{}
copy(n[:], s)
return &n
}
// Count returns the number of elements in a Vector.
func (v *vector) Len() int {
return v.count
}
// treeSize returns the number of elements stored in the tree (as opposed to the
// tail).
func (v *vector) treeSize() int {
if v.count < tailMaxLen {
return 0
}
return ((v.count - 1) >> chunkBits) << chunkBits
}
func (v *vector) Index(i int) (interface{}, bool) {
if i < 0 || i >= v.count {
return nil, false
}
// The following is very similar to sliceFor, but is implemented separately
// to avoid unncessary copying.
if i >= v.treeSize() {
return v.tail[i&chunkMask], true
}
n := v.root
for shift := v.height * chunkBits; shift > 0; shift -= chunkBits {
n = n[(i>>shift)&chunkMask].(node)
}
return n[i&chunkMask], true
}
// sliceFor returns the slice where the i-th element is stored. The index must
// be in bound.
func (v *vector) sliceFor(i int) []interface{} {
if i >= v.treeSize() {
return v.tail
}
n := v.root
for shift := v.height * chunkBits; shift > 0; shift -= chunkBits {
n = n[(i>>shift)&chunkMask].(node)
}
return n[:]
}
func (v *vector) Assoc(i int, val interface{}) Vector {
if i < 0 || i > v.count {
return nil
} else if i == v.count {
return v.Cons(val)
}
if i >= v.treeSize() {
newTail := append([]interface{}(nil), v.tail...)
newTail[i&chunkMask] = val
return &vector{v.count, v.height, v.root, newTail}
}
return &vector{v.count, v.height, doAssoc(v.height, v.root, i, val), v.tail}
}
// doAssoc returns an almost identical tree, with the i-th element replaced by
// val.
func doAssoc(height uint, n node, i int, val interface{}) node {
m := clone(n)
if height == 0 {
m[i&chunkMask] = val
} else {
sub := (i >> (height * chunkBits)) & chunkMask
m[sub] = doAssoc(height-1, m[sub].(node), i, val)
}
return m
}
func (v *vector) Cons(val interface{}) Vector {
// Room in tail?
if v.count-v.treeSize() < tailMaxLen {
newTail := make([]interface{}, len(v.tail)+1)
copy(newTail, v.tail)
newTail[len(v.tail)] = val
return &vector{v.count + 1, v.height, v.root, newTail}
}
// Full tail; push into tree.
tailNode := nodeFromSlice(v.tail)
newHeight := v.height
var newRoot node
// Overflow root?
if (v.count >> chunkBits) > (1 << (v.height * chunkBits)) {
newRoot = newNode()
newRoot[0] = v.root
newRoot[1] = newPath(v.height, tailNode)
newHeight++
} else {
newRoot = v.pushTail(v.height, v.root, tailNode)
}
return &vector{v.count + 1, newHeight, newRoot, []interface{}{val}}
}
// pushTail returns a tree with tail appended.
func (v *vector) pushTail(height uint, n node, tail node) node {
if height == 0 {
return tail
}
idx := ((v.count - 1) >> (height * chunkBits)) & chunkMask
m := clone(n)
child := n[idx]
if child == nil {
m[idx] = newPath(height-1, tail)
} else {
m[idx] = v.pushTail(height-1, child.(node), tail)
}
return m
}
// newPath creates a left-branching tree of specified height and leaf.
func newPath(height uint, leaf node) node {
if height == 0 {
return leaf
}
ret := newNode()
ret[0] = newPath(height-1, leaf)
return ret
}
func (v *vector) Pop() Vector {
switch v.count {
case 0:
return nil
case 1:
return Empty
}
if v.count-v.treeSize() > 1 {
newTail := make([]interface{}, len(v.tail)-1)
copy(newTail, v.tail)
return &vector{v.count - 1, v.height, v.root, newTail}
}
newTail := v.sliceFor(v.count - 2)
newRoot := v.popTail(v.height, v.root)
newHeight := v.height
if v.height > 0 && newRoot[1] == nil {
newRoot = newRoot[0].(node)
newHeight--
}
return &vector{v.count - 1, newHeight, newRoot, newTail}
}
// popTail returns a new tree with the last leaf removed.
func (v *vector) popTail(level uint, n node) node {
idx := ((v.count - 2) >> (level * chunkBits)) & chunkMask
if level > 1 {
newChild := v.popTail(level-1, n[idx].(node))
if newChild == nil && idx == 0 {
return nil
}
m := clone(n)
if newChild == nil {
// This is needed since `m[idx] = newChild` would store an
// interface{} with a non-nil type part, which is non-nil
m[idx] = nil
} else {
m[idx] = newChild
}
return m
} else if idx == 0 {
return nil
} else {
m := clone(n)
m[idx] = nil
return m
}
}
func (v *vector) SubVector(begin, end int) Vector {
if begin < 0 || begin > end || end > v.count {
return nil
}
return &subVector{v, begin, end}
}
func (v *vector) Iterator() Iterator {
return newIterator(v)
}
func (v *vector) MarshalJSON() ([]byte, error) {
return marshalJSON(v.Iterator())
}
type subVector struct {
v *vector
begin int
end int
}
func (s *subVector) Len() int {
return s.end - s.begin
}
func (s *subVector) Index(i int) (interface{}, bool) {
if i < 0 || s.begin+i >= s.end {
return nil, false
}
return s.v.Index(s.begin + i)
}
func (s *subVector) Assoc(i int, val interface{}) Vector {
if i < 0 || s.begin+i > s.end {
return nil
} else if s.begin+i == s.end {
return s.Cons(val)
}
return s.v.Assoc(s.begin+i, val).SubVector(s.begin, s.end)
}
func (s *subVector) Cons(val interface{}) Vector {
return s.v.Assoc(s.end, val).SubVector(s.begin, s.end+1)
}
func (s *subVector) Pop() Vector {
switch s.Len() {
case 0:
return nil
case 1:
return Empty
default:
return s.v.SubVector(s.begin, s.end-1)
}
}
func (s *subVector) SubVector(i, j int) Vector {
return s.v.SubVector(s.begin+i, s.begin+j)
}
func (s *subVector) Iterator() Iterator {
return newIteratorWithRange(s.v, s.begin, s.end)
}
func (s *subVector) MarshalJSON() ([]byte, error) {
return marshalJSON(s.Iterator())
}
type iterator struct {
v *vector
treeSize int
index int
end int
path []pathEntry
}
type pathEntry struct {
node node
index int
}
func (e pathEntry) current() interface{} {
return e.node[e.index]
}
func newIterator(v *vector) *iterator {
return newIteratorWithRange(v, 0, v.Len())
}
func newIteratorWithRange(v *vector, begin, end int) *iterator {
it := &iterator{v, v.treeSize(), begin, end, nil}
// Find the node for begin, remembering all nodes along the path.
n := v.root
for shift := v.height * chunkBits; shift > 0; shift -= chunkBits {
idx := (begin >> shift) & chunkMask
it.path = append(it.path, pathEntry{n, idx})
n = n[idx].(node)
}
it.path = append(it.path, pathEntry{n, begin & chunkMask})
return it
}
func (it *iterator) Elem() interface{} {
if it.index >= it.treeSize {
return it.v.tail[it.index-it.treeSize]
}
return it.path[len(it.path)-1].current()
}
func (it *iterator) HasElem() bool {
return it.index < it.end
}
func (it *iterator) Next() {
if it.index+1 >= it.treeSize {
// Next element is in tail. Just increment the index.
it.index++
return
}
// Find the deepest level that can be advanced.
var i int
for i = len(it.path) - 1; i >= 0; i-- {
e := it.path[i]
if e.index+1 < len(e.node) {
break
}
}
if i == -1 {
panic("cannot advance; vector iterator bug")
}
// Advance on this node, and re-populate all deeper levels.
it.path[i].index++
for i++; i < len(it.path); i++ {
it.path[i] = pathEntry{it.path[i-1].current().(node), 0}
}
it.index++
}
type marshalError struct {
index int
cause error
}
func (err *marshalError) Error() string {
return fmt.Sprintf("element %d: %s", err.index, err.cause)
}
func marshalJSON(it Iterator) ([]byte, error) {
var buf bytes.Buffer
buf.WriteByte('[')
index := 0
for ; it.HasElem(); it.Next() {
if index > 0 {
buf.WriteByte(',')
}
elemBytes, err := json.Marshal(it.Elem())
if err != nil {
return nil, &marshalError{index, err}
}
buf.Write(elemBytes)
index++
}
buf.WriteByte(']')
return buf.Bytes(), nil
}
|