/*
- * $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.
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; }
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:
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
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