now we have an iterator - albeit somewhat slow

This commit is contained in:
Yehowshua Immanuel 2022-05-18 22:57:42 -04:00
parent de08a60f17
commit 0497015783
3 changed files with 27 additions and 45 deletions

View file

@ -2,6 +2,7 @@ use std::io::prelude::*;
use std::io;
use std::fs::File;
use std::collections::BTreeMap;
use ::next_gen::prelude::*;
use num::*;
use clap::Parser;
@ -13,24 +14,6 @@ struct Cli {
path: std::path::PathBuf,
}
struct Timestamp{
file_offset: u64,
timestamp: BigInt
}
struct Cursor{
line: u64,
col : u64
}
enum Tokens {
Date,
End,
String,
Version,
Time,
}
struct Signal {
name : String,
timeline : BTreeMap<BigInt, BigInt>,
@ -38,23 +21,21 @@ struct Signal {
parent_index : usize
}
fn main() -> std::io::Result<()> {
let args = Cli::parse();
let space = " ".as_bytes()[0];
let file = File::open(&args.path)?;
#[generator(yield(String))]
fn yield_words(file : File) {
let mut reader = io::BufReader::new(file);
let mut buffer = String::new();
let mut word_count = 0u64;
let mut do_break = false;
let mut EOF = false;
let line_chunk_size = 25;
while {!do_break} {
while {!EOF} {
for _ in 0..line_chunk_size {
let bytes_read = reader.read_line(&mut buffer).unwrap();
if bytes_read == 0 {
do_break = true;
EOF = true;
break
}
}
@ -62,13 +43,24 @@ fn main() -> std::io::Result<()> {
let words = buffer.split_ascii_whitespace();
for word in words {
word_count += 1;
yield_!(word.to_string());
}
buffer.clear();
}
dbg!(word_count);
}
fn main() -> std::io::Result<()> {
let args = Cli::parse();
let file = File::open(&args.path)?;
let mut word_count = 0;
mk_gen!(let mut generator = yield_words(file));
for word in generator {
word_count += 1;
}
Ok(())
}