github.com/maypok86/otter@v1.2.1/internal/generated/node/manager.go (about) 1 // Code generated by NodeGenerator. DO NOT EDIT. 2 3 // Package node is a generated generator package. 4 package node 5 6 import ( 7 "strings" 8 "unsafe" 9 ) 10 11 const ( 12 unknownQueueType uint8 = iota 13 smallQueueType 14 mainQueueType 15 16 maxFrequency uint8 = 3 17 ) 18 19 const ( 20 aliveState uint32 = iota 21 deadState 22 ) 23 24 // Node is a cache entry. 25 type Node[K comparable, V any] interface { 26 // Key returns the key. 27 Key() K 28 // Value returns the value. 29 Value() V 30 // AsPointer returns the node as a pointer. 31 AsPointer() unsafe.Pointer 32 // Prev returns the previous node in the eviction policy. 33 Prev() Node[K, V] 34 // SetPrev sets the previous node in the eviction policy. 35 SetPrev(v Node[K, V]) 36 // Next returns the next node in the eviction policy. 37 Next() Node[K, V] 38 // SetNext sets the next node in the eviction policy. 39 SetNext(v Node[K, V]) 40 // PrevExp returns the previous node in the expiration policy. 41 PrevExp() Node[K, V] 42 // SetPrevExp sets the previous node in the expiration policy. 43 SetPrevExp(v Node[K, V]) 44 // NextExp returns the next node in the expiration policy. 45 NextExp() Node[K, V] 46 // SetNextExp sets the next node in the expiration policy. 47 SetNextExp(v Node[K, V]) 48 // HasExpired returns true if node has expired. 49 HasExpired() bool 50 // Expiration returns the expiration time. 51 Expiration() uint32 52 // Cost returns the cost of the node. 53 Cost() uint32 54 // IsAlive returns true if the entry is available in the hash-table. 55 IsAlive() bool 56 // Die sets the node to the dead state. 57 Die() 58 // Frequency returns the frequency of the node. 59 Frequency() uint8 60 // IncrementFrequency increments the frequency of the node. 61 IncrementFrequency() 62 // DecrementFrequency decrements the frequency of the node. 63 DecrementFrequency() 64 // ResetFrequency resets the frequency. 65 ResetFrequency() 66 // MarkSmall sets the status to the small queue. 67 MarkSmall() 68 // IsSmall returns true if node is in the small queue. 69 IsSmall() bool 70 // MarkMain sets the status to the main queue. 71 MarkMain() 72 // IsMain returns true if node is in the main queue. 73 IsMain() bool 74 // Unmark sets the status to unknown. 75 Unmark() 76 } 77 78 func Equals[K comparable, V any](a, b Node[K, V]) bool { 79 if a == nil { 80 return b == nil || b.AsPointer() == nil 81 } 82 if b == nil { 83 return a.AsPointer() == nil 84 } 85 return a.AsPointer() == b.AsPointer() 86 } 87 88 type Config struct { 89 WithExpiration bool 90 WithCost bool 91 } 92 93 type Manager[K comparable, V any] struct { 94 create func(key K, value V, expiration, cost uint32) Node[K, V] 95 fromPointer func(ptr unsafe.Pointer) Node[K, V] 96 } 97 98 func NewManager[K comparable, V any](c Config) *Manager[K, V] { 99 var sb strings.Builder 100 sb.WriteString("b") 101 if c.WithExpiration { 102 sb.WriteString("e") 103 } 104 if c.WithCost { 105 sb.WriteString("c") 106 } 107 nodeType := sb.String() 108 m := &Manager[K, V]{} 109 110 switch nodeType { 111 case "bec": 112 m.create = NewBEC[K, V] 113 m.fromPointer = CastPointerToBEC[K, V] 114 case "bc": 115 m.create = NewBC[K, V] 116 m.fromPointer = CastPointerToBC[K, V] 117 case "be": 118 m.create = NewBE[K, V] 119 m.fromPointer = CastPointerToBE[K, V] 120 case "b": 121 m.create = NewB[K, V] 122 m.fromPointer = CastPointerToB[K, V] 123 default: 124 panic("not valid nodeType") 125 } 126 return m 127 } 128 129 func (m *Manager[K, V]) Create(key K, value V, expiration, cost uint32) Node[K, V] { 130 return m.create(key, value, expiration, cost) 131 } 132 133 func (m *Manager[K, V]) FromPointer(ptr unsafe.Pointer) Node[K, V] { 134 return m.fromPointer(ptr) 135 } 136 137 func minUint8(a, b uint8) uint8 { 138 if a < b { 139 return a 140 } 141 142 return b 143 }