/* * libpal - Automated Placement of Labels Library * * Copyright (C) 2008 Maxence Laurent, MIS-TIC, HEIG-VD * University of Applied Sciences, Western Switzerland * http://www.hes-so.ch * * Contact: * maxence.laurent heig-vd ch * or * eric.taillard heig-vd ch * * This file is part of libpal. * * libpal is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * libpal 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 libpal. If not, see . * */ #ifdef HAVE_CONFIG_H #include #endif #ifndef _LINKED_LIST_H #define _LINKED_LIST_H #include namespace pal { class Layer; /** * \brief Generic cell for LinkedList * \param Type Generic class */ template class Cell { public: /** \brief Create a new Cell * \param item The object to store */ Cell (Type item) : item (item), next (NULL) {}; Type item; Cell *next; }; /** * \brief Generic queue * \param Type generic class * \todo thread safe */ template class LinkedList { friend class Layer; private: Cell *first; Cell *last; int s; bool (*compare) (Type a, Type b); public: /** * \brief Create a new empty list */ LinkedList (bool (*compare) (Type a, Type b)) : first (NULL), last (NULL), s (0), compare (compare) {}; /** * \brief delete the list */ ~LinkedList(); //void setCompareMethod (bool (*compare)(Type a, Type b)); /** * \brief is item into list ? * \return true or false */ bool isIn (Type item); /** * \brief Insert an item at the end * \param item The item to insert */ void push_back (Type item); /** * \brief Insert an item in first position * \param item The item to insert */ void push_front (Type item); /** * \brief extract and return the last item * \return Extracted item */ Type pop_front(); /** * \brief get the list size * \return the number of item into the queue */ int size(); /** * \brief get the first cell * \return the first elem, as iterator */ Cell *getFirst(); Cell *search (Type item); void remove (Type item); void clean(); }; template LinkedList::~LinkedList() { Cell *cur; Cell *it; cur = first; while (cur) { it = cur; cur = cur->next; delete it; } } template void LinkedList::push_back (Type item) { if (s == 0) { first = new Cell (item); last = first; } else { last->next = new Cell (item); last = last->next; } s++; } template void LinkedList::push_front (Type item) { Cell *n = new Cell (item); n->next = first; first = n; s++; } templateType LinkedList::pop_front() { Type item; Cell *it; if (first) { it = first; item = it->item; first = first->next; delete it; s--; } else item = Type (0); return item; } template bool LinkedList::isIn (Type item) { Cell *cur = first; while (cur) { if (item == cur->item) return true; cur = cur->next; } return false; } template int LinkedList::size() { return s; } template Cell *LinkedList::getFirst() { return first; } template void LinkedList::remove (Type item) { Cell *p = first; Cell *q; if (first) { if (compare (item, p->item)) { first = p->next; s--; delete p; return; } while (p->next && !compare (p->next->item, item)) {p = p->next;} if (p->next) { q = p->next; p->next = q->next; s--; delete q; if (!p->next) last = p; } } } template Cell * LinkedList::search (Type item) { Cell *p = first; while (p && !compare (p->item, item)) { p = p->next; } return p; } /*template void LinkedList::setCompareMethod (bool (*compare)(Type a, Type b)){ this->compare = compare; }*/ template void LinkedList::clean() { Cell *it = first; Cell *cur; while (it) { cur = it; it = it->next; delete cur; } first = last = NULL; s = 0; } } // end namespace #endif