mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-29 20:34:44 +00:00
Organize crates
This commit is contained in:
parent
6ddef21fec
commit
f9d91aa38e
71 changed files with 6 additions and 14 deletions
70
compiler/erg_common/lazy_buffer.rs
Normal file
70
compiler/erg_common/lazy_buffer.rs
Normal file
|
@ -0,0 +1,70 @@
|
|||
// Copied and modified from https://github.com/rust-itertools/itertools/blob/master/src/lazy_buffer.rs
|
||||
// MIT license | Apache 2.0 license
|
||||
// License files are placed at the root.
|
||||
|
||||
use std::ops::Index;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct LazyBuffer<I: Iterator> {
|
||||
pub it: I,
|
||||
done: bool,
|
||||
buffer: Vec<I::Item>,
|
||||
}
|
||||
|
||||
impl<I> LazyBuffer<I>
|
||||
where
|
||||
I: Iterator,
|
||||
{
|
||||
pub fn new(it: I) -> LazyBuffer<I> {
|
||||
LazyBuffer {
|
||||
it,
|
||||
done: false,
|
||||
buffer: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.buffer.len()
|
||||
}
|
||||
|
||||
pub fn get_next(&mut self) -> bool {
|
||||
if self.done {
|
||||
return false;
|
||||
}
|
||||
let next_item = self.it.next();
|
||||
match next_item {
|
||||
Some(x) => {
|
||||
self.buffer.push(x);
|
||||
true
|
||||
}
|
||||
None => {
|
||||
self.done = true;
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn prefill(&mut self, len: usize) {
|
||||
let buffer_len = self.buffer.len();
|
||||
|
||||
if !self.done && len > buffer_len {
|
||||
let delta = len - buffer_len;
|
||||
|
||||
self.buffer.extend(self.it.by_ref().take(delta));
|
||||
self.done = self.buffer.len() < len;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<I, J> Index<J> for LazyBuffer<I>
|
||||
where
|
||||
I: Iterator,
|
||||
I::Item: Sized,
|
||||
Vec<I::Item>: Index<J>
|
||||
{
|
||||
type Output = <Vec<I::Item> as Index<J>>::Output;
|
||||
|
||||
fn index(&self, _index: J) -> &Self::Output {
|
||||
self.buffer.index(_index)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue