-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added distinct LINQ * Added Union method * Updated README.md
- Loading branch information
1 parent
478c783
commit 1dea704
Showing
9 changed files
with
210 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#[derive(Clone)] | ||
pub struct DistinctIterator<I> | ||
where | ||
I::Item : Eq + std::hash::Hash + Copy, | ||
I : Iterator | ||
{ | ||
source : I, | ||
hash_map : std::collections::HashSet<I::Item> | ||
} | ||
|
||
impl<I> Iterator for DistinctIterator<I> | ||
where | ||
I : Iterator, | ||
I::Item : Eq + std::hash::Hash + Copy, | ||
{ | ||
type Item = I::Item; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
loop { | ||
match self.source.next() { | ||
Some(item) => { | ||
if self.hash_map.insert(item) | ||
{ | ||
return Some(item); | ||
} | ||
} | ||
None => { | ||
return None; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
pub fn distinct<I>( | ||
iter: I | ||
) -> DistinctIterator<I> | ||
where | ||
I : Iterator, | ||
I::Item : Eq + std::hash::Hash + Copy, | ||
{ | ||
let hash_map = std::collections::HashSet::new(); | ||
DistinctIterator { | ||
hash_map : hash_map, | ||
source : iter | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#[derive(Clone)] | ||
pub struct UnionIterator<I, U> | ||
where | ||
I::Item : Eq + std::hash::Hash + Copy, | ||
I : Iterator, | ||
U : Iterator<Item=I::Item>, | ||
{ | ||
first_source : I, | ||
second_source : U, | ||
was_first_source_consumed : bool, | ||
hash_map : std::collections::HashSet<I::Item> | ||
} | ||
|
||
impl<I, U> Iterator for UnionIterator<I, U> | ||
where | ||
I : Iterator, | ||
U : Iterator<Item=I::Item>, | ||
I::Item : Eq + std::hash::Hash + Copy, | ||
{ | ||
type Item = I::Item; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
loop { | ||
if !self.was_first_source_consumed | ||
{ | ||
match self.first_source.next() { | ||
Some(item) => { | ||
if self.hash_map.insert(item) | ||
{ | ||
return Some(item); | ||
} | ||
} | ||
None => { | ||
self.was_first_source_consumed = true; | ||
} | ||
} | ||
} | ||
match self.second_source.next() { | ||
Some(item) => { | ||
if self.hash_map.insert(item) | ||
{ | ||
return Some(item); | ||
} | ||
} | ||
None => { | ||
return None; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
pub fn union<I, U>( | ||
iter: I, | ||
iter_union : U | ||
) -> UnionIterator<I, U> | ||
where | ||
I : Iterator, | ||
U : Iterator<Item=I::Item>, | ||
I::Item : Eq + std::hash::Hash + Copy, | ||
{ | ||
let hash_map = std::collections::HashSet::new(); | ||
UnionIterator { | ||
hash_map : hash_map, | ||
first_source : iter, | ||
second_source : iter_union, | ||
was_first_source_consumed : false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,5 +5,7 @@ mod m_order_by; | |
mod m_select; | ||
mod m_builtin; | ||
mod m_method; | ||
mod m_distinct; | ||
mod m_union; | ||
|
||
pub use m_enumerable::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters