From 3b69903d230acda0e336b1528d9437360e24d82e Mon Sep 17 00:00:00 2001
From: Klaus Schmidinger <vdr@tvdr.de>
Date: Sun, 29 May 2005 10:33:21 +0200
Subject: Speeded up deleting duplicate channels

---
 HISTORY    |  3 ++-
 channels.c | 45 ++++++++++++++++++++++++++++++---------------
 2 files changed, 32 insertions(+), 16 deletions(-)

diff --git a/HISTORY b/HISTORY
index c2ebcaa7..287ab693 100644
--- a/HISTORY
+++ b/HISTORY
@@ -3513,7 +3513,7 @@ Video Disk Recorder Revision History
 - Fixed a wrong inheritance in libsi's SubtitlingDescriptor::Subtitling (thanks to
   Marco Schl��ler).
 
-2005-05-28: Version 1.3.25
+2005-05-29: Version 1.3.25
 
 - Updated the Estonian OSD texts (thanks to Arthur Konovalov).
 - Some cable providers don't mark short channel names according to the standard,
@@ -3572,3 +3572,4 @@ Video Disk Recorder Revision History
   a patch from Georg Acher).
 - Avoiding unnecessary calls to getLength() in libsi/si.c, and avoiding the
   '& 0xff' in CRC32::crc32() of libsi/util.c (thanks to Georg Acher).
+- Speeded up deleting duplicate channels.
diff --git a/channels.c b/channels.c
index 084b6996..b2d741e6 100644
--- a/channels.c
+++ b/channels.c
@@ -4,7 +4,7 @@
  * See the main source file 'vdr.c' for copyright information and
  * how to reach the author.
  *
- * $Id: channels.c 1.41 2005/05/28 13:55:57 kls Exp $
+ * $Id: channels.c 1.42 2005/05/29 10:32:38 kls Exp $
  */
 
 #include "channels.h"
@@ -804,6 +804,22 @@ bool cChannel::Save(FILE *f)
   return fprintf(f, "%s", *ToText()) > 0;
 }
 
+// -- cChannelSorter ---------------------------------------------------------
+
+class cChannelSorter : public cListObject {
+public:
+  cChannel *channel;
+  tChannelID channelID;
+  cChannelSorter(cChannel *Channel) {
+    channel = Channel;
+    channelID = channel->GetChannelID();
+    }
+  virtual int Compare(const cListObject &ListObject) const {
+    cChannelSorter *cs = (cChannelSorter *)&ListObject;
+    return memcmp(&channelID, &cs->channelID, sizeof(channelID));
+    }
+  };
+
 // -- cChannels --------------------------------------------------------------
 
 cChannels Channels;
@@ -816,22 +832,21 @@ cChannels::cChannels(void)
 
 void cChannels::DeleteDuplicateChannels(void)
 {
+  cList<cChannelSorter> ChannelSorter;
   for (cChannel *channel = First(); channel; channel = Next(channel)) {
-      if (!channel->GroupSep()) {
-         tChannelID ChannelID = channel->GetChannelID();
-         cChannel *other = Next(channel);
-         while (other) {
-               cChannel *d = NULL;
-               if (!other->GroupSep() && other->GetChannelID() == ChannelID)
-                  d = other;
-               other = Next(other);
-               if (d) {
-                  dsyslog("deleting duplicate channel %s", *d->ToText());
-                  Del(d);
-                  }
-               }
-         }
+      if (!channel->GroupSep())
+         ChannelSorter.Add(new cChannelSorter(channel));
       }
+  ChannelSorter.Sort();
+  cChannelSorter *cs = ChannelSorter.First();
+  while (cs) {
+        cChannelSorter *next = ChannelSorter.Next(cs);
+        if (next && cs->channelID == next->channelID) {
+           dsyslog("deleting duplicate channel %s", *next->channel->ToText());
+           Del(next->channel);
+           }
+        cs = next;
+        }
 }
 
 bool cChannels::Load(const char *FileName, bool AllowComments, bool MustExist)
-- 
cgit v1.2.3