Separate planks from main

[?]
Jul 11, 2020, 8:55 PM
ZJPL7VNQEYASQ7QIUCR3X2IPWF5MAR6WX7VY6OWMRDMPJK7GYWIAC

Dependencies

Change contents

  • file addition: floor.rs (----------)
    [14.487]
    // Use the same global constants
    use crate::PLANKMAX;
    use crate::SAWBLADE;
    // Define a plank piece structure
    #[derive(Debug)]
    pub struct Plank {
    length: u32,
    endpiece: bool,
    new: bool,
    }
    impl Default for Plank {
    /// Create a new, default plank
    fn default() -> Self {
    Self {
    length: PLANKMAX,
    endpiece: false,
    new: true,
    }
    }
    }
    impl Plank {
    /// Create a new plank
    pub fn new(&self) -> Self {
    Plank::default()
    }
    /// Is this a new plank?
    pub fn is_new(&self) -> bool {
    self.new
    }
    /// Is this at the end of a row
    pub fn get_endpiece(&self) -> bool {
    self.endpiece
    }
    /// Make this plank an endpiece
    pub fn set_endpiece(&mut self) -> () {
    self.endpiece = true;
    }
    /// Return the length of the plank
    pub fn length(&self) -> u32 {
    self.length
    }
    /// Return the original plank with the new length along with leftover
    pub fn cut_to_length(mut self, measure: u32) -> (Plank, Plank) {
    if measure > PLANKMAX {
    panic!("Can't cut a plank longer than it is!");
    }
    // Create the leftover plank, correct for material loss due to sawing
    let leftover_plank = Plank {
    length: self.length - (measure + SAWBLADE),
    endpiece: false,
    new: false,
    };
    // Trim self to the new length
    self.length = measure;
    // In case the plank was new, mark it as cut
    if self.new {
    self.new = false;
    }
    // Return the original plank and the leftover
    (self, leftover_plank)
    }
    }
    // Define a row structure
    #[derive(Debug)]
    pub struct Row {
    planks: Vec<Plank>,
    coverage: u32,
    full: bool,
    }
    impl Default for Row {
    /// Create a new, empty row
    fn default() -> Self {
    Self {
    planks: vec![],
    coverage: 0,
    full: false,
    }
    }
    }
    impl Row {
    /// Is this row considered full?
    pub fn get_full(&self) -> bool {
    self.full
    }
    /// Mark this row as full
    pub fn set_full(&mut self) -> () {
    self.full = true;
    }
    /// Get the number of plank rows for this floor
    pub fn rows_count(&self) -> usize {
    self.planks.len()
    }
    /// Get the number of plank rows for this floor
    pub fn planks(&mut self) -> &mut Vec<Plank> {
    &mut self.planks
    }
    /// Increase how much of the row is filled
    pub fn add_coverage(&mut self, coverage: u32) -> () {
    self.coverage += coverage;
    }
    /// Get the number of plank rows for this floor
    pub fn get_coverage(&self) -> u32 {
    self.coverage
    }
    /// Add a plank to this row
    pub fn add(&mut self, plank: Plank) -> () {
    self.planks.push(plank);
    }
    }
    // Define a floor structure
    #[derive(Debug)]
    pub struct Floor {
    rows: Vec<Row>,
    coverage: u32,
    complete: bool,
    }
    impl Default for Floor {
    /// Create a new, empty row
    fn default() -> Self {
    Self {
    rows: vec![],
    coverage: 0,
    complete: false,
    }
    }
    }
    impl Floor {
    /// Is this floor considered complete?
    pub fn is_complete(&self) -> bool {
    self.complete
    }
    /// Get the number of plank rows for this floor
    pub fn rows_count(&self) -> usize {
    self.rows.len()
    }
    pub fn add(&mut self, row: Row) -> () {
    self.rows.push(row);
    }
    }
  • replacement in src/main.rs at line 1
    [3.1][3.0:29]()
    static PLANKMAX: u32 = 2200;
    [3.1]
    [3.29]
    // Import and use floor.rs
    mod floor;
    use floor::{Plank, Row, Floor};
    //static PLANKMAX: u32 = 2200;
    static PLANKMAX: u32 = 2010;
  • edit in src/main.rs at line 9
    [3.82]
    [3.0]
    static SAWBLADE: u32 = 10;
  • edit in src/main.rs at line 14
    [3.61][3.132:133](),[3.71][3.132:133](),[3.75][3.132:133](),[3.98][3.132:133](),[3.185][3.132:133](),[3.132][3.132:133](),[3.133][3.0:34](),[3.34][3.214:283](),[3.214][3.214:283](),[3.283][3.65:80](),[3.80][3.35:38](),[3.283][3.35:38](),[3.38][3.81:297](),[3.297][2.0:29](),[2.29][3.338:392](),[3.338][3.338:392](),[3.392][2.30:66](),[2.66][3.444:509](),[3.444][3.444:509](),[3.509][2.67:106](),[2.106][3.0:57](),[3.509][3.0:57](),[3.57][2.107:181](),[3.57][3.509:997](),[2.181][3.509:997](),[3.509][3.509:997]()
    // Define a plank piece structure
    #[derive(Debug)]
    struct Plank {
    length: u32,
    endpiece: bool,
    new: bool,
    }
    impl Default for Plank {
    /// Create a new, default plank
    fn default() -> Self {
    Self {
    length: PLANKMAX,
    endpiece: false,
    new: true,
    }
    }
    }
    impl Plank {
    /// Is this a new plank?
    fn is_new(&self) -> bool {
    self.new
    }
    /// Is this at the end of a row
    fn is_endpiece(&self) -> bool {
    self.endpiece
    }
    /// Return the length of the plank
    fn length(&self) -> u32 {
    self.length
    }
    /// Return the original plank with the new length along with leftover
    fn cut_to_length(mut self, measure: u32) -> (Plank, Plank) {
    let leftover_plank = Plank {
    length: self.length - measure,
    endpiece: false,
    new: false,
    };
    // Trim self to the new length
    self.length = measure;
    // In case the plank was new, mark it as cut
    if self.new {
    self.new = false;
    }
    // Return the original plank and the leftover
    (self, leftover_plank)
    }
    }
  • edit in src/main.rs at line 15
    [3.998][3.38:118](),[3.38][3.38:118](),[3.118][3.999:1018](),[3.1018][3.118:136](),[3.118][3.118:136](),[3.136][3.1019:1212]()
    // Define a row structure
    #[derive(Debug)]
    struct Row {
    planks: Vec<Plank>,
    coverage: u32,
    full: bool,
    }
    impl Default for Row {
    /// Create a new, empty row
    fn default() -> Self {
    Self {
    planks: vec![],
    coverage: 0,
    full: false,
    }
    }
    }
  • edit in src/main.rs at line 16
    [3.137][3.137:448](),[3.448][3.283:286](),[3.283][3.283:286](),[3.286][3.449:529](),[3.529][3.1213:1232](),[3.1232][3.529:551](),[3.529][3.529:551](),[3.551][3.0:197](),[3.197][3.551:869](),[3.551][3.551:869]()
    impl Row {
    /// Is this row considered complete?
    fn is_full(&self) -> bool {
    self.full
    }
    /// Get the number of plank rows for this floor
    fn rows_count(&self) -> usize {
    self.planks.len()
    }
    fn add(&mut self, plank: Plank) -> () {
    self.planks.push(plank);
    }
    }
    // Define a floor structure
    #[derive(Debug)]
    struct Floor {
    rows: Vec<Row>,
    coverage: u32,
    complete: bool,
    }
    impl Default for Floor {
    /// Create a new, empty row
    fn default() -> Self {
    Self {
    rows: vec![],
    coverage: 0,
    complete: false,
    }
    }
    }
    impl Floor {
    /// Is this floor considered complete?
    fn is_complete(&self) -> bool {
    self.complete
    }
    /// Get the number of plank rows for this floor
    fn rows_count(&self) -> usize {
    self.rows.len()
    }
    fn add(&mut self, row: Row) -> () {
    self.rows.push(row);
    }
    }
  • edit in src/main.rs at line 36
    [3.1349]
    [3.1349]
    floor.coverage += PLANKWIDTH;
  • replacement in src/main.rs at line 52
    [3.1552][3.1552:1796]()
    while row.coverage <= ROOMLENGTH {
    if plank.length > ROOMLENGTH {
    // Planks are longer than the room, need to cut them
    //let leftover;
    let (plankpart, leftover) = plank.cut_to_length(ROOMLENGTH);
    [3.1552]
    [3.1796]
    while row.get_coverage() <= ROOMLENGTH {
    // TODO should also check if the plank is too short, then WARN or take a new
    if plank.length() < 200 {
    // Take a new plank and continue
    let newplank: Plank = Default::default();
    plank = newplank;
    }
    if plank.length() > ROOMLENGTH - row.get_coverage() {
    // Cut the plank to length
    let (plankpart, leftover) = plank.cut_to_length(ROOMLENGTH - row.get_coverage());
  • replacement in src/main.rs at line 66
    [3.149][3.149:197]()
    row.coverage += plankpart.length();
    [3.149]
    [3.1857]
    row.add_coverage(plankpart.length());
  • edit in src/main.rs at line 71
    [3.1969]
    [3.1969]
    } else if plank.length() == ROOMLENGTH - row.get_coverage() {
    // If the plank is the same length as the room, no need to cut
    // Use the whole plank, add to row coverage
    row.add_coverage(plank.length());
    row.add(plank);
    // Take a new plank and continue
    let newplank: Plank = Default::default();
    plank = newplank;
    } else {
    // Plank is shorter than the room, need multiple
    // Use the whole plank, add to row coverage
    row.add_coverage(plank.length());
    row.add(plank);
    // Take a new plank and continue
    let newplank: Plank = Default::default();
    plank = newplank;
  • edit in src/main.rs at line 92
    [3.1979]
    [3.1418]
    println!("Row: {:#?}", row);
  • edit in src/main.rs at line 94
    [3.1424]
    [3.1980]
    // The row is now full
    row.set_full();
    // Annotate the planks in the beginning and end as endpieces
    if let Some(plank) = row.planks().first_mut() {
    plank.set_endpiece()
    }
    if let Some(plank) = row.planks().last_mut() {
    plank.set_endpiece()
    }