github.com/mavryk-network/mvgo@v1.19.9/hash/hashmap.go (about)

     1  // Copyright (c) 2018 - 2023 Blockwatch Data Inc.
     2  // Author: alex@blockwatch.cc
     3  
     4  package hash
     5  
     6  import (
     7  	"bytes"
     8  )
     9  
    10  type HashMap map[uint64][]byte
    11  
    12  func NewHashMap() HashMap {
    13  	return make(HashMap)
    14  }
    15  
    16  func (m *HashMap) Add(buf []byte) int {
    17  	(*m)[Hash64(buf)] = buf
    18  	return len(*m)
    19  }
    20  
    21  func (m *HashMap) Remove(buf []byte) int {
    22  	h := Hash64(buf)
    23  	if b, ok := (*m)[h]; ok {
    24  		if bytes.Equal(b, buf) {
    25  			delete((*m), h)
    26  		}
    27  	}
    28  	return len(*m)
    29  }
    30  
    31  func (m HashMap) Contains(buf []byte) bool {
    32  	b, ok := m[Hash64(buf)]
    33  	return ok && bytes.Equal(b, buf)
    34  }