-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
8 additions
and
56 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,59 +1,19 @@ | ||
use std::collections::VecDeque; | ||
|
||
use crate::pathiterator::{FileIterator, IteratorItem}; | ||
|
||
pub struct FilteredIterator { | ||
pub source: FileIterator, | ||
cache: VecDeque<IteratorItem>, | ||
skip: bool, | ||
next_item: Option<IteratorItem>, | ||
} | ||
|
||
impl FilteredIterator { | ||
pub fn new(iterator: FileIterator) -> Self { | ||
FilteredIterator { | ||
source: iterator, | ||
cache: VecDeque::new(), | ||
skip: false, | ||
next_item: None, | ||
} | ||
} | ||
|
||
pub fn skip_filter(&mut self) { | ||
self.skip = true; | ||
FilteredIterator { source: iterator } | ||
} | ||
} | ||
|
||
impl Iterator for FilteredIterator { | ||
type Item = IteratorItem; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
if self.skip { | ||
return self.source.next(); | ||
} | ||
|
||
if let Some(cache_item) = self.cache.pop_front() { | ||
return Some(cache_item); | ||
} | ||
|
||
if let Some(next_item) = self.next_item.take() { | ||
return Some(next_item); | ||
} | ||
|
||
for item in self.source.by_ref() { | ||
if item.is_dir() { | ||
self.cache.push_back(item); | ||
} else { | ||
// If the cache already contains a folder, | ||
// start emptying cache, and save the item. | ||
if let Some(cache_front) = self.cache.pop_front() { | ||
self.next_item = Some(item); | ||
return Some(cache_front); | ||
} | ||
return Some(item); | ||
} | ||
} | ||
|
||
None | ||
self.source.next() | ||
} | ||
} |
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