github.com/lbryio/lbcd@v0.22.119/database/internal/treap/doc.go (about) 1 // Copyright (c) 2015-2016 The btcsuite developers 2 // Use of this source code is governed by an ISC 3 // license that can be found in the LICENSE file. 4 5 /* 6 Package treap implements a treap data structure that is used to hold ordered 7 key/value pairs using a combination of binary search tree and heap semantics. 8 It is a self-organizing and randomized data structure that doesn't require 9 complex operations to to maintain balance. Search, insert, and delete 10 operations are all O(log n). Both mutable and immutable variants are provided. 11 12 The mutable variant is typically faster since it is able to simply update the 13 treap when modifications are made. However, a mutable treap is not safe for 14 concurrent access without careful use of locking by the caller and care must be 15 taken when iterating since it can change out from under the iterator. 16 17 The immutable variant works by creating a new version of the treap for all 18 mutations by replacing modified nodes with new nodes that have updated values 19 while sharing all unmodified nodes with the previous version. This is extremely 20 useful in concurrent applications since the caller only has to atomically 21 replace the treap pointer with the newly returned version after performing any 22 mutations. All readers can simply use their existing pointer as a snapshot 23 since the treap it points to is immutable. This effectively provides O(1) 24 snapshot capability with efficient memory usage characteristics since the old 25 nodes only remain allocated until there are no longer any references to them. 26 */ 27 package treap