File: sorter.go

package info (click to toggle)
golang-github-anacrolix-missinggo 2.1.0-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 836 kB
  • sloc: makefile: 4
file content (36 lines) | stat: -rw-r--r-- 658 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package slices

import "reflect"

type sorter struct {
	sl   reflect.Value
	less reflect.Value
}

func (s *sorter) Len() int {
	return s.sl.Len()
}

func (s *sorter) Less(i, j int) bool {
	return s.less.Call([]reflect.Value{
		s.sl.Index(i),
		s.sl.Index(j),
	})[0].Bool()
}

func (s *sorter) Swap(i, j int) {
	t := reflect.New(s.sl.Type().Elem()).Elem()
	t.Set(s.sl.Index(i))
	s.sl.Index(i).Set(s.sl.Index(j))
	s.sl.Index(j).Set(t)
}

func (s *sorter) Pop() interface{} {
	ret := s.sl.Index(s.sl.Len() - 1).Interface()
	s.sl.SetLen(s.sl.Len() - 1)
	return ret
}

func (s *sorter) Push(val interface{}) {
	s.sl = reflect.Append(s.sl, reflect.ValueOf(val))
}