mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-09 15:12:26 +08:00
feat: add Size func for PriorityQueue
This commit is contained in:
@@ -29,6 +29,11 @@ func (q *PriorityQueue[T]) IsEmpty() bool {
|
|||||||
return q.size == 0
|
return q.size == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Size get number of items in the queue
|
||||||
|
func (q *PriorityQueue[T]) Size() int {
|
||||||
|
return q.size
|
||||||
|
}
|
||||||
|
|
||||||
// IsFull checks if the queue capacity is full or not
|
// IsFull checks if the queue capacity is full or not
|
||||||
func (q *PriorityQueue[T]) IsFull() bool {
|
func (q *PriorityQueue[T]) IsFull() bool {
|
||||||
return q.size == len(q.items)-1
|
return q.size == len(q.items)-1
|
||||||
|
|||||||
@@ -52,9 +52,13 @@ func TestPriorityQueue_Dequeue(t *testing.T) {
|
|||||||
pq.Enqueue(i)
|
pq.Enqueue(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assert.Equal(10, pq.Size())
|
||||||
|
|
||||||
val, ok := pq.Dequeue()
|
val, ok := pq.Dequeue()
|
||||||
assert.Equal(true, ok)
|
assert.Equal(true, ok)
|
||||||
assert.Equal(10, val)
|
assert.Equal(10, val)
|
||||||
|
|
||||||
assert.Equal([]int{9, 8, 6, 7, 3, 2, 5, 1, 4}, pq.Data())
|
assert.Equal([]int{9, 8, 6, 7, 3, 2, 5, 1, 4}, pq.Data())
|
||||||
|
|
||||||
|
assert.Equal(9, pq.Size())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,6 +51,30 @@ import (
|
|||||||
- [Contain](#LinkedQueue_Contain)
|
- [Contain](#LinkedQueue_Contain)
|
||||||
|
|
||||||
|
|
||||||
|
### 3. CircularQueue
|
||||||
|
- [NewCircularQueue](#NewCircularQueue)
|
||||||
|
- [Data](#CircularQueue_Data)
|
||||||
|
- [Enqueue](#CircularQueue_Enqueue)
|
||||||
|
- [Dequeue](#CircularQueue_Dequeue)
|
||||||
|
- [Front](#CircularQueue_Front)
|
||||||
|
- [Back](#CircularQueue_Back)
|
||||||
|
- [Front](#CircularQueue_Size)
|
||||||
|
- [IsEmpty](#CircularQueue_IsEmpty)
|
||||||
|
- [IsFull](#CircularQueue_IsFull)
|
||||||
|
- [Clear](#CircularQueue_Clear)
|
||||||
|
- [Contain](#CircularQueue_Contain)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 4. PriorityQueue
|
||||||
|
- [NewPriorityQueue](#NewPriorityQueue)
|
||||||
|
- [Data](#PriorityQueue_Data)
|
||||||
|
- [Enqueue](#PriorityQueue_Enqueue)
|
||||||
|
- [Dequeue](#PriorityQueue_Dequeue)
|
||||||
|
- [IsEmpty](#PriorityQueue_IsEmpty)
|
||||||
|
- [IsFull](#PriorityQueue_IsFull)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
@@ -717,3 +741,598 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 3. CircularQueue
|
||||||
|
Circular queue implemented by slice.
|
||||||
|
|
||||||
|
### <span id="NewCircularQueue">NewCircularQueue</span>
|
||||||
|
<p>Return a CircularQueue pointer with specific capacity </p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func NewCircularQueue[T any](capacity int) *CircularQueue[T]
|
||||||
|
|
||||||
|
type CircularQueue[T any] struct {
|
||||||
|
data []T
|
||||||
|
front int
|
||||||
|
rear int
|
||||||
|
capacity int
|
||||||
|
}
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
queue "github.com/duke-git/lancet/v2/datastructure/queue"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
q := queue.NewCircularQueue[int](5)
|
||||||
|
fmt.Println(q.Data()) // []
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="CircularQueue_Data">Data</span>
|
||||||
|
<p>Get all queue data</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (q *CircularQueue[T]) Data() []T
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
queue "github.com/duke-git/lancet/v2/datastructure/queue"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
q := queue.NewCircularQueue[int](5)
|
||||||
|
fmt.Println(q.Data()) // []
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="CircularQueue_Enqueue">Enqueue</span>
|
||||||
|
<p>Put element into queue, if queue is full, return error</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (q *CircularQueue[T]) Enqueue(value T) error
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
queue "github.com/duke-git/lancet/v2/datastructure/queue"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
q := queue.NewCircularQueue[int](5)
|
||||||
|
q.Enqueue(1)
|
||||||
|
q.Enqueue(2)
|
||||||
|
q.Enqueue(3)
|
||||||
|
|
||||||
|
fmt.Println(q.Data()) // 1,2,3
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="CircularQueue_Dequeue">Dequeue</span>
|
||||||
|
<p>Remove head element of queue and return it</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (q *CircularQueue[T]) Dequeue() (*T, bool)
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
queue "github.com/duke-git/lancet/v2/datastructure/queue"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
q := queue.NewCircularQueue[int](5)
|
||||||
|
q.Enqueue(1)
|
||||||
|
q.Enqueue(2)
|
||||||
|
q.Enqueue(3)
|
||||||
|
|
||||||
|
val := q.Dequeue()
|
||||||
|
fmt.Println(*val) // 1
|
||||||
|
fmt.Println(q.Data()) // 2,3
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="CircularQueue_Front">Front</span>
|
||||||
|
<p>Just get head element of queue</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (q *CircularQueue[T]) Front() T
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
queue "github.com/duke-git/lancet/v2/datastructure/queue"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
q := queue.NewCircularQueue[int](5)
|
||||||
|
q.Enqueue(1)
|
||||||
|
q.Enqueue(2)
|
||||||
|
q.Enqueue(3)
|
||||||
|
|
||||||
|
fmt.Println(q.Front()) // 1
|
||||||
|
fmt.Println(q.Data()) // 1,2,3
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="CircularQueue_Back">Back</span>
|
||||||
|
<p>Just get tail element of queue</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (q *CircularQueue[T]) Back() T
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
queue "github.com/duke-git/lancet/v2/datastructure/queue"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
q := queue.NewCircularQueue[int](5)
|
||||||
|
q.Enqueue(1)
|
||||||
|
q.Enqueue(2)
|
||||||
|
q.Enqueue(3)
|
||||||
|
|
||||||
|
fmt.Println(q.Back()) // 3
|
||||||
|
fmt.Println(q.Data()) // 1,2,3
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="CircularQueue_Size">Size</span>
|
||||||
|
<p>Get the number of elements in queue</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (q *CircularQueue[T]) Size() int
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
queue "github.com/duke-git/lancet/v2/datastructure/queue"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
q := queue.NewCircularQueue[int](5)
|
||||||
|
q.Enqueue(1)
|
||||||
|
q.Enqueue(2)
|
||||||
|
q.Enqueue(3)
|
||||||
|
|
||||||
|
fmt.Println(q.Size()) // 3
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="CircularQueue_IsEmpty">IsEmpty</span>
|
||||||
|
<p>Check if queue is empty or not</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (q *CircularQueue[T]) IsEmpty() bool
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
queue "github.com/duke-git/lancet/v2/datastructure/queue"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
q := queue.NewCircularQueue[int](5)
|
||||||
|
fmt.Println(q.IsEmpty()) // true
|
||||||
|
|
||||||
|
q.Enqueue(1)
|
||||||
|
q.Enqueue(2)
|
||||||
|
q.Enqueue(3)
|
||||||
|
|
||||||
|
fmt.Println(q.IsEmpty()) // false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="CircularQueue_IsFull">IsFull</span>
|
||||||
|
<p>Check if queue is full or not</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (q *CircularQueue[T]) IsFull() bool
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
queue "github.com/duke-git/lancet/v2/datastructure/queue"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
q := queue.NewCircularQueue[int](3)
|
||||||
|
fmt.Println(q.IsFull()) // false
|
||||||
|
|
||||||
|
q.Enqueue(1)
|
||||||
|
q.Enqueue(2)
|
||||||
|
q.Enqueue(3)
|
||||||
|
|
||||||
|
fmt.Println(q.IsFull()) // true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="CircularQueue_Clear">Clear</span>
|
||||||
|
<p>Clean all data in queue</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (q *CircularQueue[T]) Clear()
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
queue "github.com/duke-git/lancet/v2/datastructure/queue"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
q := queue.NewCircularQueue[int](5)
|
||||||
|
q.Enqueue(1)
|
||||||
|
q.Enqueue(2)
|
||||||
|
q.Enqueue(3)
|
||||||
|
q.Clear()
|
||||||
|
|
||||||
|
fmt.Println(q.IsEmpty()) // true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="CircularQueue_Contain">Contain</span>
|
||||||
|
<p>checks if the value is in queue or not</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (q *CircularQueue[T]) Contain(value T) bool
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
queue "github.com/duke-git/lancet/v2/datastructure/queue"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
q := queue.NewCircularQueue[int](5)
|
||||||
|
q.Enqueue(1)
|
||||||
|
q.Enqueue(2)
|
||||||
|
q.Enqueue(3)
|
||||||
|
|
||||||
|
fmt.Println(q.Contain(1)) // true
|
||||||
|
fmt.Println(q.Contain(4)) // false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### 4. PriorityQueue
|
||||||
|
Common queue implemented by slice.
|
||||||
|
|
||||||
|
### <span id="NewPriorityQueue">NewPriorityQueue</span>
|
||||||
|
<p>Return a PriorityQueue pointer with specific capacity, param `comparator` is used to compare values of type T in the queue. </p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func NewPriorityQueue[T any](capacity int, comparator lancetconstraints.Comparator) *PriorityQueue[T]
|
||||||
|
|
||||||
|
type PriorityQueue[T any] struct {
|
||||||
|
items []T
|
||||||
|
size int
|
||||||
|
comparator lancetconstraints.Comparator
|
||||||
|
}
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
queue "github.com/duke-git/lancet/v2/datastructure/queue"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
q := queue.NewPriorityQueue[int](3)
|
||||||
|
fmt.Println(q.Data()) // []
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="PriorityQueue_Data">Data</span>
|
||||||
|
<p>Get all queue data</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (q *PriorityQueue[T]) Data() []T
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
queue "github.com/duke-git/lancet/v2/datastructure/queue"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
q := queue.NewPriorityQueue[int](3)
|
||||||
|
fmt.Println(q.Data()) // []
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="PriorityQueue_Enqueue">Enqueue</span>
|
||||||
|
<p>Put element into queue, if queue is full, return false</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (q *PriorityQueue[T]) Enqueue(item T) bool
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
queue "github.com/duke-git/lancet/v2/datastructure/queue"
|
||||||
|
)
|
||||||
|
|
||||||
|
type intComparator struct{}
|
||||||
|
|
||||||
|
func (c *intComparator) Compare(v1, v2 any) int {
|
||||||
|
val1, _ := v1.(int)
|
||||||
|
val2, _ := v2.(int)
|
||||||
|
|
||||||
|
if val1 < val2 {
|
||||||
|
return -1
|
||||||
|
} else if val1 > val2 {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
comparator := &intComparator{}
|
||||||
|
q := queue.NewPriorityQueue[int](10, comparator)
|
||||||
|
for i := 1; i < 11; i++ {
|
||||||
|
q.Enqueue(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(q.Data()) // 10, 9, 6, 7, 8, 2, 5, 1, 4, 3
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="PriorityQueue_Dequeue">Dequeue</span>
|
||||||
|
<p>Remove head element of queue and return it</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (q *PriorityQueue[T]) Dequeue() (T, bool)
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
queue "github.com/duke-git/lancet/v2/datastructure/queue"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
type intComparator struct{}
|
||||||
|
|
||||||
|
func (c *intComparator) Compare(v1, v2 any) int {
|
||||||
|
val1, _ := v1.(int)
|
||||||
|
val2, _ := v2.(int)
|
||||||
|
|
||||||
|
if val1 < val2 {
|
||||||
|
return -1
|
||||||
|
} else if val1 > val2 {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
comparator := &intComparator{}
|
||||||
|
q := queue.NewPriorityQueue[int](10, comparator)
|
||||||
|
for i := 1; i < 11; i++ {
|
||||||
|
q.Enqueue(i)
|
||||||
|
}
|
||||||
|
val, ok := pq.Dequeue()
|
||||||
|
fmt.Println(val) // 10
|
||||||
|
fmt.Println(q.Data()) // 9, 8, 6, 7, 3, 2, 5, 1, 4
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="PriorityQueue_IsEmpty">IsEmpty</span>
|
||||||
|
<p>Check if queue is empty or not</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (q *PriorityQueue[T]) IsEmpty() bool
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
queue "github.com/duke-git/lancet/v2/datastructure/queue"
|
||||||
|
)
|
||||||
|
|
||||||
|
type intComparator struct{}
|
||||||
|
|
||||||
|
func (c *intComparator) Compare(v1, v2 any) int {
|
||||||
|
val1, _ := v1.(int)
|
||||||
|
val2, _ := v2.(int)
|
||||||
|
|
||||||
|
if val1 < val2 {
|
||||||
|
return -1
|
||||||
|
} else if val1 > val2 {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
comparator := &intComparator{}
|
||||||
|
q := queue.NewPriorityQueue[int](10, comparator)
|
||||||
|
fmt.Println(q.IsEmpty()) // true
|
||||||
|
|
||||||
|
for i := 1; i < 11; i++ {
|
||||||
|
q.Enqueue(i)
|
||||||
|
}
|
||||||
|
fmt.Println(q.IsEmpty()) // false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="PriorityQueue_IsFull">IsFull</span>
|
||||||
|
<p>Check if queue is full or not</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (q *PriorityQueue[T]) IsFull() bool
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
queue "github.com/duke-git/lancet/v2/datastructure/queue"
|
||||||
|
)
|
||||||
|
|
||||||
|
type intComparator struct{}
|
||||||
|
|
||||||
|
func (c *intComparator) Compare(v1, v2 any) int {
|
||||||
|
val1, _ := v1.(int)
|
||||||
|
val2, _ := v2.(int)
|
||||||
|
|
||||||
|
if val1 < val2 {
|
||||||
|
return -1
|
||||||
|
} else if val1 > val2 {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
comparator := &intComparator{}
|
||||||
|
q := queue.NewPriorityQueue[int](10, comparator)
|
||||||
|
fmt.Println(q.IsFull()) // false
|
||||||
|
|
||||||
|
for i := 1; i < 11; i++ {
|
||||||
|
q.Enqueue(i)
|
||||||
|
}
|
||||||
|
fmt.Println(q.IsFull()) // true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user