Merge branch 'master' of github.com:ethcore/parity into move_hash

This commit is contained in:
debris
2016-08-04 08:41:30 +02:00
66 changed files with 1281 additions and 600 deletions

View File

@@ -19,7 +19,8 @@
use std::{ops, fmt, cmp, mem};
use std::cmp::*;
use std::ops::*;
use std::hash::{Hash, Hasher};
use std::hash::{Hash, Hasher, BuildHasherDefault};
use std::collections::{HashMap, HashSet};
use std::str::FromStr;
use rand::Rng;
use rand::os::OsRng;
@@ -544,6 +545,37 @@ impl_hash!(H1024, 128);
impl_hash!(H2048, 256);
known_heap_size!(0, H32, H64, H128, Address, H256, H264, H512, H520, H1024, H2048);
// Specialized HashMap and HashSet
/// Hasher that just takes 8 bytes of the provided value.
pub struct PlainHasher(u64);
impl Default for PlainHasher {
#[inline]
fn default() -> PlainHasher {
PlainHasher(0)
}
}
impl Hasher for PlainHasher {
#[inline]
fn finish(&self) -> u64 {
self.0
}
#[inline]
fn write(&mut self, bytes: &[u8]) {
debug_assert!(bytes.len() == 32);
let mut prefix = [0u8; 8];
prefix.clone_from_slice(&bytes[0..8]);
self.0 = unsafe { ::std::mem::transmute(prefix) };
}
}
/// Specialized version of HashMap with H256 keys and fast hashing function.
pub type H256FastMap<T> = HashMap<H256, T, BuildHasherDefault<PlainHasher>>;
/// Specialized version of HashSet with H256 keys and fast hashing function.
pub type H256FastSet = HashSet<H256, BuildHasherDefault<PlainHasher>>;
#[cfg(test)]
mod tests {

View File

@@ -36,10 +36,8 @@
//! The functions here are designed to be fast.
//!
#[cfg(all(asm_available, target_arch="x86_64"))]
use std::mem;
use std::fmt;
use std::str::{FromStr};
use std::convert::From;
use std::hash::Hash;
@@ -647,16 +645,46 @@ macro_rules! construct_uint {
(arr[index / 8] >> (((index % 8)) * 8)) as u8
}
#[cfg(any(
target_arch = "arm",
target_arch = "mips",
target_arch = "powerpc",
target_arch = "x86",
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "powerpc64"))]
#[inline]
fn to_big_endian(&self, bytes: &mut[u8]) {
assert!($n_words * 8 == bytes.len());
debug_assert!($n_words * 8 == bytes.len());
let &$name(ref arr) = self;
for i in 0..bytes.len() {
let rev = bytes.len() - 1 - i;
let pos = rev / 8;
bytes[i] = (arr[pos] >> ((rev % 8) * 8)) as u8;
unsafe {
let mut out: *mut u64 = mem::transmute(bytes.as_mut_ptr());
out = out.offset($n_words);
for i in 0..$n_words {
out = out.offset(-1);
*out = arr[i].swap_bytes();
}
}
}
#[cfg(not(any(
target_arch = "arm",
target_arch = "mips",
target_arch = "powerpc",
target_arch = "x86",
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "powerpc64")))]
#[inline]
fn to_big_endian(&self, bytes: &mut[u8]) {
debug_assert!($n_words * 8 == bytes.len());
let &$name(ref arr) = self;
for i in 0..bytes.len() {
let rev = bytes.len() - 1 - i;
let pos = rev / 8;
bytes[i] = (arr[pos] >> ((rev % 8) * 8)) as u8;
}
}
#[inline]
fn exp10(n: usize) -> Self {
match n {