Skip to content

Commit

Permalink
Returning current row, and copying any row
Browse files Browse the repository at this point in the history
  • Loading branch information
tmahmood committed Dec 7, 2023
1 parent 1b63367 commit 697d1b9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
24 changes: 24 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 22 additions & 3 deletions src/table_map.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fmt::Debug;
use std::slice::Iter;

use indexmap::IndexMap;

Expand Down Expand Up @@ -63,14 +64,14 @@ macro_rules! push {
}

#[derive(Clone)]
pub struct TableMap<T: Default + Clone + Debug> {
pub struct TableMap<T: Default + Debug + Clone> {
columns: IndexMap<String, usize>,
col_index: usize,
rows: Vec<Vec<T>>,
is_current_row_dirty: bool
}

impl<T: Default + Clone + Debug> TableMap<T> {
impl<T: Default + Debug + Clone> TableMap<T> {
pub fn new() -> Self {
Self {
columns: IndexMap::new(),
Expand All @@ -80,6 +81,11 @@ impl<T: Default + Clone + Debug> TableMap<T> {
}
}

/// returns current row index, None if there is no rows inserted yet
pub fn current_row_index(&self) -> Option<usize> {
if self.rows.len() == 0 { return None }
Some(self.rows.len() - 1)
}
/// number of rows
pub fn num_rows(&self) -> usize {
self.rows.len()
Expand All @@ -95,7 +101,7 @@ impl<T: Default + Clone + Debug> TableMap<T> {
self.columns.keys().cloned().collect()
}

/// insert current row to main collection, clears the current row
/// moves to next row.
pub fn next_row(&mut self) {
self.rows.push(vec![T::default(); self.columns.len()]);
}
Expand All @@ -106,6 +112,12 @@ impl<T: Default + Clone + Debug> TableMap<T> {
self.rows.push(new_row);
}

/// copies the row given by row_index, and push it at the end, creating duplicate
pub fn copy_row_at_index(&mut self, row_index: usize) {
let new_row = self.rows.get( row_index).cloned().unwrap();
self.rows.push(new_row);
}

/// Adds a column
pub fn add_column(&mut self, col_name: &str) {
if self.columns.contains_key(col_name) {
Expand Down Expand Up @@ -182,6 +194,7 @@ impl<T: Default + Clone + Debug> TableMap<T> {
.cloned()
}

/// get all the data, this returns reference, so will not take any additional memory
pub fn get_vec(&self) -> &Vec<Vec<T>> {
&self.rows
}
Expand Down Expand Up @@ -211,4 +224,10 @@ impl<T: Default + Clone + Debug> TableMap<T> {
}
self.rows.last_mut().unwrap()
}

/// returns iter for the inner vec
pub fn iter(&self) -> Iter<'_, Vec<T>> {
self.rows.iter()
}

}

0 comments on commit 697d1b9

Please sign in to comment.