Skip to content

Commit

Permalink
Clean
Browse files Browse the repository at this point in the history
  • Loading branch information
danon committed Nov 24, 2023
1 parent e3af598 commit 45e6c57
Showing 1 changed file with 42 additions and 9 deletions.
51 changes: 42 additions & 9 deletions src/Internal/View/ReindexedKeysDataFrame.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,21 @@ public function dataset(): array
*/
private function reindexedRows(array $dataRows): array
{
for ($i = 0; $i < $this->widestRowWidth($dataRows); $i++) {
$sequence = 0;
foreach ($dataRows as $row) {
if (isset($row->keys[$i])) {
if (!$row->isAssociative($i)) {
$row->keys[$i] = $sequence++;
}
}
}
foreach ($this->columnIndices($dataRows) as $columnIndex) {
$this->reindexColumn($dataRows, $columnIndex);
}
return $dataRows;
}

/**
* @param DataRow[] $dataRows
* @return \Iterator
*/
private function columnIndices(array $dataRows): iterable
{
return \range(0, $this->widestRowWidth($dataRows));
}

/**
* @param DataRow[] $dataRows
* @return int
Expand All @@ -52,4 +54,35 @@ private function widestRowWidth(array $dataRows): int
return \count($row->keys);
}, $dataRows));
}

/**
* @param DataRow[] $dataRows
* @param int $columnIndex
* @return void
*/
private function reindexColumn(array $dataRows, int $columnIndex): void
{
$sequence = 0;
foreach ($this->rowsWithSequentialKeyAt($dataRows, $columnIndex) as $row) {
$row->keys[$columnIndex] = $sequence++;
}
}

/**
* @param DataRow[] $dataRows
* @param int $index
* @return \Iterator
*/
private function rowsWithSequentialKeyAt(array $dataRows, int $index): \Iterator
{
foreach ($dataRows as $row) {
if (!isset($row->keys[$index])) {
continue;
}
if ($row->isAssociative($index)) {
continue;
}
yield $row;
}
}
}

0 comments on commit 45e6c57

Please sign in to comment.