9 type EpisodeCount struct {
18 type EpisodeList []*EpisodeRange
20 func EpisodeToList(ep *Episode) EpisodeList {
21 return RangesToList(EpisodeToRange(ep))
24 func RangesToList(ranges ...*EpisodeRange) EpisodeList {
25 return EpisodeList(ranges)
28 // Converts the EpisodeList into the AniDB API list format.
29 func (el EpisodeList) String() string {
30 scales := map[EpisodeType]int{}
32 for _, er := range el {
38 if s > scales[er.Type] {
43 parts := make([]string, len(el))
44 for i, er := range el {
45 parts[i] = er.Format(scales[er.Type])
48 return strings.Join(parts, ",")
51 // Formats the list according to the number of digits of
52 // the count for its type, given in the EpisodeCount.
53 func (el EpisodeList) FormatLog(ec EpisodeCount) string {
54 parts := make([]string, len(el))
55 for i, er := range el {
57 case EpisodeTypeRegular:
58 parts[i] = er.FormatLog(ec.RegularCount)
59 case EpisodeTypeSpecial:
60 parts[i] = er.FormatLog(ec.SpecialCount)
61 case EpisodeTypeCredits:
62 parts[i] = er.FormatLog(ec.CreditsCount)
63 case EpisodeTypeOther:
64 parts[i] = er.FormatLog(ec.OtherCount)
65 case EpisodeTypeTrailer:
66 parts[i] = er.FormatLog(ec.TrailerCount)
67 case EpisodeTypeParody:
68 parts[i] = er.FormatLog(ec.ParodyCount)
70 parts[i] = er.Format(er.scale())
74 return strings.Join(parts, ",")
77 // Returns true if any of the contained EpisodeRanges contain the
78 // given EpisodeContainer.
79 func (el EpisodeList) ContainsEpisodes(ec EpisodeContainer) bool {
80 for _, i := range el {
81 if i != nil && i.ContainsEpisodes(ec) {
88 // Parses a string in the AniDB API list format and converts into
91 // ParseEpisodeList("01") <=> EpisodeList{ParseEpisodeRange("01")}
92 // ParseEpisodeList("S2-S3") <=> EpisodeList{ParseEpisodeRange("S2-S3")}
93 // ParseEpisodeList("T1,C1-C3") <=> EpisodeList{ParseEpisodeRange("T1"), ParseEpisodeRange("C1-C3")}
94 func ParseEpisodeList(s string) (el EpisodeList) {
95 parts := strings.Split(s, ",")
97 el = make(EpisodeList, len(parts))
98 for i := range parts {
99 el[i] = ParseEpisodeRange(parts[i])
105 // Returns a simplified version of the EpisodeList (removes nil ranges, merges mergeable ranges, sorts).
106 func (el EpisodeList) Simplify() EpisodeList {
107 nl := make(EpisodeList, 0, len(el))
110 for _, er := range el {
117 for n, changed := 0, true; changed; n++ {
119 used := map[int]bool{}
122 for i, a := range nl {
126 for j, b := range nl[i+1:] {
127 if c := a.Merge(b); c != nil {
138 panic(fmt.Sprintf("Too many iterations (%d) when simplifing %s!", n, el))
145 func (el EpisodeList) CountEpisodes() (ec EpisodeCount) {
146 for _, er := range el {
149 case EpisodeTypeRegular:
151 case EpisodeTypeSpecial:
153 case EpisodeTypeCredits:
155 case EpisodeTypeOther:
157 case EpisodeTypeTrailer:
159 case EpisodeTypeParody:
171 *c += er.End.Number - er.Start.Number
176 func (el EpisodeList) Len() int {
180 func (el EpisodeList) Less(i, j int) bool {
186 case el[i].Type < el[j].Type:
188 case el[i].Type > el[j].Type:
190 case el[i].Start.Number < el[j].Start.Number:
196 func (el EpisodeList) Swap(i, j int) {
197 el[i], el[j] = el[j], el[i]