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 // Formats the list according to the number of digits of
44 // the count for its type, given in the EpisodeCount.
45 func (el EpisodeList) FormatLog(ec EpisodeCount) string {
46 parts := make([]string, len(el))
47 for i, er := range el {
49 case EpisodeTypeRegular:
50 parts[i] = er.FormatLog(ec.RegularCount)
51 case EpisodeTypeSpecial:
52 parts[i] = er.FormatLog(ec.SpecialCount)
53 case EpisodeTypeCredits:
54 parts[i] = er.FormatLog(ec.CreditsCount)
55 case EpisodeTypeOther:
56 parts[i] = er.FormatLog(ec.OtherCount)
57 case EpisodeTypeTrailer:
58 parts[i] = er.FormatLog(ec.TrailerCount)
59 case EpisodeTypeParody:
60 parts[i] = er.FormatLog(ec.ParodyCount)
62 parts[i] = er.Format(er.scale())
66 return strings.Join(parts, ",")
69 // Returns true if any of the contained EpisodeRanges contain the
70 // given EpisodeContainer.
71 func (el EpisodeList) ContainsEpisodes(ec EpisodeContainer) bool {
72 for _, i := range el {
73 if i != nil && i.ContainsEpisodes(ec) {
80 // Parses a string in the AniDB API list format and converts into
83 // ParseEpisodeList("01") <=> EpisodeList{ParseEpisodeRange("01")}
84 // ParseEpisodeList("S2-S3") <=> EpisodeList{ParseEpisodeRange("S2-S3")}
85 // ParseEpisodeList("T1,C1-C3") <=> EpisodeList{ParseEpisodeRange("T1"), ParseEpisodeRange("C1-C3")}
86 func ParseEpisodeList(s string) (el EpisodeList) {
87 parts := strings.Split(s, ",")
89 el = make(EpisodeList, len(parts))
90 for i := range parts {
91 el[i] = ParseEpisodeRange(parts[i])
97 // Returns a simplified version of the EpisodeList (removes nil ranges, merges mergeable ranges, sorts).
98 func (el EpisodeList) Simplify() EpisodeList {
99 nl := make(EpisodeList, 0, len(el))
102 for _, er := range el {
109 for n, changed := 0, true; changed; n++ {
111 used := map[int]bool{}
114 for i, a := range nl {
118 for j, b := range nl[i+1:] {
119 if c := a.Merge(b); c != nil {
130 panic(fmt.Sprintf("Too many iterations (%d) when simplifing %s!", n, el))
137 func (el EpisodeList) Len() int {
141 func (el EpisodeList) Less(i, j int) bool {
147 case el[i].Type < el[j].Type:
149 case el[i].Type > el[j].Type:
151 case el[i].Start.Number < el[j].Start.Number:
157 func (el EpisodeList) Swap(i, j int) {
158 el[i], el[j] = el[j], el[i]