Reorganize math package into subpackages

- Create math/big for big.Int utilities (HexOrDecimal256, U256, parsing)
- Create math/safe for overflow-safe arithmetic (SafeAdd, SafeMul, etc.)
- Move averager files to root package level
- Add re-exports in root package for backwards compatibility
- Update README with new package structure documentation
- Remove old math/meter directory (consolidated elsewhere)
- Remove redundant math/ subdirectory structure
This commit is contained in:
Zach Kelling
2026-01-03 18:22:33 -08:00
parent 7870d52f6f
commit fd27f2ecb0
25 changed files with 270 additions and 614 deletions
+11 -9
View File
@@ -3,11 +3,13 @@
package heap
import (
"container/heap"
import "container/heap"
luxmath "github.com/luxfi/math"
)
// zero returns the zero value of any type T.
func zero[T any]() T {
var z T
return z
}
var _ heap.Interface = (*indexedQueue[int, int])(nil)
@@ -47,12 +49,12 @@ func (m *Map[K, V]) Push(k K, v V) (V, bool) {
}
heap.Push(m.queue, entry[K, V]{k: k, v: v})
return luxmath.Zero[V](), false
return zero[V](), false
}
func (m *Map[K, V]) Pop() (K, V, bool) {
if m.Len() == 0 {
return luxmath.Zero[K](), luxmath.Zero[V](), false
return zero[K](), zero[V](), false
}
popped := heap.Pop(m.queue).(entry[K, V])
@@ -61,7 +63,7 @@ func (m *Map[K, V]) Pop() (K, V, bool) {
func (m *Map[K, V]) Peek() (K, V, bool) {
if m.Len() == 0 {
return luxmath.Zero[K](), luxmath.Zero[V](), false
return zero[K](), zero[V](), false
}
entry := m.queue.entries[0]
@@ -77,7 +79,7 @@ func (m *Map[K, V]) Remove(k K) (V, bool) {
removed := heap.Remove(m.queue, i).(entry[K, V])
return removed.v, true
}
return luxmath.Zero[V](), false
return zero[V](), false
}
func (m *Map[K, V]) Contains(k K) bool {
@@ -90,7 +92,7 @@ func (m *Map[K, V]) Get(k K) (V, bool) {
got := m.queue.entries[i]
return got.v, true
}
return luxmath.Zero[V](), false
return zero[V](), false
}
func (m *Map[K, V]) Fix(k K) {
+4 -8
View File
@@ -3,11 +3,7 @@
package heap
import (
"container/heap"
luxmath "github.com/luxfi/math"
)
import "container/heap"
var _ heap.Interface = (*queue[int])(nil)
@@ -44,7 +40,7 @@ func (q *Queue[T]) Push(t T) {
func (q *Queue[T]) Pop() (T, bool) {
if q.Len() == 0 {
return luxmath.Zero[T](), false
return zero[T](), false
}
return heap.Pop(q.queue).(T), true
@@ -52,7 +48,7 @@ func (q *Queue[T]) Pop() (T, bool) {
func (q *Queue[T]) Peek() (T, bool) {
if q.Len() == 0 {
return luxmath.Zero[T](), false
return zero[T](), false
}
return q.queue.entries[0], true
@@ -87,7 +83,7 @@ func (q *queue[T]) Pop() any {
end := len(q.entries) - 1
popped := q.entries[end]
q.entries[end] = luxmath.Zero[T]()
q.entries[end] = zero[T]()
q.entries = q.entries[:end]
return popped