9 type EpisodeList []*EpisodeRange
11 // Converts the EpisodeList into the AniDB API list format.
12 func (el EpisodeList) String() string {
13 scales := map[EpisodeType]int{}
15 for _, er := range el {
21 if s > scales[er.Type] {
26 parts := make([]string, len(el))
27 for i, er := range el {
28 parts[i] = er.Format(scales[er.Type])
31 return strings.Join(parts, ",")
34 // Returns true if any of the contained EpisodeRanges contain the
35 // given EpisodeContainer.
36 func (el EpisodeList) ContainsEpisodes(ec EpisodeContainer) bool {
37 for _, i := range el {
38 if i != nil && i.ContainsEpisodes(ec) {
45 // Parses a string in the AniDB API list format and converts into
48 // ParseEpisodeList("01") <=> EpisodeList{ParseEpisodeRange("01")}
49 // ParseEpisodeList("S2-S3") <=> EpisodeList{ParseEpisodeRange("S2-S3")}
50 // ParseEpisodeList("T1,C1-C3") <=> EpisodeList{ParseEpisodeRange("T1"), ParseEpisodeRange("C1-C3")}
51 func ParseEpisodeList(s string) (el EpisodeList) {
52 parts := strings.Split(s, ",")
54 el = make(EpisodeList, len(parts))
55 for i := range parts {
56 el[i] = ParseEpisodeRange(parts[i])
62 // Returns a simplified version of the EpisodeList (removes nil ranges, merges mergeable ranges, sorts).
63 func (el EpisodeList) Simplify() EpisodeList {
64 nl := make(EpisodeList, 0, len(el))
67 for _, er := range el {
74 for n, changed := 0, true; changed; n++ {
76 used := map[int]bool{}
79 for i, a := range nl {
83 for j, b := range nl[i+1:] {
84 if c := a.Merge(b); c != nil {
95 panic(fmt.Sprintf("Too many iterations (%d) when simplifing %s!", n, el))
102 func (el EpisodeList) Len() int {
106 func (el EpisodeList) Less(i, j int) bool {
112 case el[i].Type < el[j].Type:
114 case el[i].Type > el[j].Type:
116 case el[i].Start.Number < el[j].Start.Number:
122 func (el EpisodeList) Swap(i, j int) {
123 el[i], el[j] = el[j], el[i]