Skip to content
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/tableauio/loader

go 1.20
Comment thread
wenchy marked this conversation as resolved.
go 1.21

require (
github.com/iancoleman/strcase v0.3.0
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ github.com/antchfx/xpath v1.2.3 h1:CCZWOzv5bAqjVv0offZ2LVgVYFbeldKQVuLNbViZdes=
github.com/antchfx/xpath v1.2.3/go.mod h1:i54GszH55fYfBmoZXapTHN8T8tkcHfRgLyVwwqzXNcs=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/bufbuild/protocompile v0.10.0 h1:+jW/wnLMLxaCEG8AX9lD0bQ5v9h1RUiMKOBOT5ll9dM=
github.com/bufbuild/protocompile v0.10.0/go.mod h1:G9qQIQo0xZ6Uyj6CMNz0saGmx2so+KONo8/KrELABiY=
github.com/bytedance/sonic v1.13.2 h1:8/H1FempDZqC4VqjptGo14QQlJx8VdZJegxs6wwfqpQ=
Expand Down Expand Up @@ -59,6 +60,7 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
Expand All @@ -69,6 +71,7 @@ github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBF
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
Expand Down Expand Up @@ -114,6 +117,7 @@ go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqe
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
Expand All @@ -128,6 +132,7 @@ golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL
golang.org/x/exp v0.0.0-20230418202329-0354be287a23 h1:4NKENAGIctmZYLK9W+X1kDK8ObBFqOSCJM6WE7CvkJY=
golang.org/x/exp v0.0.0-20230418202329-0354be287a23/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/image v0.18.0 h1:jGzIakQa/ZXI1I0Fxvaa9W7yP25TqT6cHIHn+6CqvSQ=
golang.org/x/image v0.18.0/go.mod h1:4yyo5vMFQjVjUcVk4jEQcU9MGy/rulF5WvUILseCM2E=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
Expand Down Expand Up @@ -157,6 +162,7 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down Expand Up @@ -186,6 +192,7 @@ google.golang.org/grpc v1.33.1/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTp
google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU=
google.golang.org/grpc v1.40.0/go.mod h1:ogyxbiOoUXAkP+4+xa6PZSE9DZgIHtSpzjDTB9KAK34=
google.golang.org/grpc v1.61.0 h1:TOvOcuXn30kRao+gfcvsebNEa5iZIiLkisYEkf7R7o0=
google.golang.org/grpc v1.61.0/go.mod h1:VUbo7IFqmF1QtCAstipjG0GIoq49KvMe9+h1jFLBNJs=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
Expand Down
4 changes: 2 additions & 2 deletions pkg/treemap/enumerable.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (m *TreeMap[K, V]) Each(f func(key K, value V)) {
// Map invokes the given function once for each element and returns a container
// containing the values returned by the given function as key/value pairs.
func (m *TreeMap[K, V]) Map(f func(key1 K, value1 V) (K, V)) *TreeMap[K, V] {
newMap := New[K, V]()
newMap := new3[K, V](m.tree.Less)
iterator := m.Iterator()
for iterator.Next() {
key2, value2 := f(iterator.Key(), iterator.Value())
Expand All @@ -24,7 +24,7 @@ func (m *TreeMap[K, V]) Map(f func(key1 K, value1 V) (K, V)) *TreeMap[K, V] {

// Select returns a new container containing all elements for which the given function returns a true value.
func (m *TreeMap[K, V]) Select(f func(key K, value V) bool) *TreeMap[K, V] {
newMap := New[K, V]()
newMap := new3[K, V](m.tree.Less)
iterator := m.Iterator()
for iterator.Next() {
if f(iterator.Key(), iterator.Value()) {
Expand Down
3 changes: 1 addition & 2 deletions pkg/treemap/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package treemap

import (
rbt "github.com/tableauio/loader/pkg/treemap/redblacktree"
"golang.org/x/exp/constraints"
)

// Refer: https://github.com/emirpasic/gods/blob/master/maps/treemap/iterator.go

type TreeMapIterator[K constraints.Ordered, V any] struct {
type TreeMapIterator[K comparable, V any] struct {
iterator *rbt.Iterator[K, V]
}

Expand Down
6 changes: 1 addition & 5 deletions pkg/treemap/redblacktree/iterator.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package redblacktree

import (
"golang.org/x/exp/constraints"
)

// Refer: https://github.com/emirpasic/gods/blob/master/trees/redblacktree/iterator.go

// Iterator holding the iterator's state
type Iterator[K constraints.Ordered, V any] struct {
type Iterator[K comparable, V any] struct {
Comment thread
wenchy marked this conversation as resolved.
tree *Tree[K, V]
node *Node[K, V]
position position
Expand Down
61 changes: 37 additions & 24 deletions pkg/treemap/redblacktree/redblacktree.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package redblacktree

import (
"cmp"
"fmt"

"golang.org/x/exp/constraints"
)

// Refer: https://github.com/emirpasic/gods/blob/master/trees/redblacktree/redblacktree.go
Expand All @@ -15,13 +14,19 @@ const (
)

// Tree holds elements of the red-black tree
type Tree[K constraints.Ordered, V any] struct {
type Tree[K comparable, V any] struct {
Root *Node[K, V]
size int
Less func(K, K) bool
}

type Lesser[T any] interface {
comparable
Less(other T) bool
}
Comment thread
wenchy marked this conversation as resolved.

// Node is a single element within the tree
type Node[K constraints.Ordered, V any] struct {
type Node[K, V any] struct {
Key K
Value V
color color
Expand All @@ -30,8 +35,16 @@ type Node[K constraints.Ordered, V any] struct {
Parent *Node[K, V]
}

func New[K constraints.Ordered, V any]() *Tree[K, V] {
return &Tree[K, V]{}
func New[K cmp.Ordered, V any]() *Tree[K, V] {
return New3[K, V](cmp.Less)
}

func New2[K Lesser[K], V any]() *Tree[K, V] {
return New3[K, V](func(k1, k2 K) bool { return k1.Less(k2) })
}

func New3[K comparable, V any](less func(K, K) bool) *Tree[K, V] {
return &Tree[K, V]{Less: less}
Comment thread
wenchy marked this conversation as resolved.
}

// Put inserts node into the tree.
Expand All @@ -46,26 +59,26 @@ func (tree *Tree[K, V]) Put(key K, value V) {
loop := true
for loop {
switch {
case key == node.Key:
node.Key = key
node.Value = value
return
case key < node.Key:
case tree.Less(key, node.Key):
if node.Left == nil {
node.Left = &Node[K, V]{Key: key, Value: value, color: red}
insertedNode = node.Left
loop = false
} else {
node = node.Left
}
case key > node.Key:
case tree.Less(node.Key, key):
if node.Right == nil {
node.Right = &Node[K, V]{Key: key, Value: value, color: red}
insertedNode = node.Right
loop = false
} else {
node = node.Right
}
default:
node.Key = key
node.Value = value
return
}
}
insertedNode.Parent = node
Expand Down Expand Up @@ -204,13 +217,13 @@ func (tree *Tree[K, V]) Floor(key K) (floor *Node[K, V], found bool) {
node := tree.Root
for node != nil {
switch {
case key == node.Key:
return node, true
case key < node.Key:
case tree.Less(key, node.Key):
node = node.Left
case key > node.Key:
case tree.Less(node.Key, key):
floor, found = node, true
node = node.Right
default:
return node, true
}
}
if found {
Expand All @@ -232,13 +245,13 @@ func (tree *Tree[K, V]) Ceiling(key K) (ceiling *Node[K, V], found bool) {
node := tree.Root
for node != nil {
switch {
case key == node.Key:
return node, true
case key < node.Key:
case tree.Less(key, node.Key):
ceiling, found = node, true
node = node.Left
case key > node.Key:
case tree.Less(node.Key, key):
node = node.Right
default:
return node, true
}
}
if found {
Expand Down Expand Up @@ -298,12 +311,12 @@ func (tree *Tree[K, V]) lookup(key K) *Node[K, V] {
node := tree.Root
for node != nil {
switch {
case key == node.Key:
return node
case key < node.Key:
case tree.Less(key, node.Key):
node = node.Left
case key > node.Key:
case tree.Less(node.Key, key):
node = node.Right
default:
return node
}
}
return nil
Expand Down
14 changes: 11 additions & 3 deletions pkg/treemap/treemap.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
package treemap

import (
"cmp"
"fmt"
"strings"

rbt "github.com/tableauio/loader/pkg/treemap/redblacktree"
"golang.org/x/exp/constraints"
)

type TreeMap[K constraints.Ordered, V any] struct {
type TreeMap[K comparable, V any] struct {
tree *rbt.Tree[K, V]
}

func New[K constraints.Ordered, V any]() *TreeMap[K, V] {
func New[K cmp.Ordered, V any]() *TreeMap[K, V] {
return &TreeMap[K, V]{tree: rbt.New[K, V]()}
}

func New2[K rbt.Lesser[K], V any]() *TreeMap[K, V] {
return &TreeMap[K, V]{tree: rbt.New2[K, V]()}
}

func new3[K comparable, V any](less func(K, K) bool) *TreeMap[K, V] {
return &TreeMap[K, V]{tree: rbt.New3[K, V](less)}
}

// Put inserts key-value pair into the map.
func (m *TreeMap[K, V]) Put(key K, value V) {
m.tree.Put(key, value)
Expand Down
57 changes: 54 additions & 3 deletions pkg/treemap/treemap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"fmt"
"strings"
"testing"

"golang.org/x/exp/constraints"
)

type testCase[T any, U any] struct {
Expand Down Expand Up @@ -57,6 +55,59 @@ func TestMapPut(t *testing.T) {
}
}

type key struct {
k1, k2 int
}

func (k key) Less(other key) bool {
if k.k1 == other.k1 {
return k.k2 < other.k2
}
return k.k1 < other.k1
}

func TestMapPutByNew2(t *testing.T) {
m := New2[key, string]()
m.Put(key{3, 1}, "e")
m.Put(key{3, 2}, "f")
m.Put(key{4, 1}, "g")
m.Put(key{2, 1}, "c")
m.Put(key{2, 2}, "d")
m.Put(key{1, 1}, "x")
m.Put(key{1, 2}, "b")
m.Put(key{1, 1}, "a") //overwrite

if actualValue := m.Size(); actualValue != 7 {
t.Errorf("Got %v expected %v", actualValue, 7)
}
if actualValue, expectedValue := m.Keys(), []key{{1, 1}, {1, 2}, {2, 1}, {2, 2}, {3, 1}, {3, 2}, {4, 1}}; !sameElements(actualValue, expectedValue) {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
if actualValue, expectedValue := m.Values(), []string{"a", "b", "c", "d", "e", "f", "g"}; !sameElements(actualValue, expectedValue) {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}

// key,expectedValue,expectedFound
tests1 := []testCase[key, string]{
{key{1, 1}, "a", true},
{key{1, 2}, "b", true},
{key{2, 1}, "c", true},
{key{2, 2}, "d", true},
{key{3, 1}, "e", true},
{key{3, 2}, "f", true},
{key{4, 1}, "g", true},
{key{4, 2}, "", false},
}

for _, test := range tests1 {
// retrievals
actualValue, actualFound := m.Get(test.Key)
if actualValue != test.Value || actualFound != test.Exist {
t.Errorf("Got %v expected %v", actualValue, test.Value)
}
}
}

func TestMapMin(t *testing.T) {
m := New[int, string]()

Expand Down Expand Up @@ -360,7 +411,7 @@ func TestMapCeilingOrMax(t *testing.T) {
}
}

func sameElements[T constraints.Ordered](a []T, b []T) bool {
func sameElements[T comparable](a []T, b []T) bool {
if len(a) != len(b) {
return false
}
Expand Down
2 changes: 1 addition & 1 deletion test/cpp-tableau-loader/src/protoconf/hero_conf.pc.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Code generated by protoc-gen-cpp-tableau-loader. DO NOT EDIT.
// versions:
// - protoc-gen-cpp-tableau-loader v0.10.0
// - protoc-gen-cpp-tableau-loader v0.10.1
// - protoc v3.19.3
// source: hero_conf.proto

Expand Down
2 changes: 1 addition & 1 deletion test/cpp-tableau-loader/src/protoconf/hero_conf.pc.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Code generated by protoc-gen-cpp-tableau-loader. DO NOT EDIT.
// versions:
// - protoc-gen-cpp-tableau-loader v0.10.0
// - protoc-gen-cpp-tableau-loader v0.10.1
// - protoc v3.19.3
// source: hero_conf.proto

Expand Down
2 changes: 1 addition & 1 deletion test/cpp-tableau-loader/src/protoconf/hub.pc.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Code generated by protoc-gen-cpp-tableau-loader. DO NOT EDIT.
// versions:
// - protoc-gen-cpp-tableau-loader v0.10.0
// - protoc-gen-cpp-tableau-loader v0.10.1
// - protoc v3.19.3

#include "hub.pc.h"
Expand Down
2 changes: 1 addition & 1 deletion test/cpp-tableau-loader/src/protoconf/hub.pc.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Code generated by protoc-gen-cpp-tableau-loader. DO NOT EDIT.
// versions:
// - protoc-gen-cpp-tableau-loader v0.10.0
// - protoc-gen-cpp-tableau-loader v0.10.1
// - protoc v3.19.3

#pragma once
Expand Down
2 changes: 1 addition & 1 deletion test/cpp-tableau-loader/src/protoconf/hub_shard0.pc.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Code generated by protoc-gen-cpp-tableau-loader. DO NOT EDIT.
// versions:
// - protoc-gen-cpp-tableau-loader v0.10.0
// - protoc-gen-cpp-tableau-loader v0.10.1
// - protoc v3.19.3

#include "hub.pc.h"
Expand Down
Loading