导表临时实现

This commit is contained in:
kercylan98 2023-04-26 16:09:58 +08:00
parent 1d6b067261
commit a7f86ca887
6 changed files with 138 additions and 16 deletions

View File

@ -0,0 +1,7 @@
package configuration
type Configuration interface {
GetName() string
GetFields() []Field
AddField(field Field)
}

View File

@ -0,0 +1,7 @@
package configuration
type Field interface {
GetID() int
GetName() string
GetType() FieldType
}

View File

@ -0,0 +1,36 @@
package configuration
import "fmt"
const (
FieldTypeString = FieldType(iota)
FieldTypeInt
FieldTypeInt64
FieldTypeFloat32
FieldTypeFloat64
FieldTypeByte
FieldTypeBool
)
type FieldType byte
func (slf FieldType) String() string {
switch slf {
case FieldTypeString:
return "string"
case FieldTypeInt:
return "int"
case FieldTypeInt64:
return "int64"
case FieldTypeFloat32:
return "float32"
case FieldTypeFloat64:
return "float64"
case FieldTypeByte:
return "byte"
case FieldTypeBool:
return "bool"
}
panic(fmt.Errorf("not support field type %v", byte(slf)))
}

View File

@ -0,0 +1,30 @@
package golang
import (
"minotaur/exporter/configuration"
"minotaur/utils/hash"
)
func NewConfiguration(name string) *Configuration {
return &Configuration{
name: name,
fields: hash.NewSortMap[int, configuration.Field](),
}
}
type Configuration struct {
name string
fields *hash.SortMap[int, configuration.Field]
}
func (slf *Configuration) GetName() string {
return slf.name
}
func (slf *Configuration) GetFields() []configuration.Field {
return slf.fields.ToSliceSort()
}
func (slf *Configuration) AddField(field configuration.Field) {
slf.fields.Set(field.GetID(), field)
}

View File

@ -0,0 +1,30 @@
package golang
import (
"minotaur/exporter/configuration"
)
func NewField(name string, fieldType configuration.FieldType) *Field {
return &Field{
name: name,
fieldType: fieldType,
}
}
type Field struct {
id int
name string
fieldType configuration.FieldType
}
func (slf *Field) GetID() int {
return slf.id
}
func (slf *Field) GetName() string {
return slf.name
}
func (slf *Field) GetType() configuration.FieldType {
return slf.fieldType
}

View File

@ -4,6 +4,14 @@ import (
"sort" "sort"
) )
func NewSortMap[K comparable, V any]() *SortMap[K, V] {
return &SortMap[K, V]{
m: map[K]V{},
s: map[int]K{},
r: map[K]int{},
}
}
// SortMap 有序的 map 实现 // SortMap 有序的 map 实现
type SortMap[K comparable, V any] struct { type SortMap[K comparable, V any] struct {
i int i int
@ -13,16 +21,6 @@ type SortMap[K comparable, V any] struct {
} }
func (slf *SortMap[K, V]) Set(key K, value V) { func (slf *SortMap[K, V]) Set(key K, value V) {
if slf.m == nil {
slf.m = make(map[K]V)
}
if slf.s == nil {
slf.s = make(map[int]K)
}
if slf.r == nil {
slf.r = make(map[K]int)
}
if i, exist := slf.r[key]; exist { if i, exist := slf.r[key]; exist {
slf.s[i] = key slf.s[i] = key
slf.m[key] = value slf.m[key] = value
@ -42,12 +40,12 @@ func (slf *SortMap[K, V]) Del(key K) {
} }
} }
func (slf SortMap[K, V]) Get(key K) V { func (slf *SortMap[K, V]) Get(key K) V {
v := slf.m[key] v := slf.m[key]
return v return v
} }
func (slf SortMap[K, V]) For(handle func(key K, value V) bool) { func (slf *SortMap[K, V]) For(handle func(key K, value V) bool) {
for k, v := range slf.m { for k, v := range slf.m {
if !handle(k, v) { if !handle(k, v) {
break break
@ -55,7 +53,7 @@ func (slf SortMap[K, V]) For(handle func(key K, value V) bool) {
} }
} }
func (slf SortMap[K, V]) ForSort(handle func(key K, value V) bool) { func (slf *SortMap[K, V]) ForSort(handle func(key K, value V) bool) {
var indexes []int var indexes []int
for i, _ := range slf.s { for i, _ := range slf.s {
indexes = append(indexes, i) indexes = append(indexes, i)
@ -69,7 +67,7 @@ func (slf SortMap[K, V]) ForSort(handle func(key K, value V) bool) {
} }
} }
func (slf SortMap[K, V]) ToMap() map[K]V { func (slf *SortMap[K, V]) ToMap() map[K]V {
var m = make(map[K]V) var m = make(map[K]V)
for k, v := range slf.m { for k, v := range slf.m {
m[k] = v m[k] = v
@ -77,7 +75,7 @@ func (slf SortMap[K, V]) ToMap() map[K]V {
return m return m
} }
func (slf SortMap[K, V]) ToSlice() []V { func (slf *SortMap[K, V]) ToSlice() []V {
var s = make([]V, 0, len(slf.m)) var s = make([]V, 0, len(slf.m))
for _, v := range slf.m { for _, v := range slf.m {
s = append(s, v) s = append(s, v)
@ -85,7 +83,21 @@ func (slf SortMap[K, V]) ToSlice() []V {
return s return s
} }
func (slf SortMap[K, V]) KeyToSlice() []K { func (slf *SortMap[K, V]) ToSliceSort() []V {
var indexes []int
for i, _ := range slf.s {
indexes = append(indexes, i)
}
sort.Ints(indexes)
var result []V
for _, i := range indexes {
k := slf.s[i]
result = append(result, slf.m[k])
}
return result
}
func (slf *SortMap[K, V]) KeyToSlice() []K {
var s = make([]K, 0, len(slf.m)) var s = make([]K, 0, len(slf.m))
for k := range slf.m { for k := range slf.m {
s = append(s, k) s = append(s, k)