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]
fn new<S: AsRef<str>>(name: S, view: T) -> Self
[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);
fn with_prefix<S: AsRef<str>>(name: S, prefix: Vec<u8>, view: T) -> Self
[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]
T: AsRef<Snapshot>,
V: StorageValue,
fn get(&self, index: u64) -> Option<V>
[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));
fn is_empty(&self) -> bool
[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());
fn capacity(&self) -> u64
[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());
fn len(&self) -> u64
[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());
fn iter(&self) -> SparseListIndexIter<V>
[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); }
fn indices(&self) -> SparceListIndexKeys
[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); }
fn values(&self) -> SparceListIndexValues<V>
[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); }
fn iter_from(&self, from: u64) -> SparseListIndexIter<V>
[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]
V: StorageValue,
fn push(&mut self, value: V)
[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());
fn remove(&mut self, index: u64) -> Option<V>
[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());
fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = V>,
[src]
I: IntoIterator<Item = V>,
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());
fn set(&mut self, index: u64, value: V) -> Option<V>
[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));
fn clear(&mut self)
[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());
fn pop(&mut self) -> Option<V>
[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]
impl<'a, T, V> IntoIterator for &'a SparseListIndex<T, V> where
T: AsRef<Snapshot>,
V: StorageValue,
[src]
T: AsRef<Snapshot>,
V: StorageValue,