@@ -5,17 +5,19 @@ import (
55 "sync"
66)
77
8- type ArrayQueue struct {
9- Arr []int
8+ // ArrayQueue represents a generic array-based queue
9+ type ArrayQueue [T any ] struct {
10+ Arr []T
1011 ArrSize int
1112 FirstIndex int
1213 LastIndex int
1314 mutex sync.RWMutex
1415}
1516
16- func NewArrayQueue () * ArrayQueue {
17- return & ArrayQueue {
18- Arr : []int {0 , 0 },
17+ // NewArrayQueue creates a new generic array-based queue
18+ func NewArrayQueue [T any ]() * ArrayQueue [T ] {
19+ return & ArrayQueue [T ]{
20+ Arr : make ([]T , 2 ),
1921 ArrSize : 2 ,
2022 FirstIndex : 0 ,
2123 LastIndex : 0 ,
@@ -24,7 +26,7 @@ func NewArrayQueue() *ArrayQueue {
2426}
2527
2628// Enqueue adds data to the queue
27- func (arr * ArrayQueue ) Enqueue (data int ) {
29+ func (arr * ArrayQueue [ T ] ) Enqueue (data T ) {
2830 arr .mutex .Lock ()
2931 defer arr .mutex .Unlock ()
3032
@@ -34,10 +36,8 @@ func (arr *ArrayQueue) Enqueue(data int) {
3436 // If first index is 0 and last index is bigger than array size we will increase array size
3537 if arr .LastIndex >= arr .ArrSize {
3638 if arr .FirstIndex == 0 {
37- newArr := make ([]int , arr .ArrSize * 2 )
38- for i := 0 ; i < arr .ArrSize ; i ++ {
39- newArr [i ] = arr .Arr [i ]
40- }
39+ newArr := make ([]T , arr .ArrSize * 2 )
40+ copy (newArr , arr .Arr )
4141 arr .Arr = newArr
4242 arr .ArrSize *= 2
4343 } else {
@@ -49,23 +49,25 @@ func (arr *ArrayQueue) Enqueue(data int) {
4949}
5050
5151// Dequeue removes data from the queue
52- func (arr * ArrayQueue ) Dequeue () {
52+ func (arr * ArrayQueue [ T ] ) Dequeue () {
5353 arr .mutex .Lock ()
5454 defer arr .mutex .Unlock ()
5555
5656 // if deque is run first
5757 if arr .FirstIndex == 0 && arr .LastIndex == 0 {
5858 return
5959 }
60- arr .Arr [arr .FirstIndex ] = 0
60+ var zero T
61+ arr .Arr [arr .FirstIndex ] = zero
6162 arr .FirstIndex ++
6263 if arr .LastIndex - arr .FirstIndex <= arr .ArrSize / 4 {
63- newArr := make ([]int , arr .ArrSize / 2 )
64+ newArr := make ([]T , arr .ArrSize / 2 )
6465 sort := 0
6566 for i := arr .FirstIndex ; i < arr .LastIndex ; i ++ {
6667 newArr [sort ] = arr .Arr [i ]
6768 sort ++
6869 }
70+
6971 arr .Arr = newArr
7072 arr .ArrSize /= 2
7173 arr .LastIndex = arr .LastIndex - arr .FirstIndex
@@ -74,8 +76,8 @@ func (arr *ArrayQueue) Dequeue() {
7476}
7577
7678// reSort reorders the queue data
77- func (arr * ArrayQueue ) reSort () {
78- newArr := make ([]int , arr .ArrSize )
79+ func (arr * ArrayQueue [ T ] ) reSort () {
80+ newArr := make ([]T , arr .ArrSize )
7981 sort := 0
8082 for i := arr .FirstIndex ; i < arr .LastIndex ; i ++ {
8183 newArr [sort ] = arr .Arr [i ]
@@ -87,22 +89,22 @@ func (arr *ArrayQueue) reSort() {
8789}
8890
8991// List returns a slice of queue data
90- func (arr * ArrayQueue ) List () []int {
92+ func (arr * ArrayQueue [ T ] ) List () []T {
9193 arr .mutex .RLock ()
9294 defer arr .mutex .RUnlock ()
9395
9496 if arr .FirstIndex >= arr .LastIndex {
95- return []int {}
97+ return []T {}
9698 }
97- var list []int
99+ var list []T
98100 for i := arr .FirstIndex ; i < arr .LastIndex ; i ++ {
99101 list = append (list , arr .Arr [i ])
100102 }
101103 return list
102104}
103105
104106// Print displays queue data
105- func (arr * ArrayQueue ) Print () {
107+ func (arr * ArrayQueue [ T ] ) Print () {
106108 arr .mutex .RLock ()
107109 defer arr .mutex .RUnlock ()
108110 fmt .Print ("print : " )
0 commit comments