reward_calculation_types.gno
2.86 Kb ยท 119 lines
1package v1
2
3import (
4 "strconv"
5 "strings"
6
7 "gno.land/p/nt/avl"
8 "gno.land/p/nt/ufmt"
9)
10
11// EncodeUint converts a uint64 number into a zero-padded 20-character string.
12//
13// Parameters:
14// - num (uint64): The number to encode.
15//
16// Returns:
17// - string: A zero-padded string representation of the number.
18//
19// Example:
20// Input: 12345
21// Output: "00000000000000012345"
22func EncodeUint(num uint64) string {
23 // Convert the value to a decimal string.
24 s := strconv.FormatUint(num, 10)
25
26 // Zero-pad to a total length of 20 characters.
27 zerosNeeded := 20 - len(s)
28 return strings.Repeat("0", zerosNeeded) + s
29}
30
31func EncodeInt64(num int64) string {
32 if num < 0 {
33 panic(ufmt.Sprintf("negative value not supported: %d", num))
34 }
35 return EncodeUint(uint64(num))
36}
37
38// DecodeUint converts a zero-padded string back into a uint64 number.
39//
40// Parameters:
41// - s (string): The zero-padded string.
42//
43// Returns:
44// - uint64: The decoded number.
45//
46// Panics:
47// - If the string cannot be parsed into a uint64.
48//
49// Example:
50// Input: "00000000000000012345"
51// Output: 12345
52func DecodeUint(s string) uint64 {
53 num, err := strconv.ParseUint(s, 10, 64)
54 if err != nil {
55 panic(err)
56 }
57 return num
58}
59
60func DecodeInt64(s string) int64 {
61 num, err := strconv.ParseInt(s, 10, 64)
62 if err != nil {
63 panic(err)
64 }
65 return num
66}
67
68// UintTree is a wrapper around an AVL tree for storing block timestamps as strings.
69// Since block timestamps are defined as int64, we take int64 and convert it to uint64 for the tree.
70//
71// Methods:
72// - Get: Retrieves a value associated with a uint64 key.
73// - set: Stores a value with a uint64 key.
74// - Has: Checks if a uint64 key exists in the tree.
75// - remove: Removes a uint64 key and its associated value.
76// - Iterate: Iterates over keys and values in a range.
77// - ReverseIterate: Iterates in reverse order over keys and values in a range.
78type UintTree struct {
79 tree *avl.Tree // blockTimestamp -> any
80}
81
82// NewUintTree creates a new UintTree instance.
83func NewUintTree() *UintTree {
84 return &UintTree{
85 tree: avl.NewTree(),
86 }
87}
88
89func (self *UintTree) Get(key int64) (any, bool) {
90 v, ok := self.tree.Get(EncodeInt64(key))
91 if !ok {
92 return nil, false
93 }
94 return v, true
95}
96
97func (self *UintTree) set(key int64, value any) {
98 self.tree.Set(EncodeInt64(key), value)
99}
100
101func (self *UintTree) Has(key int64) bool {
102 return self.tree.Has(EncodeInt64(key))
103}
104
105func (self *UintTree) remove(key int64) {
106 self.tree.Remove(EncodeInt64(key))
107}
108
109func (self *UintTree) Iterate(start, end int64, fn func(key int64, value any) bool) {
110 self.tree.Iterate(EncodeInt64(start), EncodeInt64(end), func(key string, value any) bool {
111 return fn(DecodeInt64(key), value)
112 })
113}
114
115func (self *UintTree) ReverseIterate(start, end int64, fn func(key int64, value any) bool) {
116 self.tree.ReverseIterate(EncodeInt64(start), EncodeInt64(end), func(key string, value any) bool {
117 return fn(DecodeInt64(key), value)
118 })
119}