Browse Source

use genericized slice-reversing function

tags/v2.10.0-rc1
Shivaram Lingamneni 2 years ago
parent
commit
2df5fb1956
3 changed files with 11 additions and 9 deletions
  1. 3
    8
      irc/history/history.go
  2. 1
    1
      irc/mysql/history.go
  3. 7
    0
      irc/utils/types.go

+ 3
- 8
irc/history/history.go View File

4
 package history
4
 package history
5
 
5
 
6
 import (
6
 import (
7
-	"github.com/ergochat/ergo/irc/utils"
8
 	"sync"
7
 	"sync"
9
 	"time"
8
 	"time"
9
+
10
+	"github.com/ergochat/ergo/irc/utils"
10
 )
11
 )
11
 
12
 
12
 type ItemType uint
13
 type ItemType uint
55
 
56
 
56
 type Predicate func(item *Item) (matches bool)
57
 type Predicate func(item *Item) (matches bool)
57
 
58
 
58
-func Reverse(results []Item) {
59
-	for i, j := 0, len(results)-1; i < j; i, j = i+1, j-1 {
60
-		results[i], results[j] = results[j], results[i]
61
-	}
62
-}
63
-
64
 // Buffer is a ring buffer holding message/event history for a channel or user
59
 // Buffer is a ring buffer holding message/event history for a channel or user
65
 type Buffer struct {
60
 type Buffer struct {
66
 	sync.RWMutex
61
 	sync.RWMutex
160
 
155
 
161
 	defer func() {
156
 	defer func() {
162
 		if !ascending {
157
 		if !ascending {
163
-			Reverse(results)
158
+			utils.ReverseSlice(results)
164
 		}
159
 		}
165
 	}()
160
 	}()
166
 
161
 

+ 1
- 1
irc/mysql/history.go View File

916
 
916
 
917
 	results, err = mysql.selectItems(ctx, queryBuf.String(), args...)
917
 	results, err = mysql.selectItems(ctx, queryBuf.String(), args...)
918
 	if err == nil && !ascending {
918
 	if err == nil && !ascending {
919
-		history.Reverse(results)
919
+		utils.ReverseSlice(results)
920
 	}
920
 	}
921
 	return
921
 	return
922
 }
922
 }

+ 7
- 0
irc/utils/types.go View File

27
 	}
27
 	}
28
 	return
28
 	return
29
 }
29
 }
30
+
31
+// reverse the order of a slice in place
32
+func ReverseSlice[T any](results []T) {
33
+	for i, j := 0, len(results)-1; i < j; i, j = i+1, j-1 {
34
+		results[i], results[j] = results[j], results[i]
35
+	}
36
+}

Loading…
Cancel
Save