Struct exonum::storage::sparse_list_index::SparseListIndex [] [src]

pub struct SparseListIndex<T, V> { /* fields omitted */ }

The list of items is similar to the ListIndex, but it may contain "spaces". For instance, list might contain six elements with indexes: "1, 2, 3, 5, 7, 8" (missing 4 and 6). And if you try to get element for index 4 or 6 you'll get None.

SparseListIndex implements an array list, storing the element as values and using u64 as an index. SparseListIndex requires that the elements implement the StorageValue trait.

Methods

impl<T, V> SparseListIndex<T, V>
[src]

[src]

Creates a new index representation based on the name and storage view.

Storage view can be specified as &Snapshot or &mut Fork. In the first case only immutable methods are available. In the second case both immutable and mutable methods are available.

Examples

use exonum::storage::{MemoryDB, Database, SparseListIndex};

let db = MemoryDB::new();
let snapshot = db.snapshot();
let name = "name";
let index: SparseListIndex<_, u8> = SparseListIndex::new(name, &snapshot);

[src]

Creates a new index representation based on the name, common prefix of its keys and storage view.

Storage view can be specified as &Snapshot or &mut Fork. In the first case only immutable methods are available. In the second case both immutable and mutable methods are available.

Examples

use exonum::storage::{MemoryDB, Database, SparseListIndex};

let db = MemoryDB::new();
let snapshot = db.snapshot();
let name = "name";
let prefix = vec![123];
let index: SparseListIndex<_, u8> = SparseListIndex::with_prefix(name, prefix, &snapshot);

impl<T, V> SparseListIndex<T, V> where
    T: AsRef<Snapshot>,
    V: StorageValue
[src]

[src]

Returns an element at that position or None if out of bounds or it does not exist.

Examples

use exonum::storage::{MemoryDB, Database, SparseListIndex};

let db = MemoryDB::new();
let mut fork = db.fork();
let mut index = SparseListIndex::new("name", &mut fork);
assert_eq!(None, index.get(0));

index.push(42);
assert_eq!(Some(42), index.get(0));
index.push(1);
index.remove(0);
assert_eq!(None, index.get(0));
assert_eq!(Some(1), index.get(1));

[src]

Returns true if the list contains no elements.

Examples

use exonum::storage::{MemoryDB, Database, SparseListIndex};

let db = MemoryDB::new();
let mut fork = db.fork();
let mut index = SparseListIndex::new("name", &mut fork);
assert!(index.is_empty());

index.push(42);
assert!(!index.is_empty());

[src]

Returns the total amount of elements (including "empty" elements) in the list. The value of capacity is determined by the maximum index of the element ever inserted into the index.

Examples

use exonum::storage::{MemoryDB, Database, SparseListIndex};

let db = MemoryDB::new();
let mut fork = db.fork();
let mut index = SparseListIndex::new("name", &mut fork);
assert_eq!(0, index.capacity());

index.push(10);
index.push(12);
assert_eq!(2, index.capacity());

index.remove(0);

index.push(100);
assert_eq!(3, index.capacity());

[src]

Returns the total amount of non-empty elements in the list.

Examples

use exonum::storage::{MemoryDB, Database, SparseListIndex};

let db = MemoryDB::new();
let mut fork = db.fork();
let mut index = SparseListIndex::new("name", &mut fork);
assert_eq!(0, index.len());

index.push(10);
assert_eq!(1, index.len());

index.remove(0);

index.push(100);
assert_eq!(1, index.len());

[src]

Returns an iterator over the list. The iterator element type is (u64, V).

Examples

use exonum::storage::{MemoryDB, Database, SparseListIndex};

let db = MemoryDB::new();
let mut fork = db.fork();
let mut index = SparseListIndex::new("name", &mut fork);

index.extend([1, 2, 3, 4, 5].iter().cloned());

for val in index.iter() {
    println!("{:?}", val);
}

[src]

Returns an iterator over the indices of the 'SparceListIndex'.

Examples

use exonum::storage::{MemoryDB, Database, SparseListIndex};

let db = MemoryDB::new();
let mut fork = db.fork();
let mut index = SparseListIndex::new("name", &mut fork);

index.extend([1, 2, 3, 4, 5].iter().cloned());

for val in index.indices() {
    println!("{}", val);
}

[src]

Returns an iterator over the values of the 'SparceListIndex'. The iterator element type is V.

Examples

use exonum::storage::{MemoryDB, Database, SparseListIndex};

let db = MemoryDB::new();
let mut fork = db.fork();
let mut index = SparseListIndex::new("name", &mut fork);

index.extend([1, 2, 3, 4, 5].iter().cloned());

for val in index.values() {
    println!("{}", val);
}

[src]

Returns an iterator over the list starting from the specified position. The iterator element type is (u64, V).

Examples

use exonum::storage::{MemoryDB, Database, SparseListIndex};

let db = MemoryDB::new();
let mut fork = db.fork();
let mut index = SparseListIndex::new("name", &mut fork);

index.extend([1, 2, 3, 4, 5].iter().cloned());
index.remove(3);

for val in index.iter_from(3) {
    println!("{:?}", val);
}

impl<'a, V> SparseListIndex<&'a mut Fork, V> where
    V: StorageValue
[src]

[src]

Appends an element to the back of the 'SparceListIndex'.

Examples

use exonum::storage::{MemoryDB, Database, SparseListIndex};

let db = MemoryDB::new();
let mut fork = db.fork();
let mut index = SparseListIndex::new("name", &mut fork);

index.push(1);
assert!(!index.is_empty());

[src]

Removes the element with the given index from the list and returns it, or None if it is empty.

Examples

use exonum::storage::{MemoryDB, Database, SparseListIndex};

let db = MemoryDB::new();
let mut fork = db.fork();
let mut index = SparseListIndex::new("name", &mut fork);
assert_eq!(0, index.capacity());

index.push(10);
index.push(12);

assert_eq!(Some(10), index.remove(0));
assert_eq!(None, index.remove(0));
assert_eq!(2, index.capacity());
assert_eq!(1, index.len());
assert_eq!(Some(12), index.remove(1));
assert_eq!(2, index.capacity());

[src]

Extends the list with the contents of an iterator.

Examples

use exonum::storage::{MemoryDB, Database, SparseListIndex};

let db = MemoryDB::new();
let mut fork = db.fork();
let mut index = SparseListIndex::new("name", &mut fork);
assert!(index.is_empty());

index.extend([1, 2, 3].iter().cloned());
assert_eq!(3, index.capacity());

[src]

Changes a value at the specified position. If the position contains empty value it also increments elements count. If a value of the index of new element is greater than current capacity, the capacity of the list considered index + 1 and all further elements without specific index value will be appended after this index.

Returns the value of an old element at this position or None if it was empty.

Examples

use exonum::storage::{MemoryDB, Database, SparseListIndex};

let db = MemoryDB::new();
let mut fork = db.fork();
let mut index = SparseListIndex::new("name", &mut fork);

index.push(1);
assert_eq!(Some(1), index.get(0));

index.set(0, 10);
assert_eq!(Some(10), index.get(0));

[src]

Clears the list, removing all values.

Notes

Currently this method is not optimized to delete a large set of data. During the execution of this method the amount of allocated memory is linearly dependent on the number of elements in the index.

Examples

use exonum::storage::{MemoryDB, Database, SparseListIndex};

let db = MemoryDB::new();
let mut fork = db.fork();
let mut index = SparseListIndex::new("name", &mut fork);

index.push(1);
assert!(!index.is_empty());

index.clear();
assert!(index.is_empty());

[src]

Removes the first element from the 'SparceListIndex' and returns it, or None if it is empty.

Examples

use exonum::storage::{MemoryDB, Database, SparseListIndex};

let db = MemoryDB::new();
let mut fork = db.fork();
let mut index = SparseListIndex::new("name", &mut fork);
assert_eq!(None, index.pop());

index.push(1);
assert_eq!(Some(1), index.pop());

Trait Implementations

impl<T: Debug, V: Debug> Debug for SparseListIndex<T, V>
[src]

[src]

Formats the value using the given formatter.

impl<'a, T, V> IntoIterator for &'a SparseListIndex<T, V> where
    T: AsRef<Snapshot>,
    V: StorageValue
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

[src]

Creates an iterator from a value. Read more