File: generic.go

package info (click to toggle)
golang-github-segmentio-asm 1.2.0%2Bgit20231107.1cfacc8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 932 kB
  • sloc: asm: 6,093; makefile: 32
file content (40 lines) | stat: -rw-r--r-- 673 bytes parent folder | download
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
37
38
39
40
package qsort

import "bytes"

type generic struct {
	data []byte
	size int
	temp []byte
	swap func(int, int)
}

func newGeneric(data []byte, size int, swap func(int, int)) *generic {
	return &generic{
		data: data,
		size: size,
		temp: make([]byte, size),
		swap: swap,
	}
}

func (g *generic) Len() int {
	return len(g.data) / g.size
}

func (g *generic) Less(i, j int) bool {
	return bytes.Compare(g.slice(i), g.slice(j)) < 0
}

func (g *generic) Swap(i, j int) {
	copy(g.temp, g.slice(j))
	copy(g.slice(j), g.slice(i))
	copy(g.slice(i), g.temp)
	if g.swap != nil {
		g.swap(i, j)
	}
}

func (g *generic) slice(i int) []byte {
	return g.data[i*g.size : (i+1)*g.size]
}