1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of the copyright holder nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
37 #ifndef anna_core_util_SortedVector_hpp
38 #define anna_core_util_SortedVector_hpp
42 #include <anna/core/RuntimeException.hpp>
43 #include <anna/config/defines.hpp>
48 Patron para ordenar instancias de objetos en base de una clave.
50 \param T Clase del patron.
51 \param SortBy Clase que ofrece el valor por el que ordenar. Debe implementar un metodo constante con la
52 signatura: TKey value (const T*)
53 \param TKey Tipo de clave usado para calcular la ordenacion. Debe implementar los operadores '=', '<' y '==' y el
56 \warning no actua como clase segura en MT, ver #anna::SafeSortedVector.
58 template < typename T, typename SortBy, typename TKey = int > class SortedVector : public std::map <TKey, T*> {
60 typedef typename std::map <TKey, T*> container;
61 typedef typename container::iterator iterator;
62 typedef typename container::const_iterator const_iterator;
63 typedef typename container::value_type value_type;
72 \param other Instancia de la que copiar.
74 explicit SortedVector(const SortedVector& other) :
80 virtual ~SortedVector() { container::clear(); }
83 Devolvera \em true si la instancia recibida como parametro esta contenido en el
84 map o \em en otro caso. Si la instancia recibida es NULL siempre devolvera \em false.
85 \param t Instancia a comprobar.
86 \return \em true si la instancia recibida como parametro esta contenido en el
87 map o \em en otro caso.
89 bool contains(const T* t) const
94 TKey key(SortBy::value(t));
95 return find(key) != NULL;
99 Incorpora la instancia recibida como parametro en la lista ordenada de objetos.
100 \param t Instancia a guardar en el map. Si es NULL la operacion no tendra ningun efecto.
101 \return \em true si ha registrado la nueva instancia o \em false en otro caso.
104 throw(RuntimeException) {
108 TKey key(SortBy::value(t));
109 value_type v(key, t);
110 std::pair <iterator, bool> result = container::insert(v);
111 return result.second;
115 Elimina la instancia recibida como parametro de la lista ordenada de objetos.
116 \param t Instancia a guardar en el map. Si es NULL la operacion no tendra ningun efecto.
117 \return \em true si ha eliminado la instancia o \em false en otro caso.
120 throw(RuntimeException) {
125 TKey key(SortBy::value(t));
126 iterator ii = container::find(key);
128 if(ii != container::end()) {
129 container::erase(ii);
137 Devuelve la instancia asociada a la clave recibida como parametro o NULL si no existe.
138 \param key Clave a buscar en el map.
139 \return la instancia asociada a la clave recibida como parametro o NULL si no existe.
141 T* find(const TKey key)
143 iterator ii = container::find(key);
144 return (ii == container::end()) ? NULL : ii->second;
148 Devuelve la instancia asociada a la clave recibida como parametro o NULL si no existe.
149 \param key Clave a buscar en el map.
150 \return la instancia asociada a la clave recibida como parametro o NULL si no existe.
152 const T* find(const TKey key) const throw() {
153 return const_cast <SortedVector <T, SortBy, TKey>*>(this)->find(key);
157 Devuelve el objeto referenciado por el iterador.
158 \return El objeto referenciado por el iterador.
160 static T* data(iterator ii) throw() { return ii->second; }
163 Devuelve el objeto referenciado por el iterador.
164 \return El objeto referenciado por el iterador.
166 static const T* data(const_iterator ii) throw() { return ii->second; }