Skip to content

Commit 80e48f0

Browse files
committed
refactor: rename lancetconstraints package name to constraints
1 parent 0b976e9 commit 80e48f0

File tree

16 files changed

+92
-92
lines changed

16 files changed

+92
-92
lines changed

algorithm/search.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// Package algorithm contain some basic algorithm functions. eg. sort, search, list, linklist, stack, queue, tree, graph.
55
package algorithm
66

7-
import "github.com/duke-git/lancet/v2/lancetconstraints"
7+
import "github.com/duke-git/lancet/v2/constraints"
88

99
// Search algorithms see https://github.com/TheAlgorithms/Go/tree/master/search
1010

@@ -23,7 +23,7 @@ func LinearSearch[T any](slice []T, target T, equal func(a, b T) bool) int {
2323
// BinarySearch return the index of target within a sorted slice, use binary search (recursive call itself).
2424
// If not found return -1.
2525
// Play: https://go.dev/play/p/t6MeGiUSN47
26-
func BinarySearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int {
26+
func BinarySearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator constraints.Comparator) int {
2727
if highIndex < lowIndex || len(sortedSlice) == 0 {
2828
return -1
2929
}
@@ -44,7 +44,7 @@ func BinarySearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, com
4444
// BinaryIterativeSearch return the index of target within a sorted slice, use binary search (no recursive).
4545
// If not found return -1.
4646
// Play: https://go.dev/play/p/Anozfr8ZLH3
47-
func BinaryIterativeSearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int {
47+
func BinaryIterativeSearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator constraints.Comparator) int {
4848
startIndex := lowIndex
4949
endIndex := highIndex
5050

algorithm/sort.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
package algorithm
55

6-
import "github.com/duke-git/lancet/v2/lancetconstraints"
6+
import "github.com/duke-git/lancet/v2/constraints"
77

88
// BubbleSort applys the bubble sort algorithm to sort the collection, will change the original collection data.
99
// Play: https://go.dev/play/p/GNdv7Jg2Taj
10-
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) {
10+
func BubbleSort[T any](slice []T, comparator constraints.Comparator) {
1111
for i := 0; i < len(slice); i++ {
1212
for j := 0; j < len(slice)-1-i; j++ {
1313
isCurrGreatThanNext := comparator.Compare(slice[j], slice[j+1]) == 1
@@ -20,7 +20,7 @@ func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) {
2020

2121
// InsertionSort applys the insertion sort algorithm to sort the collection, will change the original collection data.
2222
// Play: https://go.dev/play/p/G5LJiWgJJW6
23-
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) {
23+
func InsertionSort[T any](slice []T, comparator constraints.Comparator) {
2424
for i := 0; i < len(slice); i++ {
2525
for j := i; j > 0; j-- {
2626
isPreLessThanCurrent := comparator.Compare(slice[j], slice[j-1]) == -1
@@ -35,7 +35,7 @@ func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) {
3535

3636
// SelectionSort applys the selection sort algorithm to sort the collection, will change the original collection data.
3737
// Play: https://go.dev/play/p/oXovbkekayS
38-
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) {
38+
func SelectionSort[T any](slice []T, comparator constraints.Comparator) {
3939
for i := 0; i < len(slice); i++ {
4040
min := i
4141
for j := i + 1; j < len(slice); j++ {
@@ -49,7 +49,7 @@ func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) {
4949

5050
// ShellSort applys the shell sort algorithm to sort the collection, will change the original collection data.
5151
// Play: https://go.dev/play/p/3ibkszpJEu3
52-
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) {
52+
func ShellSort[T any](slice []T, comparator constraints.Comparator) {
5353
size := len(slice)
5454

5555
gap := 1
@@ -69,11 +69,11 @@ func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) {
6969

7070
// QuickSort quick sorting for slice, lowIndex is 0 and highIndex is len(slice)-1.
7171
// Play: https://go.dev/play/p/7Y7c1Elk3ax
72-
func QuickSort[T any](slice []T, comparator lancetconstraints.Comparator) {
72+
func QuickSort[T any](slice []T, comparator constraints.Comparator) {
7373
quickSort(slice, 0, len(slice)-1, comparator)
7474
}
7575

76-
func quickSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) {
76+
func quickSort[T any](slice []T, lowIndex, highIndex int, comparator constraints.Comparator) {
7777
if lowIndex < highIndex {
7878
p := partition(slice, lowIndex, highIndex, comparator)
7979
quickSort(slice, lowIndex, p-1, comparator)
@@ -82,7 +82,7 @@ func quickSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconst
8282
}
8383

8484
// partition split slice into two parts
85-
func partition[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int {
85+
func partition[T any](slice []T, lowIndex, highIndex int, comparator constraints.Comparator) int {
8686
p := slice[highIndex]
8787
i := lowIndex
8888
for j := lowIndex; j < highIndex; j++ {
@@ -99,7 +99,7 @@ func partition[T any](slice []T, lowIndex, highIndex int, comparator lancetconst
9999

100100
// HeapSort applys the heap sort algorithm to sort the collection, will change the original collection data.
101101
// Play: https://go.dev/play/p/u6Iwa1VZS_f
102-
func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator) {
102+
func HeapSort[T any](slice []T, comparator constraints.Comparator) {
103103
size := len(slice)
104104

105105
for i := size/2 - 1; i >= 0; i-- {
@@ -111,7 +111,7 @@ func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator) {
111111
}
112112
}
113113

114-
func sift[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) {
114+
func sift[T any](slice []T, lowIndex, highIndex int, comparator constraints.Comparator) {
115115
i := lowIndex
116116
j := 2*i + 1
117117

@@ -133,11 +133,11 @@ func sift[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraint
133133

134134
// MergeSort applys the merge sort algorithm to sort the collection, will change the original collection data.
135135
// Play: https://go.dev/play/p/ydinn9YzUJn
136-
func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator) {
136+
func MergeSort[T any](slice []T, comparator constraints.Comparator) {
137137
mergeSort(slice, 0, len(slice)-1, comparator)
138138
}
139139

140-
func mergeSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) {
140+
func mergeSort[T any](slice []T, lowIndex, highIndex int, comparator constraints.Comparator) {
141141
if lowIndex < highIndex {
142142
mid := (lowIndex + highIndex) / 2
143143
mergeSort(slice, lowIndex, mid, comparator)
@@ -146,7 +146,7 @@ func mergeSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconst
146146
}
147147
}
148148

149-
func merge[T any](slice []T, lowIndex, midIndex, highIndex int, comparator lancetconstraints.Comparator) {
149+
func merge[T any](slice []T, lowIndex, midIndex, highIndex int, comparator constraints.Comparator) {
150150
i := lowIndex
151151
j := midIndex + 1
152152
temp := []T{}
@@ -175,7 +175,7 @@ func merge[T any](slice []T, lowIndex, midIndex, highIndex int, comparator lance
175175

176176
// CountSort applys the count sort algorithm to sort the collection, don't change the original collection data.
177177
// Play: https://go.dev/play/p/tB-Umgm0DrP
178-
func CountSort[T any](slice []T, comparator lancetconstraints.Comparator) []T {
178+
func CountSort[T any](slice []T, comparator constraints.Comparator) []T {
179179
size := len(slice)
180180
out := make([]T, size)
181181

algorithm/sort_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type people struct {
1616
// PeopleAageComparator sort people slice by age field
1717
type peopleAgeComparator struct{}
1818

19-
// Compare implements github.com/duke-git/lancet/v2/lancetconstraints/constraints.go/Comparator
19+
// Compare implements github.com/duke-git/lancet/v2/constraints/constraints.go/Comparator
2020
func (pc *peopleAgeComparator) Compare(v1 any, v2 any) int {
2121
p1, _ := v1.(people)
2222
p2, _ := v2.(people)

lancetconstraints/constraints.go renamed to constraints/constraints.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright 2021 [email protected]. All rights reserved.
22
// Use of this source code is governed by MIT license
33

4-
// Package lancetconstraints contain some comstomer constraints.
5-
package lancetconstraints
4+
// Package constraints contain some custom interface.
5+
package constraints
66

77
// Comparator is for comparing two values
88
type Comparator interface {

datastructure/heap/maxheap.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,26 @@ package datastructure
77
import (
88
"fmt"
99

10-
"github.com/duke-git/lancet/v2/lancetconstraints"
10+
"github.com/duke-git/lancet/v2/constraints"
1111
)
1212

1313
// MaxHeap implements a binary max heap
14-
// type T should implements Compare function in lancetconstraints.Comparator interface.
14+
// type T should implements Compare function in constraints.Comparator interface.
1515
type MaxHeap[T any] struct {
1616
data []T
17-
comparator lancetconstraints.Comparator
17+
comparator constraints.Comparator
1818
}
1919

2020
// NewMaxHeap returns a MaxHeap instance with the given comparator.
21-
func NewMaxHeap[T any](comparator lancetconstraints.Comparator) *MaxHeap[T] {
21+
func NewMaxHeap[T any](comparator constraints.Comparator) *MaxHeap[T] {
2222
return &MaxHeap[T]{
2323
data: make([]T, 0),
2424
comparator: comparator,
2525
}
2626
}
2727

2828
// BuildMaxHeap builds a MaxHeap instance with data and given comparator.
29-
func BuildMaxHeap[T any](data []T, comparator lancetconstraints.Comparator) *MaxHeap[T] {
29+
func BuildMaxHeap[T any](data []T, comparator constraints.Comparator) *MaxHeap[T] {
3030
heap := &MaxHeap[T]{
3131
data: make([]T, 0, len(data)),
3232
comparator: comparator,

datastructure/queue/priorityqueue.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ package datastructure
88
import (
99
"errors"
1010

11-
"github.com/duke-git/lancet/v2/lancetconstraints"
11+
"github.com/duke-git/lancet/v2/constraints"
1212
)
1313

1414
// PriorityQueue is a priority queue implemented by binary heap tree
15-
// type T should implements Compare function in lancetconstraints.Comparator interface.
15+
// type T should implements Compare function in constraints.Comparator interface.
1616
type PriorityQueue[T any] struct {
1717
items []T
1818
size int
19-
comparator lancetconstraints.Comparator
19+
comparator constraints.Comparator
2020
}
2121

2222
// NewPriorityQueue return a pointer of PriorityQueue
2323
// param `comparator` is used to compare values in the queue
24-
func NewPriorityQueue[T any](capacity int, comparator lancetconstraints.Comparator) *PriorityQueue[T] {
24+
func NewPriorityQueue[T any](capacity int, comparator constraints.Comparator) *PriorityQueue[T] {
2525
return &PriorityQueue[T]{
2626
items: make([]T, capacity+1),
2727
size: 0,

datastructure/tree/bstree.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@ package datastructure
77
import (
88
"math"
99

10+
"github.com/duke-git/lancet/v2/constraints"
1011
"github.com/duke-git/lancet/v2/datastructure"
11-
"github.com/duke-git/lancet/v2/lancetconstraints"
1212
)
1313

1414
// BSTree is a binary search tree data structure in which each node has at most two children,
1515
// which are referred to as the left child and the right child.
1616
// In BSTree: leftNode < rootNode < rightNode
17-
// type T should implements Compare function in lancetconstraints.Comparator interface.
17+
// type T should implements Compare function in constraints.Comparator interface.
1818
type BSTree[T any] struct {
1919
root *datastructure.TreeNode[T]
20-
comparator lancetconstraints.Comparator
20+
comparator constraints.Comparator
2121
}
2222

2323
// NewBSTree create a BSTree pointer
2424
// param `comparator` is used to compare values in the tree
25-
func NewBSTree[T any](rootData T, comparator lancetconstraints.Comparator) *BSTree[T] {
25+
func NewBSTree[T any](rootData T, comparator constraints.Comparator) *BSTree[T] {
2626
root := datastructure.NewTreeNode(rootData)
2727
return &BSTree[T]{root, comparator}
2828
}
@@ -87,7 +87,7 @@ func (t *BSTree[T]) HasSubTree(subTree *BSTree[T]) bool {
8787
}
8888

8989
func hasSubTree[T any](superTreeRoot, subTreeRoot *datastructure.TreeNode[T],
90-
comparator lancetconstraints.Comparator) bool {
90+
comparator constraints.Comparator) bool {
9191
result := false
9292

9393
if superTreeRoot != nil && subTreeRoot != nil {

datastructure/tree/tree_internal.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"fmt"
55
"math"
66

7+
"github.com/duke-git/lancet/v2/constraints"
78
"github.com/duke-git/lancet/v2/datastructure"
8-
"github.com/duke-git/lancet/v2/lancetconstraints"
99
)
1010

1111
func preOrderTraverse[T any](node *datastructure.TreeNode[T]) []T {
@@ -86,7 +86,7 @@ func levelOrderTraverse[T any](root *datastructure.TreeNode[T], traversal *[]T)
8686
}
8787
}
8888

89-
func insertTreeNode[T any](rootNode, newNode *datastructure.TreeNode[T], comparator lancetconstraints.Comparator) {
89+
func insertTreeNode[T any](rootNode, newNode *datastructure.TreeNode[T], comparator constraints.Comparator) {
9090
if comparator.Compare(newNode.Value, rootNode.Value) == -1 {
9191
if rootNode.Left == nil {
9292
rootNode.Left = newNode
@@ -103,7 +103,7 @@ func insertTreeNode[T any](rootNode, newNode *datastructure.TreeNode[T], compara
103103
}
104104

105105
// todo, delete root node failed
106-
func deleteTreeNode[T any](node *datastructure.TreeNode[T], data T, comparator lancetconstraints.Comparator) *datastructure.TreeNode[T] {
106+
func deleteTreeNode[T any](node *datastructure.TreeNode[T], data T, comparator constraints.Comparator) *datastructure.TreeNode[T] {
107107
if node == nil {
108108
return nil
109109
}
@@ -216,7 +216,7 @@ func calculateDepth[T any](node *datastructure.TreeNode[T], depth int) int {
216216
return max(calculateDepth(node.Left, depth+1), calculateDepth(node.Right, depth+1))
217217
}
218218

219-
func isSubTree[T any](superTreeRoot, subTreeRoot *datastructure.TreeNode[T], comparator lancetconstraints.Comparator) bool {
219+
func isSubTree[T any](superTreeRoot, subTreeRoot *datastructure.TreeNode[T], comparator constraints.Comparator) bool {
220220
if subTreeRoot == nil {
221221
return true
222222
}

0 commit comments

Comments
 (0)