Skip to content
This repository was archived by the owner on Nov 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
OrderedMap
Copyright 2025 bibenga

This product includes software developed at
The Apache Software Foundation (https://www.apache.org/),
originally part of Apache Commons Collections.
33 changes: 16 additions & 17 deletions internal/hash/hash.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
// Portions Copyright 2025 bibenga
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hash

import "orderedmap"

func FnvIntHash32[T orderedmap.Integer](x T) uint32 {
const (
offset = 2166136261
prime = 16777619
)

x64 := uint64(x)
hash := uint32(offset)
for i := range 8 {
b := byte(x64 >> (i * 8))
hash ^= uint32(b)
hash *= prime
}
return hash
}

func MapHash32(h uint32) uint32 {
h += ^(h << 9)
h ^= h >> 14
h += h << 4
h ^= h >> 10
return h
// return FnvIntHash32(h)
}

func MapHashIndex(hashCode uint32, dataSize int) int {
Expand Down
3 changes: 2 additions & 1 deletion internal/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// Copyright 2025 bibenga
package utils

import (
"fmt"
"orderedmap"
)

func ToString[K, V any](name string, m orderedmap.Map[K, V]) string {
func MapToString[K, V any](name string, m orderedmap.Map[K, V]) string {
str := name + "["
first := true
for k, v := range m.Items() {
Expand Down
4 changes: 3 additions & 1 deletion intlinkedhashmap/intlinkedhashmap.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Portions of this file are based on Apache Commons Collections,
// licensed under the Apache License, Version 2.0.
package intlinkedhashmap

import (
Expand Down Expand Up @@ -333,5 +335,5 @@ func (m *IntLinkedHashMap[K, V]) Items() iter.Seq2[K, V] {
}

func (m *IntLinkedHashMap[K, V]) String() string {
return utils.ToString("IntLinkedHashMap", m)
return utils.MapToString("IntLinkedHashMap", m)
}
18 changes: 17 additions & 1 deletion linkedhashmap/linkedhashmap.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
// Portions Copyright 2025 bibenga
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package linkedhashmap

import (
Expand Down Expand Up @@ -319,5 +335,5 @@ func (m *LinkedHashMap[K, V]) Items() iter.Seq2[K, V] {
}

func (m *LinkedHashMap[K, V]) String() string {
return utils.ToString("LinkedHashMap", m)
return utils.MapToString("LinkedHashMap", m)
}
4 changes: 3 additions & 1 deletion linkedmap/linkedmap.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright 2025 bibenga
// Inspired by Apache Commons Collections (Apache License 2.0)
package linkedmap

import (
Expand Down Expand Up @@ -135,5 +137,5 @@ func (m *LinkedMap[K, V]) Items() iter.Seq2[K, V] {
}

func (m *LinkedMap[K, V]) String() string {
return utils.ToString("LinkedMap", m)
return utils.MapToString("LinkedMap", m)
}
2 changes: 2 additions & 0 deletions maps.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// This implementation is inspired by Apache Commons Collections
// (licensed under the Apache License, Version 2.0).
package orderedmap

import "iter"
Expand Down
2 changes: 2 additions & 0 deletions tests/benchmark1_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright 2025 bibenga
// Inspired by Apache Commons Collections (Apache License 2.0)
package tests

import (
Expand Down
2 changes: 2 additions & 0 deletions tests/benchmark2_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright 2025 bibenga
// Inspired by Apache Commons Collections (Apache License 2.0)
package tests

import (
Expand Down
22 changes: 2 additions & 20 deletions tests/general_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright 2025 bibenga
// Inspired by Apache Commons Collections (Apache License 2.0)
package tests

import (
"encoding/binary"
"hash/fnv"
"orderedmap"
"orderedmap/internal/hash"
"orderedmap/intlinkedhashmap"
Expand All @@ -14,24 +14,6 @@ import (
type Factory func() orderedmap.Map[int, int]
type StdFactory func() map[int]int

func TestFnvIntHash32(t *testing.T) {
values := []int64{0, 1, 42, 12345, -1, -123456789}
for _, v := range values {
// we count through native fnv.New32a()
h := fnv.New32a()
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, uint64(v))
h.Write(buf)
want := h.Sum32()

got := hash.FnvIntHash32(v)

if got != want {
t.Errorf("fnvInt(%d) = %d, want %d", v, got, want)
}
}
}

func TestIntLinkedHashMap(t *testing.T) {
factory := func() orderedmap.Map[int, int] {
return intlinkedhashmap.NewDefault[int, int]()
Expand Down
Loading