| 1 | /* |
|---|
| 2 | * QuteCom, a voice over Internet phone |
|---|
| 3 | * Copyright (C) 2004-2010 Mbdsys |
|---|
| 4 | * |
|---|
| 5 | * This program is free software; you can redistribute it and/or modify |
|---|
| 6 | * it under the terms of the GNU General Public License as published by |
|---|
| 7 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | * (at your option) any later version. |
|---|
| 9 | * |
|---|
| 10 | * This program is distributed in the hope that it will be useful, |
|---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | * GNU General Public License for more details. |
|---|
| 14 | * |
|---|
| 15 | * You should have received a copy of the GNU General Public License |
|---|
| 16 | * along with this program; if not, write to the Free Software |
|---|
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | #ifndef OWLIST_H |
|---|
| 21 | #define OWLIST_H |
|---|
| 22 | |
|---|
| 23 | #include <vector> |
|---|
| 24 | #include <algorithm> |
|---|
| 25 | |
|---|
| 26 | /** |
|---|
| 27 | * List. |
|---|
| 28 | * |
|---|
| 29 | * @see java.util.List<E> |
|---|
| 30 | * @see QList |
|---|
| 31 | * @author Tanguy Krotoff |
|---|
| 32 | */ |
|---|
| 33 | template<typename T> |
|---|
| 34 | class List : public std::vector<T> { |
|---|
| 35 | public: |
|---|
| 36 | |
|---|
| 37 | /** |
|---|
| 38 | * Appends the specified element to the end of this list. |
|---|
| 39 | * |
|---|
| 40 | * @param element element to be appended to this list |
|---|
| 41 | */ |
|---|
| 42 | void operator+=(const T & element) { |
|---|
| 43 | this->push_back(element); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | /** |
|---|
| 47 | * Removes the first occurrence in this list of the specified element. |
|---|
| 48 | * |
|---|
| 49 | * Does not delete the element, just remove it from the list. |
|---|
| 50 | * |
|---|
| 51 | * @param element to remove from the list |
|---|
| 52 | * @return true if the element was removed; false otherwise |
|---|
| 53 | */ |
|---|
| 54 | bool remove(const T & element) { |
|---|
| 55 | typename std::vector<T>::iterator it = std::find(this->begin(), this->end(), element); |
|---|
| 56 | if (it != this->end()) { |
|---|
| 57 | this->erase(it); |
|---|
| 58 | return true; |
|---|
| 59 | } |
|---|
| 60 | return false; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | /** |
|---|
| 64 | * @see remove() |
|---|
| 65 | */ |
|---|
| 66 | bool operator-=(const T & element) { |
|---|
| 67 | return remove(element); |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | /** |
|---|
| 71 | * Gets the number of occurrences of an element contained in this list. |
|---|
| 72 | * |
|---|
| 73 | * @param element element to find inside this list |
|---|
| 74 | * @return number of occurrences of the specified element contained in this list |
|---|
| 75 | */ |
|---|
| 76 | unsigned contains(const T & element) const { |
|---|
| 77 | unsigned j = 0; |
|---|
| 78 | for (unsigned i = 0; i < this->size(); i++) { |
|---|
| 79 | if ((*this)[i] == element) { |
|---|
| 80 | j++; |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | return j; |
|---|
| 85 | } |
|---|
| 86 | }; |
|---|
| 87 | |
|---|
| 88 | #endif //OWLIST_H |
|---|