]> git.lizzy.rs Git - go-anidb.git/commitdiff
misc: (*EpisodeList).Sub: allow subtracting arbitrary containers
authorDiogo Franco (Kovensky) <diogomfranco@gmail.com>
Wed, 17 Jul 2013 20:55:47 +0000 (17:55 -0300)
committerDiogo Franco (Kovensky) <diogomfranco@gmail.com>
Wed, 17 Jul 2013 20:55:47 +0000 (17:55 -0300)
misc/episodelist.go
misc/episoderange.go

index 10747a30e1f90a4503b37345132b13495dac0b49..9bc9036223adb17f4d820c335ac6f62367c8a702 100644 (file)
@@ -255,10 +255,28 @@ func (el *EpisodeList) Add(ec EpisodeContainer) {
        *el = el.Simplify()
 }
 
-func (el *EpisodeList) Sub(ep *Episode) {
-       el2 := make(EpisodeList, 0, len(*el))
-       for _, r := range *el {
-               el2 = append(el2, r.Split(ep)...)
+func (el *EpisodeList) Sub(ec EpisodeContainer) {
+       el2 := make(EpisodeList, 0, len(*el)*2)
+       switch e, ok := ec.(canInfinite); {
+       case ok:
+               if e.Infinite() {
+                       eCh := e.Episodes()
+                       ep := <-eCh
+                       close(eCh)
+
+                       for _, r := range *el {
+                               el2 = append(el2, r.Split(&ep)[0])
+                       }
+                       break
+               }
+               fallthrough
+       default:
+               for ep := range ec.Episodes() {
+                       for _, r := range *el {
+                               el2 = append(el2, r.Split(&ep)...)
+                       }
+                       el2 = el2.Simplify()
+               }
        }
        *el = append(*el, el2.Simplify()...)
 }
index 8645fba24d4e94c5473aae0f125f217bf694a4e0..8a32017c8879fe989eb808b165463978a7628657 100644 (file)
@@ -5,6 +5,11 @@ import (
        "strings"
 )
 
+type canInfinite interface {
+       EpisodeContainer
+       Infinite() bool
+}
+
 // A range of episodes with a start and possibly without an end.
 type EpisodeRange struct {
        Type  EpisodeType // Must be equal to both the Start and End types; if End is nil, must be equal to the Start type