mirror of
				https://github.com/ergochat/ergo.git
				synced 2025-11-03 23:37:22 +01:00 
			
		
		
		
	Merge pull request #1945 from slingamn/generic_reverse
use genericized slice-reversing function
This commit is contained in:
		
						commit
						71fe4ecf48
					
				@ -4,9 +4,10 @@
 | 
			
		||||
package history
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/ergochat/ergo/irc/utils"
 | 
			
		||||
	"sync"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/ergochat/ergo/irc/utils"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type ItemType uint
 | 
			
		||||
@ -55,12 +56,6 @@ func (item *Item) HasMsgid(msgid string) bool {
 | 
			
		||||
 | 
			
		||||
type Predicate func(item *Item) (matches bool)
 | 
			
		||||
 | 
			
		||||
func Reverse(results []Item) {
 | 
			
		||||
	for i, j := 0, len(results)-1; i < j; i, j = i+1, j-1 {
 | 
			
		||||
		results[i], results[j] = results[j], results[i]
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Buffer is a ring buffer holding message/event history for a channel or user
 | 
			
		||||
type Buffer struct {
 | 
			
		||||
	sync.RWMutex
 | 
			
		||||
@ -160,7 +155,7 @@ func (list *Buffer) betweenHelper(start, end Selector, cutoff time.Time, pred Pr
 | 
			
		||||
 | 
			
		||||
	defer func() {
 | 
			
		||||
		if !ascending {
 | 
			
		||||
			Reverse(results)
 | 
			
		||||
			utils.ReverseSlice(results)
 | 
			
		||||
		}
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -916,7 +916,7 @@ func (mysql *MySQL) betweenTimestamps(ctx context.Context, target, correspondent
 | 
			
		||||
 | 
			
		||||
	results, err = mysql.selectItems(ctx, queryBuf.String(), args...)
 | 
			
		||||
	if err == nil && !ascending {
 | 
			
		||||
		history.Reverse(results)
 | 
			
		||||
		utils.ReverseSlice(results)
 | 
			
		||||
	}
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -27,3 +27,10 @@ func CopyMap[K comparable, V any](input map[K]V) (result map[K]V) {
 | 
			
		||||
	}
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// reverse the order of a slice in place
 | 
			
		||||
func ReverseSlice[T any](results []T) {
 | 
			
		||||
	for i, j := 0, len(results)-1; i < j; i, j = i+1, j-1 {
 | 
			
		||||
		results[i], results[j] = results[j], results[i]
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user