diff --git a/heap/heap.go b/heap/heap.go index 35c27ff..feab4c6 100644 --- a/heap/heap.go +++ b/heap/heap.go @@ -2,15 +2,18 @@ package heap import ( "474420502.top/eson/structure/compare" + "474420502.top/eson/structure/sparse_array/array3" ) type Heap struct { size int cap int - elements []interface{} + elements *array3.Array3 Compare compare.Compare } func New(Compare compare.Compare) *Heap { - return &Heap{Compare: Compare, cap: 8} + h := &Heap{Compare: Compare, cap: 8} + h.elements = array3.NewWithCap(4, 4, 4) + return h } diff --git a/sparse_array/array2/array2.go b/sparse_array/array2/array2.go index 273ae4f..9611e90 100644 --- a/sparse_array/array2/array2.go +++ b/sparse_array/array2/array2.go @@ -5,6 +5,8 @@ type Array2 struct { ysize int xsize int data [][]interface{} + + cap int } func New() *Array2 { @@ -15,6 +17,8 @@ func NewWithCap(ysize, xsize int) *Array2 { arr := &Array2{ysize: ysize, xsize: xsize} arr.sizes = make([]int, arr.ysize, arr.ysize) arr.data = make([][]interface{}, arr.ysize, arr.ysize) + + arr.cap = arr.ysize * arr.xsize return arr } @@ -58,6 +62,10 @@ func (arr *Array2) Values() []interface{} { return result } +func (arr *Array2) Cap() int { + return arr.cap +} + func (arr *Array2) Grow(size int) { arr.ysize += size temp := make([][]interface{}, arr.ysize, arr.ysize) @@ -67,6 +75,8 @@ func (arr *Array2) Grow(size int) { tempsizes := make([]int, arr.ysize, arr.ysize) copy(tempsizes, arr.sizes) arr.sizes = tempsizes + + arr.cap = arr.ysize * arr.xsize } func (arr *Array2) Set(idx int, value interface{}) { diff --git a/sparse_array/array3/array3.go b/sparse_array/array3/array3.go index b87a50a..d232c55 100644 --- a/sparse_array/array3/array3.go +++ b/sparse_array/array3/array3.go @@ -8,6 +8,8 @@ type Array3 struct { ysize int xsize int data [][][]interface{} + + cap int } func New() *Array3 { @@ -26,6 +28,8 @@ func NewWithCap(zsize, ysize, xsize int) *Array3 { arr.xyproduct = arr.ysize * arr.xsize arr.data = make([][][]interface{}, arr.zsize, arr.zsize) + + arr.cap = arr.zsize * arr.xyproduct return arr } @@ -87,8 +91,12 @@ func (arr *Array3) Values() []interface{} { return result } +func (arr *Array3) Cap() int { + return arr.cap +} + func (arr *Array3) Grow(size int) { - arr.ysize += size + arr.zsize += size temp := make([][][]interface{}, arr.zsize, arr.zsize) copy(temp, arr.data) arr.data = temp @@ -100,6 +108,8 @@ func (arr *Array3) Grow(size int) { tempxsizes := make([][]int, arr.ysize, arr.ysize) copy(tempxsizes, arr.xsizes) arr.xsizes = tempxsizes + + arr.cap = arr.zsize * arr.xyproduct } func (arr *Array3) Set(idx int, value interface{}) { diff --git a/sparse_array/arrayn/arrayn.go b/sparse_array/arrayn/arrayn.go index 54252d3..f29317f 100644 --- a/sparse_array/arrayn/arrayn.go +++ b/sparse_array/arrayn/arrayn.go @@ -23,6 +23,8 @@ type ArrayN struct { dimN int data *Node // []*Node + + cap int } func New() *ArrayN { @@ -40,6 +42,10 @@ func NewWithCap(dims ...int) *ArrayN { arr.product[i] = pvalue } // arr.data = make([]*Node, arr.dims[0], arr.dims[0]) + arr.cap = 1 + for _, d := range arr.dims { + arr.cap *= d + } return arr } @@ -107,6 +113,10 @@ func (arr *ArrayN) set(curDim int, curidx int, pdata **Node, parent *Node) (*Nod return arr.set(curDim-1, nidx, &cur.data.([]*Node)[dimindex], cur) } +func (arr *ArrayN) Cap() int { + return arr.cap +} + func (arr *ArrayN) Grow(size int) { arr.dims[0] += size @@ -121,6 +131,11 @@ func (arr *ArrayN) Grow(size int) { newData := make([]*Node, arr.dims[0], arr.dims[0]) copy(newData, tempdata) arr.data.data = newData + + arr.cap = 1 + for _, d := range arr.dims { + arr.cap *= d + } } func (arr *ArrayN) Set(idx int, value interface{}) {