From ec818aef816a56029f55c90ee2045975f3372092 Mon Sep 17 00:00:00 2001 From: John Adler Date: Tue, 14 Jun 2022 13:25:44 -0400 Subject: [PATCH] Export `SetCell` and `GetCell`. (#92) * Export SetCells. * Make SetCell panic on trying to set non-nil cell. Add internal setCell that doesn't have this check. --- datasquare.go | 21 +++++++++++++++++---- extendeddatacrossword.go | 2 +- extendeddatacrossword_test.go | 8 ++++---- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/datasquare.go b/datasquare.go index 5c0a3cf..9af39e1 100644 --- a/datasquare.go +++ b/datasquare.go @@ -2,6 +2,7 @@ package rsmt2d import ( "errors" + "fmt" "math" ) @@ -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) } @@ -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) } @@ -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 } @@ -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 diff --git a/extendeddatacrossword.go b/extendeddatacrossword.go index e9c9558..e450b35 100644 --- a/extendeddatacrossword.go +++ b/extendeddatacrossword.go @@ -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 } } diff --git a/extendeddatacrossword_test.go b/extendeddatacrossword_test.go index ed23729..f109fd1 100644 --- a/extendeddatacrossword_test.go +++ b/extendeddatacrossword_test.go @@ -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()