Skip to content

ImmutableTree

ratranqu edited this page Apr 7, 2021 · 3 revisions

ImmutableTree

The ImmutableTree implementation aims to mimic the equivalent Go ImmutableTree. However, the design we implement ere is such that we have encapsulated the storage and access to the nodes into the NodeStorageProtocol. Therefore, any loading, saving, deleting, managing of cache, memory and such is expected to be handled and delegated to the storage implementation itself. We are providing this struct because as part of the design process, it was implemented before eventually being carved out in favour of the NodeStorageProtocol and the NodeProtocol. As it may be useful as part of the coming development of future CosmosSwift milestones, we keep it as suc for now, albeit as part of the Legacy module. This tree is not thread safe.

public struct ImmutableTree<Storage: NodeStorageProtocol>: CustomStringConvertible

Inheritance

CustomStringConvertible

Nested Type Aliases

Hash

public typealias Hash = Storage.Hasher.Hash

Node

public typealias Node = Storage.Node

Key

public typealias Key = Storage.Key

Value

public typealias Value = Storage.Value

Initializers

init(_:)

We implement here a simple initializer taking a storage instance. The specific strategies around caching, pruning, managing memory and speed of access are intended to be implemented by the type of storage provided dependin on the specific use case.

public init(_ storage: Storage) throws

Properties

root

var root: Node

version

var version: Int64

size

var size: Int64

height

var height: Int8

hash

var hash: Hash

description

var description: String

Methods

next(key:)

public func next(key: Key) -> Key?

has(_:)

public func has(_ key: Key) -> Bool

get(_:)

public func get(_ key: Key) -> (index: Int64, value: Value?)

getByIndex(_:)

public func getByIndex(_ index: Int64) -> (key: Key, value: Value)?

iterate(_:)

public func iterate(_ calling: (Key, Value) -> Bool) -> Bool

iterateRange(_:_:_:_:)

public func iterateRange(_ start: Key, _ end: Key, _ ascending: Bool, _ calling: (Key, Value, Int64) -> Bool) -> Bool

nodeSize()

public func nodeSize() -> Int

getRangeWithProof(_:_:_:)

public func getRangeWithProof(_ start: Storage.Key?, _ end: Storage.Key?, _ limit: UInt) throws -> (keys: [Storage.Key], value: [Storage.Value], proof: RangeProof<Storage.Node>)

getWithProof(_:)

public func getWithProof(_ key: Storage.Key) throws -> (value: Storage.Value?, proof: RangeProof<Storage.Node>)