Refactoring & Updates
* Renamed borrowed to full_record and owned to destructured * Updated to new Index API
This commit is contained in:
@@ -20,7 +20,6 @@ include = ["Cargo.toml", "README.md", "LICENSE", "ACKNOWLEDGEMENTS", "src/**/*.r
|
||||
|
||||
[dependencies]
|
||||
ironsea_index = "^0.1"
|
||||
ironsea_table = "^0.1"
|
||||
|
||||
serde = "^1.0"
|
||||
serde_derive = "^1.0"
|
||||
|
||||
102
src/borrowed.rs
102
src/borrowed.rs
@@ -1,102 +0,0 @@
|
||||
use std::collections::HashMap;
|
||||
use std::hash::Hash;
|
||||
use std::marker;
|
||||
|
||||
use ironsea_index::Indexed;
|
||||
use ironsea_index::Record;
|
||||
use ironsea_table::Table;
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct Index<T, R, K>
|
||||
where
|
||||
T: Table<R>,
|
||||
R: Record<K>,
|
||||
K: Hash + Eq + PartialEq + Ord,
|
||||
{
|
||||
table: HashMap<K, R>,
|
||||
keys: Vec<K>,
|
||||
_marker: marker::PhantomData<(T)>,
|
||||
}
|
||||
|
||||
impl<T, R, K> Index<T, R, K>
|
||||
where
|
||||
T: Table<R>,
|
||||
R: Clone + Record<K>,
|
||||
K: Hash + Eq + PartialEq + Ord,
|
||||
{
|
||||
pub fn new(table: T) -> Self {
|
||||
let size = table.get_table().len();
|
||||
let mut ht = HashMap::with_capacity(size);
|
||||
let mut keys = Vec::with_capacity(size);
|
||||
|
||||
for r in table.get_table() {
|
||||
keys.push(r.key());
|
||||
ht.insert(r.key(), r.clone());
|
||||
}
|
||||
|
||||
keys.sort_unstable();
|
||||
keys.dedup();
|
||||
|
||||
Index {
|
||||
table: ht,
|
||||
keys,
|
||||
_marker: marker::PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn keys(&self) -> &Vec<K> {
|
||||
&self.keys
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, R, K> Indexed<T, R, K> for Index<T, R, K>
|
||||
where
|
||||
T: Table<R>,
|
||||
R: Record<K>,
|
||||
K: Hash + Eq + PartialEq + Ord,
|
||||
{
|
||||
fn find(&self, key: &K) -> Vec<&R> {
|
||||
let mut values = vec![];
|
||||
|
||||
if let Some(record) = self.table.get(key) {
|
||||
values.push(record);
|
||||
}
|
||||
|
||||
values
|
||||
}
|
||||
|
||||
fn find_range(&self, start: &K, end: &K) -> Vec<&R> {
|
||||
let start = match self.keys.binary_search(start) {
|
||||
Ok(i) => i,
|
||||
Err(i) => {
|
||||
if i >= self.keys.len() {
|
||||
self.keys.len() - 1
|
||||
} else {
|
||||
i
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let end = match self.keys.binary_search(end) {
|
||||
Ok(i) => i,
|
||||
Err(i) => {
|
||||
if i >= self.keys.len() {
|
||||
self.keys.len() - 1
|
||||
} else {
|
||||
i
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let mut values = vec![];
|
||||
|
||||
for i in start..end {
|
||||
let key = &self.keys[i];
|
||||
if let Some(record) = self.table.get(key) {
|
||||
values.push(record);
|
||||
}
|
||||
}
|
||||
|
||||
values
|
||||
}
|
||||
}
|
||||
88
src/destructured.rs
Normal file
88
src/destructured.rs
Normal file
@@ -0,0 +1,88 @@
|
||||
#![allow(clippy::type_repetition_in_bounds)]
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::hash::Hash;
|
||||
|
||||
use ironsea_index::IndexedDestructured;
|
||||
use ironsea_index::Record;
|
||||
use ironsea_index::RecordFields;
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct Index<F, K>
|
||||
where
|
||||
K: Clone + Eq + Hash + PartialEq + Ord,
|
||||
{
|
||||
hashmap: HashMap<K, F>,
|
||||
keys: Vec<K>,
|
||||
}
|
||||
|
||||
impl<F, K> Index<F, K>
|
||||
where
|
||||
K: Clone + Eq + Hash + PartialEq + Ord,
|
||||
{
|
||||
pub fn new<I, R>(iter: I) -> Self
|
||||
where
|
||||
I: Iterator<Item = R>,
|
||||
R: Record<K> + RecordFields<F>,
|
||||
{
|
||||
let (size, _) = iter.size_hint();
|
||||
let mut hashmap = HashMap::with_capacity(size);
|
||||
|
||||
for r in iter {
|
||||
hashmap.insert(r.key(), r.fields());
|
||||
}
|
||||
|
||||
let mut keys = hashmap.keys().cloned().collect::<Vec<_>>();
|
||||
keys.sort_unstable();
|
||||
|
||||
Index { hashmap, keys }
|
||||
}
|
||||
|
||||
pub fn keys(&self) -> &Vec<K> {
|
||||
&self.keys
|
||||
}
|
||||
|
||||
pub fn index(&self, key: &K) -> usize {
|
||||
match self.keys.binary_search(&key) {
|
||||
Ok(i) => i,
|
||||
Err(i) => {
|
||||
if i >= self.keys.len() {
|
||||
self.keys.len() - 1
|
||||
} else {
|
||||
i
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<F, K> IndexedDestructured<F, K> for Index<F, K>
|
||||
where
|
||||
K: Clone + Eq + Hash + PartialEq + Ord,
|
||||
{
|
||||
fn find(&self, key: &K) -> Vec<&F> {
|
||||
let mut values = vec![];
|
||||
|
||||
if let Some(fields) = self.hashmap.get(key) {
|
||||
values.push(fields);
|
||||
}
|
||||
|
||||
values
|
||||
}
|
||||
|
||||
fn find_range(&self, start: &K, end: &K) -> Vec<(K, &F)> {
|
||||
let start = self.index(start);
|
||||
let end = self.index(end);
|
||||
|
||||
(start..end)
|
||||
.filter_map(|i| {
|
||||
let key = &self.keys[i];
|
||||
if let Some(fields) = self.hashmap.get(key) {
|
||||
Some((key.clone(), fields))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
82
src/full_record.rs
Normal file
82
src/full_record.rs
Normal file
@@ -0,0 +1,82 @@
|
||||
#![allow(clippy::type_repetition_in_bounds)]
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::hash::Hash;
|
||||
use std::iter::Iterator;
|
||||
|
||||
use ironsea_index::Indexed;
|
||||
use ironsea_index::Record;
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct Index<R, K>
|
||||
where
|
||||
K: Clone + Eq + Hash + PartialEq + Ord,
|
||||
{
|
||||
hashmap: HashMap<K, R>,
|
||||
keys: Vec<K>,
|
||||
}
|
||||
|
||||
impl<K, R> Index<R, K>
|
||||
where
|
||||
R: Record<K>,
|
||||
K: Clone + Eq + Hash + PartialEq + Ord,
|
||||
{
|
||||
pub fn new<I>(iter: I) -> Self
|
||||
where
|
||||
I: Iterator<Item = R>,
|
||||
{
|
||||
let (size, _) = iter.size_hint();
|
||||
let mut hashmap = HashMap::with_capacity(size);
|
||||
|
||||
for r in iter {
|
||||
hashmap.insert(r.key(), r);
|
||||
}
|
||||
|
||||
let mut keys = hashmap.keys().cloned().collect::<Vec<_>>();
|
||||
keys.sort_unstable();
|
||||
|
||||
Index { hashmap, keys }
|
||||
}
|
||||
|
||||
pub fn keys(&self) -> &Vec<K> {
|
||||
&self.keys
|
||||
}
|
||||
|
||||
pub fn index(&self, key: &K) -> usize {
|
||||
match self.keys.binary_search(key) {
|
||||
Ok(i) => i,
|
||||
Err(i) => {
|
||||
if i >= self.keys.len() {
|
||||
self.keys.len() - 1
|
||||
} else {
|
||||
i
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, R> Indexed<R, K> for Index<R, K>
|
||||
where
|
||||
R: Record<K>,
|
||||
K: Clone + Eq + Hash + PartialEq + Ord,
|
||||
{
|
||||
fn find(&self, key: &K) -> Vec<&R> {
|
||||
let mut values = vec![];
|
||||
|
||||
if let Some(record) = self.hashmap.get(key) {
|
||||
values.push(record);
|
||||
}
|
||||
|
||||
values
|
||||
}
|
||||
|
||||
fn find_range(&self, start: &K, end: &K) -> Vec<&R> {
|
||||
let start = self.index(start);
|
||||
let end = self.index(end);
|
||||
|
||||
(start..end)
|
||||
.filter_map(|i| self.hashmap.get(&self.keys[i]))
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
10
src/lib.rs
10
src/lib.rs
@@ -1,10 +1,8 @@
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
|
||||
mod borrowed;
|
||||
mod owned;
|
||||
mod destructured;
|
||||
mod full_record;
|
||||
|
||||
pub use ironsea_table::Table;
|
||||
|
||||
pub use borrowed::Index;
|
||||
pub use owned::IndexOwned;
|
||||
pub use destructured::Index as IndexDestructured;
|
||||
pub use full_record::Index;
|
||||
|
||||
104
src/owned.rs
104
src/owned.rs
@@ -1,104 +0,0 @@
|
||||
use std::collections::HashMap;
|
||||
use std::hash::Hash;
|
||||
use std::marker;
|
||||
|
||||
use ironsea_index::IndexedOwned;
|
||||
use ironsea_index::Record;
|
||||
use ironsea_index::RecordBuild;
|
||||
use ironsea_index::RecordFields;
|
||||
use ironsea_table::Table;
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct IndexOwned<T, R, K, F>
|
||||
where
|
||||
T: Table<R>,
|
||||
R: Record<K> + RecordFields<F> + RecordBuild<K, F, R>,
|
||||
K: Hash + Eq + PartialEq + Ord,
|
||||
{
|
||||
table: HashMap<K, F>,
|
||||
keys: Vec<K>,
|
||||
_marker: marker::PhantomData<(T, R)>,
|
||||
}
|
||||
|
||||
impl<T, R, K, F> IndexOwned<T, R, K, F>
|
||||
where
|
||||
T: Table<R>,
|
||||
R: Record<K> + RecordFields<F> + RecordBuild<K, F, R>,
|
||||
K: Hash + Eq + PartialEq + Ord,
|
||||
{
|
||||
pub fn new(table: T) -> Self {
|
||||
let size = table.get_table().len();
|
||||
let mut ht = HashMap::with_capacity(size);
|
||||
let mut keys = Vec::with_capacity(size);
|
||||
|
||||
for r in table.get_table() {
|
||||
ht.insert(r.key(), r.fields());
|
||||
keys.push(r.key());
|
||||
}
|
||||
|
||||
keys.sort_unstable();
|
||||
keys.dedup();
|
||||
|
||||
IndexOwned {
|
||||
table: ht,
|
||||
keys,
|
||||
_marker: marker::PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn keys(&self) -> &Vec<K> {
|
||||
&self.keys
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, R, K, F> IndexedOwned<T, R, K> for IndexOwned<T, R, K, F>
|
||||
where
|
||||
T: Table<R>,
|
||||
R: Record<K> + RecordFields<F> + RecordBuild<K, F, R>,
|
||||
K: Hash + Eq + PartialEq + Ord,
|
||||
{
|
||||
fn find(&self, key: &K) -> Vec<R> {
|
||||
let mut values = vec![];
|
||||
|
||||
if let Some(fields) = self.table.get(key) {
|
||||
values.push(R::build(key, fields));
|
||||
}
|
||||
|
||||
values
|
||||
}
|
||||
|
||||
fn find_range(&self, start: &K, end: &K) -> Vec<R> {
|
||||
let start = match self.keys.binary_search(start) {
|
||||
Ok(i) => i,
|
||||
Err(i) => {
|
||||
if i >= self.keys.len() {
|
||||
self.keys.len() - 1
|
||||
} else {
|
||||
i
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let end = match self.keys.binary_search(end) {
|
||||
Ok(i) => i,
|
||||
Err(i) => {
|
||||
if i >= self.keys.len() {
|
||||
self.keys.len() - 1
|
||||
} else {
|
||||
i
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let mut values = vec![];
|
||||
|
||||
for i in start..end {
|
||||
let key = &self.keys[i];
|
||||
if let Some(record) = self.table.get(key) {
|
||||
values.push(R::build(key, record));
|
||||
}
|
||||
}
|
||||
|
||||
values
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user