File: seqset.go

package info (click to toggle)
golang-github-protonmail-gluon 0.17.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,020 kB
  • sloc: sh: 55; makefile: 5
file content (64 lines) | stat: -rw-r--r-- 1,081 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package imap

import (
	"fmt"
	"strconv"
	"strings"

	"github.com/bradenaw/juniper/xslices"
	"golang.org/x/exp/slices"
)

type SeqVal struct {
	Begin, End SeqID
}

func (seqval SeqVal) canCombine(val SeqID) bool {
	return val == SeqID(uint32(seqval.End)+1)
}

func (seqval SeqVal) String() string {
	if seqval.End > seqval.Begin {
		return fmt.Sprintf("%v:%v", seqval.Begin, seqval.End)
	}

	return strconv.FormatUint(uint64(seqval.End), 10)
}

type SeqSet []SeqVal

func NewSeqSetFromUID(set []UID) SeqSet {
	return NewSeqSet(xslices.Map(set, func(t UID) SeqID {
		return SeqID(t)
	}))
}

func NewSeqSet(set []SeqID) SeqSet {
	slices.Sort(set)

	var res SeqSet

	for _, val := range set {
		if n := len(res); n > 0 {
			if res[n-1].canCombine(val) {
				res[n-1].End = val
			} else {
				res = append(res, SeqVal{Begin: val, End: val})
			}
		} else {
			res = append(res, SeqVal{Begin: val, End: val})
		}
	}

	return res
}

func (set SeqSet) String() string {
	var res []string

	for _, val := range set {
		res = append(res, val.String())
	}

	return strings.Join(res, ",")
}