9 type EpisodeCount struct {
18 type EpisodeList []*EpisodeRange
20 // Converts the EpisodeList into the AniDB API list format.
21 func (el EpisodeList) String() string {
22 scales := map[EpisodeType]int{}
24 for _, er := range el {
30 if s > scales[er.Type] {
35 parts := make([]string, len(el))
36 for i, er := range el {
37 parts[i] = er.Format(scales[er.Type])
40 return strings.Join(parts, ",")
43 // Returns true if any of the contained EpisodeRanges contain the
44 // given EpisodeContainer.
45 func (el EpisodeList) ContainsEpisodes(ec EpisodeContainer) bool {
46 for _, i := range el {
47 if i != nil && i.ContainsEpisodes(ec) {
54 // Parses a string in the AniDB API list format and converts into
57 // ParseEpisodeList("01") <=> EpisodeList{ParseEpisodeRange("01")}
58 // ParseEpisodeList("S2-S3") <=> EpisodeList{ParseEpisodeRange("S2-S3")}
59 // ParseEpisodeList("T1,C1-C3") <=> EpisodeList{ParseEpisodeRange("T1"), ParseEpisodeRange("C1-C3")}
60 func ParseEpisodeList(s string) (el EpisodeList) {
61 parts := strings.Split(s, ",")
63 el = make(EpisodeList, len(parts))
64 for i := range parts {
65 el[i] = ParseEpisodeRange(parts[i])
71 // Returns a simplified version of the EpisodeList (removes nil ranges, merges mergeable ranges, sorts).
72 func (el EpisodeList) Simplify() EpisodeList {
73 nl := make(EpisodeList, 0, len(el))
76 for _, er := range el {
83 for n, changed := 0, true; changed; n++ {
85 used := map[int]bool{}
88 for i, a := range nl {
92 for j, b := range nl[i+1:] {
93 if c := a.Merge(b); c != nil {
104 panic(fmt.Sprintf("Too many iterations (%d) when simplifing %s!", n, el))
111 func (el EpisodeList) Len() int {
115 func (el EpisodeList) Less(i, j int) bool {
121 case el[i].Type < el[j].Type:
123 case el[i].Type > el[j].Type:
125 case el[i].Start.Number < el[j].Start.Number:
131 func (el EpisodeList) Swap(i, j int) {
132 el[i], el[j] = el[j], el[i]