Merge branch 'master' into fo-6418-dont-export-bigint
# Conflicts: # dapps/src/tests/helpers/registrar.rs # ethcore/evm/src/interpreter/shared_cache.rs # ethcore/light/src/client/header_chain.rs # ethcore/light/src/client/mod.rs # ethcore/light/src/net/mod.rs # ethcore/light/src/on_demand/request.rs # ethcore/light/src/on_demand/tests.rs # ethcore/light/src/provider.rs # ethcore/node_filter/src/lib.rs # ethcore/src/block.rs # ethcore/src/blockchain/blockchain.rs # ethcore/src/client/test_client.rs # ethcore/src/engines/authority_round/mod.rs # ethcore/src/engines/basic_authority.rs # ethcore/src/engines/mod.rs # ethcore/src/engines/tendermint/mod.rs # ethcore/src/engines/validator_set/contract.rs # ethcore/src/engines/validator_set/multi.rs # ethcore/src/engines/validator_set/safe_contract.rs # ethcore/src/engines/vote_collector.rs # ethcore/src/miner/external.rs # ethcore/src/miner/miner.rs # ethcore/src/miner/service_transaction_checker.rs # ethcore/src/miner/work_notify.rs # ethcore/src/pod_account.rs # ethcore/src/pod_state.rs # ethcore/src/snapshot/block.rs # ethcore/src/snapshot/consensus/work.rs # ethcore/src/snapshot/mod.rs # ethcore/src/snapshot/service.rs # ethcore/src/spec/spec.rs # ethcore/src/state/backend.rs # ethcore/src/trace/db.rs # ethcore/src/verification/queue/mod.rs # ethcore/src/verification/verification.rs # parity/informant.rs # rpc/src/v1/helpers/dispatch.rs # rpc/src/v1/helpers/light_fetch.rs # rpc/src/v1/helpers/signing_queue.rs # rpc/src/v1/impls/eth.rs # rpc/src/v1/impls/eth_filter.rs # rpc/src/v1/impls/eth_pubsub.rs # rpc/src/v1/impls/light/eth.rs # rpc/src/v1/impls/signing.rs # rpc/src/v1/tests/helpers/miner_service.rs # rpc/src/v1/tests/helpers/snapshot_service.rs # rpc/src/v1/tests/helpers/sync_provider.rs # rpc/src/v1/tests/mocked/eth.rs # stratum/src/lib.rs # sync/src/blocks.rs # sync/src/chain.rs # sync/src/light_sync/mod.rs # sync/src/tests/helpers.rs # sync/src/tests/snapshot.rs # updater/src/updater.rs # util/src/lib.rs # util/triehash/src/lib.rs
This commit is contained in:
@@ -117,38 +117,27 @@ pub mod common;
|
||||
pub mod error;
|
||||
pub mod bytes;
|
||||
pub mod misc;
|
||||
pub mod vector;
|
||||
pub mod hashdb;
|
||||
pub mod memorydb;
|
||||
pub mod migration;
|
||||
pub mod overlaydb;
|
||||
pub mod journaldb;
|
||||
pub mod kvdb;
|
||||
pub mod triehash;
|
||||
pub mod trie;
|
||||
pub mod nibbleslice;
|
||||
pub mod nibblevec;
|
||||
pub mod semantic_version;
|
||||
pub mod snappy;
|
||||
pub mod cache;
|
||||
mod timer;
|
||||
|
||||
pub use misc::*;
|
||||
pub use hashdb::*;
|
||||
pub use memorydb::MemoryDB;
|
||||
pub use overlaydb::*;
|
||||
pub use journaldb::JournalDB;
|
||||
pub use triehash::*;
|
||||
pub use trie::{Trie, TrieMut, TrieDB, TrieDBMut, TrieFactory, TrieError, SecTrieDB, SecTrieDBMut};
|
||||
pub use semantic_version::*;
|
||||
pub use kvdb::*;
|
||||
pub use timer::*;
|
||||
pub use error::*;
|
||||
pub use bytes::*;
|
||||
pub use vector::*;
|
||||
|
||||
pub use ansi_term::{Colour, Style};
|
||||
pub use parking_lot::{Condvar, Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||
|
||||
/// 160-bit integer representing account address
|
||||
pub type Address = bigint::hash::H160;
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity.
|
||||
|
||||
// Parity is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Semantic version formatting and comparing.
|
||||
|
||||
/// A version value with strict meaning. Use `as_u32` to convert to a simple integer.
|
||||
///
|
||||
/// # Example
|
||||
/// ```
|
||||
/// extern crate ethcore_util as util;
|
||||
/// use util::semantic_version::*;
|
||||
///
|
||||
/// fn main() {
|
||||
/// assert_eq!(SemanticVersion::new(1, 2, 3).as_u32(), 0x010203);
|
||||
/// }
|
||||
/// ```
|
||||
pub struct SemanticVersion {
|
||||
/// Major version - API/feature removals & breaking changes.
|
||||
pub major: u8,
|
||||
/// Minor version - API/feature additions.
|
||||
pub minor: u8,
|
||||
/// Tiny version - bug fixes.
|
||||
pub tiny: u8,
|
||||
}
|
||||
|
||||
impl SemanticVersion {
|
||||
/// Create a new object.
|
||||
pub fn new(major: u8, minor: u8, tiny: u8) -> SemanticVersion { SemanticVersion{major: major, minor: minor, tiny: tiny} }
|
||||
|
||||
/// Convert to a `u32` representation.
|
||||
pub fn as_u32(&self) -> u32 { ((self.major as u32) << 16) + ((self.minor as u32) << 8) + self.tiny as u32 }
|
||||
}
|
||||
|
||||
// TODO: implement Eq, Comparison and Debug/Display for SemanticVersion.
|
||||
@@ -1,51 +0,0 @@
|
||||
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity.
|
||||
|
||||
// Parity is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Performance timer with logging
|
||||
use time::precise_time_ns;
|
||||
|
||||
/// Performance timer with logging. Starts measuring time in the constructor, prints
|
||||
/// elapsed time in the destructor or when `stop` is called.
|
||||
pub struct PerfTimer {
|
||||
name: &'static str,
|
||||
start: u64,
|
||||
stopped: bool,
|
||||
}
|
||||
|
||||
impl PerfTimer {
|
||||
/// Create an instance with given name.
|
||||
pub fn new(name: &'static str) -> PerfTimer {
|
||||
PerfTimer {
|
||||
name: name,
|
||||
start: precise_time_ns(),
|
||||
stopped: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Stop the timer and print elapsed time on trace level with `perf` target.
|
||||
pub fn stop(&mut self) {
|
||||
if !self.stopped {
|
||||
trace!(target: "perf", "{}: {:.2}ms", self.name, (precise_time_ns() - self.start) as f32 / 1000_000.0);
|
||||
self.stopped = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for PerfTimer {
|
||||
fn drop(&mut self) {
|
||||
self.stop()
|
||||
}
|
||||
}
|
||||
@@ -939,7 +939,8 @@ impl<'a> Drop for TrieDBMut<'a> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use triehash::trie_root;
|
||||
extern crate triehash;
|
||||
use self::triehash::trie_root;
|
||||
use hashdb::*;
|
||||
use memorydb::*;
|
||||
use super::*;
|
||||
|
||||
@@ -1,354 +0,0 @@
|
||||
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity.
|
||||
|
||||
// Parity is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Generetes trie root.
|
||||
//!
|
||||
//! This module should be used to generate trie root hash.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::cmp;
|
||||
use bigint::hash::*;
|
||||
use keccak::keccak;
|
||||
use rlp;
|
||||
use rlp::RlpStream;
|
||||
use vector::SharedPrefix;
|
||||
|
||||
/// Generates a trie root hash for a vector of values
|
||||
///
|
||||
/// ```rust
|
||||
/// extern crate ethcore_util as util;
|
||||
/// extern crate ethcore_bigint as bigint;
|
||||
/// use std::str::FromStr;
|
||||
/// use util::triehash::*;
|
||||
/// use bigint::hash::*;
|
||||
///
|
||||
/// fn main() {
|
||||
/// let v = vec![From::from("doe"), From::from("reindeer")];
|
||||
/// let root = "e766d5d51b89dc39d981b41bda63248d7abce4f0225eefd023792a540bcffee3";
|
||||
/// assert_eq!(ordered_trie_root(v), H256::from_str(root).unwrap());
|
||||
/// }
|
||||
/// ```
|
||||
pub fn ordered_trie_root<I>(input: I) -> H256
|
||||
where I: IntoIterator<Item=Vec<u8>>
|
||||
{
|
||||
let gen_input = input
|
||||
// first put elements into btree to sort them by nibbles
|
||||
// optimize it later
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.map(|(i, vec)| (rlp::encode(&i).into_vec(), vec))
|
||||
.collect::<BTreeMap<_, _>>()
|
||||
// then move them to a vector
|
||||
.into_iter()
|
||||
.map(|(k, v)| (as_nibbles(&k), v) )
|
||||
.collect();
|
||||
|
||||
gen_trie_root(gen_input)
|
||||
}
|
||||
|
||||
/// Generates a trie root hash for a vector of key-values
|
||||
///
|
||||
/// ```rust
|
||||
/// extern crate ethcore_util as util;
|
||||
/// extern crate ethcore_bigint as bigint;
|
||||
/// use std::str::FromStr;
|
||||
/// use util::triehash::*;
|
||||
/// use bigint::hash::*;
|
||||
///
|
||||
/// fn main() {
|
||||
/// let v = vec![
|
||||
/// (From::from("doe"), From::from("reindeer")),
|
||||
/// (From::from("dog"), From::from("puppy")),
|
||||
/// (From::from("dogglesworth"), From::from("cat")),
|
||||
/// ];
|
||||
///
|
||||
/// let root = "8aad789dff2f538bca5d8ea56e8abe10f4c7ba3a5dea95fea4cd6e7c3a1168d3";
|
||||
/// assert_eq!(trie_root(v), H256::from_str(root).unwrap());
|
||||
/// }
|
||||
/// ```
|
||||
pub fn trie_root<I>(input: I) -> H256
|
||||
where I: IntoIterator<Item=(Vec<u8>, Vec<u8>)>
|
||||
{
|
||||
let gen_input = input
|
||||
// first put elements into btree to sort them and to remove duplicates
|
||||
.into_iter()
|
||||
.collect::<BTreeMap<_, _>>()
|
||||
// then move them to a vector
|
||||
.into_iter()
|
||||
.map(|(k, v)| (as_nibbles(&k), v) )
|
||||
.collect();
|
||||
|
||||
gen_trie_root(gen_input)
|
||||
}
|
||||
|
||||
/// Generates a key-hashed (secure) trie root hash for a vector of key-values.
|
||||
///
|
||||
/// ```rust
|
||||
/// extern crate ethcore_util as util;
|
||||
/// extern crate ethcore_bigint as bigint;
|
||||
/// use std::str::FromStr;
|
||||
/// use util::triehash::*;
|
||||
/// use bigint::hash::*;
|
||||
///
|
||||
/// fn main() {
|
||||
/// let v = vec![
|
||||
/// (From::from("doe"), From::from("reindeer")),
|
||||
/// (From::from("dog"), From::from("puppy")),
|
||||
/// (From::from("dogglesworth"), From::from("cat")),
|
||||
/// ];
|
||||
///
|
||||
/// let root = "d4cd937e4a4368d7931a9cf51686b7e10abb3dce38a39000fd7902a092b64585";
|
||||
/// assert_eq!(sec_trie_root(v), H256::from_str(root).unwrap());
|
||||
/// }
|
||||
/// ```
|
||||
pub fn sec_trie_root(input: Vec<(Vec<u8>, Vec<u8>)>) -> H256 {
|
||||
let gen_input = input
|
||||
// first put elements into btree to sort them and to remove duplicates
|
||||
.into_iter()
|
||||
.map(|(k, v)| (keccak(k), v))
|
||||
.collect::<BTreeMap<_, _>>()
|
||||
// then move them to a vector
|
||||
.into_iter()
|
||||
.map(|(k, v)| (as_nibbles(&k), v) )
|
||||
.collect();
|
||||
|
||||
gen_trie_root(gen_input)
|
||||
}
|
||||
|
||||
fn gen_trie_root(input: Vec<(Vec<u8>, Vec<u8>)>) -> H256 {
|
||||
let mut stream = RlpStream::new();
|
||||
hash256rlp(&input, 0, &mut stream);
|
||||
keccak(stream.out())
|
||||
}
|
||||
|
||||
/// Hex-prefix Notation. First nibble has flags: oddness = 2^0 & termination = 2^1.
|
||||
///
|
||||
/// The "termination marker" and "leaf-node" specifier are completely equivalent.
|
||||
///
|
||||
/// Input values are in range `[0, 0xf]`.
|
||||
///
|
||||
/// ```markdown
|
||||
/// [0,0,1,2,3,4,5] 0x10012345 // 7 > 4
|
||||
/// [0,1,2,3,4,5] 0x00012345 // 6 > 4
|
||||
/// [1,2,3,4,5] 0x112345 // 5 > 3
|
||||
/// [0,0,1,2,3,4] 0x00001234 // 6 > 3
|
||||
/// [0,1,2,3,4] 0x101234 // 5 > 3
|
||||
/// [1,2,3,4] 0x001234 // 4 > 3
|
||||
/// [0,0,1,2,3,4,5,T] 0x30012345 // 7 > 4
|
||||
/// [0,0,1,2,3,4,T] 0x20001234 // 6 > 4
|
||||
/// [0,1,2,3,4,5,T] 0x20012345 // 6 > 4
|
||||
/// [1,2,3,4,5,T] 0x312345 // 5 > 3
|
||||
/// [1,2,3,4,T] 0x201234 // 4 > 3
|
||||
/// ```
|
||||
fn hex_prefix_encode(nibbles: &[u8], leaf: bool) -> Vec<u8> {
|
||||
let inlen = nibbles.len();
|
||||
let oddness_factor = inlen % 2;
|
||||
// next even number divided by two
|
||||
let reslen = (inlen + 2) >> 1;
|
||||
let mut res = Vec::with_capacity(reslen);
|
||||
|
||||
let first_byte = {
|
||||
let mut bits = ((inlen as u8 & 1) + (2 * leaf as u8)) << 4;
|
||||
if oddness_factor == 1 {
|
||||
bits += nibbles[0];
|
||||
}
|
||||
bits
|
||||
};
|
||||
|
||||
res.push(first_byte);
|
||||
|
||||
let mut offset = oddness_factor;
|
||||
while offset < inlen {
|
||||
let byte = (nibbles[offset] << 4) + nibbles[offset + 1];
|
||||
res.push(byte);
|
||||
offset += 2;
|
||||
}
|
||||
|
||||
res
|
||||
}
|
||||
|
||||
/// Converts slice of bytes to nibbles.
|
||||
fn as_nibbles(bytes: &[u8]) -> Vec<u8> {
|
||||
let mut res = Vec::with_capacity(bytes.len() * 2);
|
||||
for i in 0..bytes.len() {
|
||||
let byte = bytes[i];
|
||||
res.push(byte >> 4);
|
||||
res.push(byte & 0b1111);
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
fn hash256rlp(input: &[(Vec<u8>, Vec<u8>)], pre_len: usize, stream: &mut RlpStream) {
|
||||
let inlen = input.len();
|
||||
|
||||
// in case of empty slice, just append empty data
|
||||
if inlen == 0 {
|
||||
stream.append_empty_data();
|
||||
return;
|
||||
}
|
||||
|
||||
// take slices
|
||||
let key: &[u8] = &input[0].0;
|
||||
let value: &[u8] = &input[0].1;
|
||||
|
||||
// if the slice contains just one item, append the suffix of the key
|
||||
// and then append value
|
||||
if inlen == 1 {
|
||||
stream.begin_list(2);
|
||||
stream.append(&hex_prefix_encode(&key[pre_len..], true));
|
||||
stream.append(&value);
|
||||
return;
|
||||
}
|
||||
|
||||
// get length of the longest shared prefix in slice keys
|
||||
let shared_prefix = input.iter()
|
||||
// skip first element
|
||||
.skip(1)
|
||||
// get minimum number of shared nibbles between first and each successive
|
||||
.fold(key.len(), | acc, &(ref k, _) | {
|
||||
cmp::min(key.shared_prefix_len(k), acc)
|
||||
});
|
||||
|
||||
// if shared prefix is higher than current prefix append its
|
||||
// new part of the key to the stream
|
||||
// then recursively append suffixes of all items who had this key
|
||||
if shared_prefix > pre_len {
|
||||
stream.begin_list(2);
|
||||
stream.append(&hex_prefix_encode(&key[pre_len..shared_prefix], false));
|
||||
hash256aux(input, shared_prefix, stream);
|
||||
return;
|
||||
}
|
||||
|
||||
// an item for every possible nibble/suffix
|
||||
// + 1 for data
|
||||
stream.begin_list(17);
|
||||
|
||||
// if first key len is equal to prefix_len, move to next element
|
||||
let mut begin = match pre_len == key.len() {
|
||||
true => 1,
|
||||
false => 0
|
||||
};
|
||||
|
||||
// iterate over all possible nibbles
|
||||
for i in 0..16 {
|
||||
// cout how many successive elements have same next nibble
|
||||
let len = match begin < input.len() {
|
||||
true => input[begin..].iter()
|
||||
.take_while(| pair | pair.0[pre_len] == i )
|
||||
.count(),
|
||||
false => 0
|
||||
};
|
||||
|
||||
// if at least 1 successive element has the same nibble
|
||||
// append their suffixes
|
||||
match len {
|
||||
0 => { stream.append_empty_data(); },
|
||||
_ => hash256aux(&input[begin..(begin + len)], pre_len + 1, stream)
|
||||
}
|
||||
begin += len;
|
||||
}
|
||||
|
||||
// if fist key len is equal prefix, append its value
|
||||
match pre_len == key.len() {
|
||||
true => { stream.append(&value); },
|
||||
false => { stream.append_empty_data(); }
|
||||
};
|
||||
}
|
||||
|
||||
fn hash256aux(input: &[(Vec<u8>, Vec<u8>)], pre_len: usize, stream: &mut RlpStream) {
|
||||
let mut s = RlpStream::new();
|
||||
hash256rlp(input, pre_len, &mut s);
|
||||
let out = s.out();
|
||||
match out.len() {
|
||||
0...31 => stream.append_raw(&out, 1),
|
||||
_ => stream.append(&keccak(out))
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_nibbles() {
|
||||
let v = vec![0x31, 0x23, 0x45];
|
||||
let e = vec![3, 1, 2, 3, 4, 5];
|
||||
assert_eq!(as_nibbles(&v), e);
|
||||
|
||||
// A => 65 => 0x41 => [4, 1]
|
||||
let v: Vec<u8> = From::from("A");
|
||||
let e = vec![4, 1];
|
||||
assert_eq!(as_nibbles(&v), e);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hex_prefix_encode() {
|
||||
let v = vec![0, 0, 1, 2, 3, 4, 5];
|
||||
let e = vec![0x10, 0x01, 0x23, 0x45];
|
||||
let h = hex_prefix_encode(&v, false);
|
||||
assert_eq!(h, e);
|
||||
|
||||
let v = vec![0, 1, 2, 3, 4, 5];
|
||||
let e = vec![0x00, 0x01, 0x23, 0x45];
|
||||
let h = hex_prefix_encode(&v, false);
|
||||
assert_eq!(h, e);
|
||||
|
||||
let v = vec![0, 1, 2, 3, 4, 5];
|
||||
let e = vec![0x20, 0x01, 0x23, 0x45];
|
||||
let h = hex_prefix_encode(&v, true);
|
||||
assert_eq!(h, e);
|
||||
|
||||
let v = vec![1, 2, 3, 4, 5];
|
||||
let e = vec![0x31, 0x23, 0x45];
|
||||
let h = hex_prefix_encode(&v, true);
|
||||
assert_eq!(h, e);
|
||||
|
||||
let v = vec![1, 2, 3, 4];
|
||||
let e = vec![0x00, 0x12, 0x34];
|
||||
let h = hex_prefix_encode(&v, false);
|
||||
assert_eq!(h, e);
|
||||
|
||||
let v = vec![4, 1];
|
||||
let e = vec![0x20, 0x41];
|
||||
let h = hex_prefix_encode(&v, true);
|
||||
assert_eq!(h, e);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::str::FromStr;
|
||||
use bigint::hash::H256;
|
||||
use super::trie_root;
|
||||
|
||||
#[test]
|
||||
fn simple_test() {
|
||||
assert_eq!(trie_root(vec![
|
||||
(b"A".to_vec(), b"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_vec())
|
||||
]), H256::from_str("d23786fb4a010da3ce639d66d5e904a11dbc02746d1ce25029e53290cabf28ab").unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_triehash_out_of_order() {
|
||||
assert!(trie_root(vec![
|
||||
(vec![0x01u8, 0x23], vec![0x01u8, 0x23]),
|
||||
(vec![0x81u8, 0x23], vec![0x81u8, 0x23]),
|
||||
(vec![0xf1u8, 0x23], vec![0xf1u8, 0x23]),
|
||||
]) ==
|
||||
trie_root(vec![
|
||||
(vec![0x01u8, 0x23], vec![0x01u8, 0x23]),
|
||||
(vec![0xf1u8, 0x23], vec![0xf1u8, 0x23]),
|
||||
(vec![0x81u8, 0x23], vec![0x81u8, 0x23]),
|
||||
]));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity.
|
||||
|
||||
// Parity is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Vector extensions.
|
||||
|
||||
/// Returns len of prefix shared with elem
|
||||
///
|
||||
/// ```rust
|
||||
/// extern crate ethcore_util as util;
|
||||
/// use util::vector::SharedPrefix;
|
||||
///
|
||||
/// fn main () {
|
||||
/// let a = vec![1,2,3,3,5];
|
||||
/// let b = vec![1,2,3];
|
||||
/// assert_eq!(a.shared_prefix_len(&b), 3);
|
||||
/// }
|
||||
/// ```
|
||||
pub trait SharedPrefix<T> {
|
||||
/// Get common prefix length
|
||||
fn shared_prefix_len(&self, elem: &[T]) -> usize;
|
||||
}
|
||||
|
||||
impl<T> SharedPrefix<T> for [T] where T: Eq {
|
||||
fn shared_prefix_len(&self, elem: &[T]) -> usize {
|
||||
use std::cmp;
|
||||
let len = cmp::min(self.len(), elem.len());
|
||||
(0..len).take_while(|&i| self[i] == elem[i]).count()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use vector::SharedPrefix;
|
||||
|
||||
#[test]
|
||||
fn test_shared_prefix() {
|
||||
let a = vec![1,2,3,4,5,6];
|
||||
let b = vec![4,2,3,4,5,6];
|
||||
assert_eq!(a.shared_prefix_len(&b), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_shared_prefix2() {
|
||||
let a = vec![1,2,3,3,5];
|
||||
let b = vec![1,2,3];
|
||||
assert_eq!(a.shared_prefix_len(&b), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_shared_prefix3() {
|
||||
let a = vec![1,2,3,4,5,6];
|
||||
let b = vec![1,2,3,4,5,6];
|
||||
assert_eq!(a.shared_prefix_len(&b), 6);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user