Skip to content

Commit

Permalink
Export SetCell and GetCell. (#92)
Browse files Browse the repository at this point in the history
* Export SetCells.

* Make SetCell panic on trying to set non-nil cell. Add internal setCell that doesn't have this check.
  • Loading branch information
adlerjohn authored Jun 14, 2022
1 parent 80b016d commit ec818ae
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
21 changes: 17 additions & 4 deletions datasquare.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rsmt2d

import (
"errors"
"fmt"
"math"
)

Expand Down Expand Up @@ -117,7 +118,7 @@ func (ds *dataSquare) rowSlice(x uint, y uint, length uint) [][]byte {
}

// row returns a row slice.
// Do not modify this slice directly, instead use setCell.
// Do not modify this slice directly, instead use SetCell.
func (ds *dataSquare) row(x uint) [][]byte {
return ds.rowSlice(x, 0, ds.width)
}
Expand All @@ -144,7 +145,7 @@ func (ds *dataSquare) colSlice(x uint, y uint, length uint) [][]byte {
}

// col returns a column slice.
// Do not modify this slice directly, instead use setCell.
// Do not modify this slice directly, instead use SetCell.
func (ds *dataSquare) col(y uint) [][]byte {
return ds.colSlice(0, y, ds.width)
}
Expand Down Expand Up @@ -231,8 +232,8 @@ func (ds *dataSquare) getColRoot(y uint) []byte {
return tree.Root()
}

// getCell returns a copy of single chunk at a specific cell.
func (ds *dataSquare) getCell(x uint, y uint) []byte {
// GetCell returns a copy of a specific cell.
func (ds *dataSquare) GetCell(x uint, y uint) []byte {
if ds.squareRow[x][y] == nil {
return nil
}
Expand All @@ -241,6 +242,18 @@ func (ds *dataSquare) getCell(x uint, y uint) []byte {
return cell
}

// SetCell sets a specific cell. Cell to set must be `nil`.
// Panics if attempting to set a cell that is not `nil`.
func (ds *dataSquare) SetCell(x uint, y uint, newChunk []byte) {
if ds.squareRow[x][y] != nil {
panic(fmt.Sprintf("cannot set cell (%d, %d) as it already has a value %x", x, y, ds.squareRow[x][y]))
}
ds.squareRow[x][y] = newChunk
ds.squareCol[y][x] = newChunk
ds.resetRoots()
}

// setCell sets a specific cell.
func (ds *dataSquare) setCell(x uint, y uint, newChunk []byte) {
ds.squareRow[x][y] = newChunk
ds.squareCol[y][x] = newChunk
Expand Down
2 changes: 1 addition & 1 deletion extendeddatacrossword.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func (eds *ExtendedDataSquare) rowRangeNoMissingData(r, start, end uint) bool {

func (eds *ExtendedDataSquare) colRangeNoMissingData(c, start, end uint) bool {
for r := start; r <= end && r < eds.width; r++ {
if eds.squareRow[r][c] == nil {
if eds.squareRow[r][c] == nil {
return false
}
}
Expand Down
8 changes: 4 additions & 4 deletions extendeddatacrossword_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ func TestRepairExtendedDataSquare(t *testing.T) {
if err != nil {
t.Errorf("unexpected err while repairing data square: %v, codec: :%s", err, codecName)
} else {
assert.Equal(t, original.getCell(0, 0), ones)
assert.Equal(t, original.getCell(0, 1), twos)
assert.Equal(t, original.getCell(1, 0), threes)
assert.Equal(t, original.getCell(1, 1), fours)
assert.Equal(t, original.GetCell(0, 0), ones)
assert.Equal(t, original.GetCell(0, 1), twos)
assert.Equal(t, original.GetCell(1, 0), threes)
assert.Equal(t, original.GetCell(1, 1), fours)
}

flattened = original.flattened()
Expand Down

0 comments on commit ec818ae

Please sign in to comment.