]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - include/CbDataList.h
SourceFormat Enforcement
[thirdparty/squid.git] / include / CbDataList.h
index 0aa43dba4021bfc22aac923ec97a86cf27d5a84e..a7376416e25ccfa840715374bd41c1b7edf37ef5 100644 (file)
@@ -1,7 +1,4 @@
 /*
- * $Id: List.h,v 1.8 2008/02/26 21:49:33 amosjeffries Exp $
- *
- *
  * SQUID Web Proxy Cache          http://www.squid-cache.org/
  * ----------------------------------------------------------
  *
  *  it under the terms of the GNU General Public License as published by
  *  the Free Software Foundation; either version 2 of the License, or
  *  (at your option) any later version.
- *  
+ *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
- *  
+ *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
@@ -47,8 +44,14 @@ public:
     CbDataList (C const &);
     ~CbDataList();
 
+    /// If element is already in the list, returns false.
+    /// Otherwise, adds the element to the end of the list and returns true.
+    /// Exists to avoid double iteration of find() and push() combo.
+    bool push_back_unique(C const &element);
     bool find(C const &)const;
     bool findAndTune(C const &);
+    /// Iterates the entire list to return the last element holder.
+    CbDataList *tail();
     CbDataList *next;
     C element;
     bool empty() const { return this == NULL; }
@@ -79,13 +82,13 @@ class CbDataListIterator
 public:
     CbDataListIterator(CbDataListContainer<C> const &list) : next_entry(list.head) {}
     const C & next() {
-       CbDataList<C> *entry = next_entry;
-       if (entry)
-           next_entry = entry->next;
-       return entry->element;
+        CbDataList<C> *entry = next_entry;
+        if (entry)
+            next_entry = entry->next;
+        return entry->element;
     }
     bool end() {
-       return next_entry == NULL;
+        return next_entry == NULL;
     }
 
 private:
@@ -128,6 +131,29 @@ CbDataList<C>::~CbDataList()
         delete next;
 }
 
+template <class C>
+bool
+CbDataList<C>::push_back_unique(C const &toAdd)
+{
+    CbDataList<C> *last;
+    for (last = this; last->next; last = last->next) {
+        if (last->element == toAdd)
+            return false;
+    }
+
+    last->next = new CbDataList<C>(toAdd);
+    return true;
+}
+
+template <class C>
+CbDataList<C> *
+CbDataList<C>::tail()
+{
+    CbDataList<C> *last;
+    for (last = this; last->next; last = last->next);
+    return last;
+}
+
 template <class C>
 bool
 CbDataList<C>::find (C const &toFind) const
@@ -148,7 +174,7 @@ CbDataList<C>::findAndTune(C const & toFind)
     CbDataList<C> *prev = NULL;
 
     for (CbDataList<C> *node = this; node; node = node->
-                                            next) {
+            next) {
         if (node->element == toFind) {
             if (prev != NULL) {
                 /* shift the element just found to the second position