From 1b50e1e3c5e8d46e4ae581023ca115b39cfc4d9f Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Sun, 21 Aug 2022 08:49:52 -0700 Subject: [PATCH] move lines under chunk --- rust/src/chunk.rs | 83 ++++++++++++++++++++++++----------------------- 1 file changed, 42 insertions(+), 41 deletions(-) diff --git a/rust/src/chunk.rs b/rust/src/chunk.rs index 41ddbe5..d6ac680 100644 --- a/rust/src/chunk.rs +++ b/rust/src/chunk.rs @@ -17,47 +17,6 @@ pub struct Chunk { lines: Lines, } -// Lines are stored using run-length encoding, where the first element is the line and the second -// element the number of instructions that are associated with that line -#[derive(Default, Debug)] -struct Lines(std::vec::Vec<(usize, usize)>); - -impl Lines { - fn add(&mut self, line: usize) { - if let Some(last) = self.0.last_mut() { - last.1 += 1; - } else { - self.0.push((line, 1)); - } - } - - fn get(&self, offset: usize) -> (usize, bool) { - let mut offset = offset; - for (line, run) in self.0.iter() { - if offset == 0 { - return (*line, true); - } - - if offset < *run { - return (*line, false); - } - - offset -= run; - } - - unreachable!() - } -} - -#[test] -fn test_get_line() { - let lines = Lines(vec![(1_usize, 2_usize), (2_usize, 2_usize)]); - assert_eq!(lines.get(0), (1, true)); - assert_eq!(lines.get(1), (1, false)); - assert_eq!(lines.get(2), (2, true)); - assert_eq!(lines.get(3), (2, false)); -} - impl Chunk { pub fn write(&mut self, byte: u8, line: usize) { self.code.push(byte); @@ -110,3 +69,45 @@ impl Chunk { offset + 2 } } + +// Lines are stored using run-length encoding, where the first element is the line and the second +// element the number of instructions that are associated with that line +#[derive(Default, Debug)] +struct Lines(std::vec::Vec<(usize, usize)>); + +impl Lines { + fn add(&mut self, line: usize) { + if let Some(last) = self.0.last_mut() { + last.1 += 1; + } else { + self.0.push((line, 1)); + } + } + + fn get(&self, offset: usize) -> (usize, bool) { + let mut offset = offset; + for (line, run) in self.0.iter() { + if offset == 0 { + return (*line, true); + } + + if offset < *run { + return (*line, false); + } + + offset -= run; + } + + unreachable!() + } +} + +#[test] +fn test_get_line() { + let lines = Lines(vec![(1_usize, 2_usize), (2_usize, 2_usize)]); + assert_eq!(lines.get(0), (1, true)); + assert_eq!(lines.get(1), (1, false)); + assert_eq!(lines.get(2), (2, true)); + assert_eq!(lines.get(3), (2, false)); +} +