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 func ContainerToList(ec EpisodeContainer) EpisodeList {
29 switch v := ec.(type) {
31 return EpisodeToList(v)
33 return RangesToList(v)
37 panic("unimplemented")
41 // Converts the EpisodeList into the AniDB API list format.
42 func (el EpisodeList) String() string {
43 scales := map[EpisodeType]int{}
45 for _, er := range el {
51 if s > scales[er.Type] {
56 parts := make([]string, len(el))
57 for i, er := range el {
58 parts[i] = er.Format(scales[er.Type])
61 return strings.Join(parts, ",")
64 // Formats the list according to the number of digits of
65 // the count for its type, given in the EpisodeCount.
66 func (el EpisodeList) FormatLog(ec EpisodeCount) string {
67 parts := make([]string, len(el))
68 for i, er := range el {
70 case EpisodeTypeRegular:
71 parts[i] = er.FormatLog(ec.RegularCount)
72 case EpisodeTypeSpecial:
73 parts[i] = er.FormatLog(ec.SpecialCount)
74 case EpisodeTypeCredits:
75 parts[i] = er.FormatLog(ec.CreditsCount)
76 case EpisodeTypeOther:
77 parts[i] = er.FormatLog(ec.OtherCount)
78 case EpisodeTypeTrailer:
79 parts[i] = er.FormatLog(ec.TrailerCount)
80 case EpisodeTypeParody:
81 parts[i] = er.FormatLog(ec.ParodyCount)
83 parts[i] = er.Format(er.scale())
87 return strings.Join(parts, ",")
90 // Returns true if any of the contained EpisodeRanges contain the
91 // given EpisodeContainer.
92 func (el EpisodeList) ContainsEpisodes(ec EpisodeContainer) bool {
93 for _, i := range el {
94 if i != nil && i.ContainsEpisodes(ec) {
101 // Parses a string in the AniDB API list format and converts into
104 // ParseEpisodeList("01") <=> EpisodeList{ParseEpisodeRange("01")}
105 // ParseEpisodeList("S2-S3") <=> EpisodeList{ParseEpisodeRange("S2-S3")}
106 // ParseEpisodeList("T1,C1-C3") <=> EpisodeList{ParseEpisodeRange("T1"), ParseEpisodeRange("C1-C3")}
107 func ParseEpisodeList(s string) (el EpisodeList) {
108 parts := strings.Split(s, ",")
110 el = make(EpisodeList, len(parts))
111 for i := range parts {
112 el[i] = ParseEpisodeRange(parts[i])
118 // Returns a simplified version of the EpisodeList (removes nil ranges, merges mergeable ranges, sorts).
119 func (el EpisodeList) Simplify() EpisodeList {
120 nl := make(EpisodeList, 0, len(el))
123 for _, er := range el {
130 for n, changed := 0, true; changed; n++ {
132 used := map[int]bool{}
135 for i, a := range nl {
139 for j, b := range nl[i+1:] {
140 if c := a.Merge(b); c != nil {
151 panic(fmt.Sprintf("Too many iterations (%d) when simplifing %s!", n, el))
158 func (el EpisodeList) CountEpisodes() (ec EpisodeCount) {
159 for _, er := range el {
162 case EpisodeTypeRegular:
164 case EpisodeTypeSpecial:
166 case EpisodeTypeCredits:
168 case EpisodeTypeOther:
170 case EpisodeTypeTrailer:
172 case EpisodeTypeParody:
184 *c += er.End.Number - er.Start.Number
189 func (el EpisodeList) Len() int {
193 func (el EpisodeList) Less(i, j int) bool {
199 case el[i].Type < el[j].Type:
201 case el[i].Type > el[j].Type:
203 case el[i].Start.Number < el[j].Start.Number:
209 func (el EpisodeList) Swap(i, j int) {
210 el[i], el[j] = el[j], el[i]
213 func (el *EpisodeList) Add(ec EpisodeContainer) {
214 *el = append(*el, ContainerToList(ec)...)
218 func (el *EpisodeList) Sub(ep *Episode) {
219 el2 := make(EpisodeList, 0, len(*el))
220 for _, r := range *el {
221 el2 = append(el2, r.Split(ep)...)
223 *el = append(*el, el2.Simplify()...)