github.com/matrixorigin/matrixone@v1.2.0/pkg/container/hashtable/fixed_map.go (about)

     1  // Copyright 2021 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package hashtable
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    19  )
    20  
    21  type FixedMap struct {
    22  	cellCnt uint32
    23  	elemCnt uint32
    24  	cells   []uint64
    25  }
    26  
    27  type FixedMapIterator struct {
    28  	table *FixedMap
    29  	idx   uint32
    30  }
    31  
    32  func (ht *FixedMap) Init(cellCnt uint32) {
    33  	ht.cellCnt = cellCnt
    34  	ht.cells = make([]uint64, cellCnt)
    35  }
    36  
    37  func (ht *FixedMap) Insert(key uint32) uint64 {
    38  	value := ht.cells[key]
    39  	if value == 0 {
    40  		ht.elemCnt++
    41  		value = uint64(ht.elemCnt)
    42  		ht.cells[key] = value
    43  	}
    44  	return value
    45  }
    46  
    47  func (ht *FixedMap) Cells() []uint64 {
    48  	return ht.cells
    49  }
    50  
    51  func (ht *FixedMap) Cardinality() uint64 {
    52  	return uint64(ht.elemCnt)
    53  }
    54  
    55  func (it *FixedMapIterator) Init(ht *FixedMap) {
    56  	it.table = ht
    57  	it.idx = 0
    58  }
    59  
    60  func (it *FixedMapIterator) Next() (key uint32, value uint64, err error) {
    61  	for it.idx < it.table.cellCnt && it.table.cells[it.idx] == 0 {
    62  		it.idx++
    63  	}
    64  
    65  	if it.idx == it.table.cellCnt {
    66  		err = moerr.NewInternalErrorNoCtx("out of range")
    67  		return
    68  	}
    69  
    70  	key = it.idx
    71  	value = it.table.cells[key]
    72  
    73  	return
    74  }