1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use crate::{
    grid::config::Entity,
    grid::records::{ExactRecords, Records},
    settings::object::Object,
};

/// Frame includes cells which are on the edges of each side.
/// Therefore it's [`Object`] implementation returns a subset of cells which are present in frame.
#[derive(Debug)]
pub struct Frame;

impl<I> Object<I> for Frame
where
    I: Records + ExactRecords,
{
    type Iter = FrameIter;

    fn cells(&self, records: &I) -> Self::Iter {
        FrameIter::new(records.count_rows(), records.count_columns())
    }
}

/// An [`Iterator`] which goes goes over all cell on a frame of a [`Table`].
///
/// [`Table`]: crate::Table
#[derive(Debug)]
pub struct FrameIter {
    rows: usize,
    cols: usize,
    row: usize,
    col: usize,
}

impl FrameIter {
    const fn new(count_rows: usize, count_columns: usize) -> Self {
        Self {
            rows: count_rows,
            cols: count_columns,
            row: 0,
            col: 0,
        }
    }
}

impl Iterator for FrameIter {
    type Item = Entity;

    fn next(&mut self) -> Option<Self::Item> {
        if self.cols == 0 || self.rows == 0 {
            return None;
        }

        if self.row == self.rows {
            return None;
        }

        let row = self.row;
        let col = self.col;

        self.col += 1;

        if self.col == self.cols {
            self.row += 1;
            self.col = 0;
        }

        Some(Entity::Cell(row, col))
    }
}