MonotoneBoundedVector.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *
00003  * Copyright (C) 2009-2011 Cassio Neri Moreira
00004  *
00005  * This file is part of the KeyValue library.
00006  *
00007  * The KeyValue library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License as published
00009  * by the Free Software Foundation, either version 3 of the License, or (at
00010  * your option) any later version.
00011  *
00012  * The KeyValue library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
00015  * Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License along
00018  * with KeyValue. If not, see <http://www.gnu.org/licenses/>.
00019  *
00020  * If you modify this library, or any covered work, by linking or combining
00021  * it with Excel (or a modified version of that program), containing parts
00022  * covered by the terms of End-User License Agreement for Microsoft
00023  * Software, the licensors of KeyValue grant you additional permission to
00024  * convey the resulting work.
00025  *
00026  **************************************************************************/
00027 
00035 #ifndef KEYVALUE_KEY_GENERIC_MONOTONEBOUNDEDVECTOR_H_
00036 #define KEYVALUE_KEY_GENERIC_MONOTONEBOUNDEDVECTOR_H_
00037 
00038 #include "keyvalue/key/converter/StdVector.h"
00039 #include "keyvalue/key/generic/bound/NoBound.h"
00040 #include "keyvalue/key/Traits.h"
00041 
00042 namespace keyvalue {
00043 namespace key {
00044 
00111 template <typename ElementType, typename Monotone,
00112   template <typename> class Bound1 = NoBound,
00113   template <typename> class Bound2 = NoBound>
00114 class MonotoneBoundedVector: public Traits<ElementType, StdVector, NoMap> {
00115 
00116 public:
00117 
00127   explicit
00128   MonotoneBoundedVector(const string& name, size_t size = 0);
00129 
00140   MonotoneBoundedVector(const string& name, const ElementType& bound1,
00141     size_t size = 0);
00142 
00154   MonotoneBoundedVector(const string& name, const ElementType& bound1,
00155     const ElementType& bound2, size_t size = 0);
00156 
00165   void
00166   checkSoFar(const StdVector<ElementType>& container) const;
00167 
00177   void
00178   checkSize(size_t size) const;
00179 
00180 private:
00181 
00182   const size_t              size_;
00183   const Bound1<ElementType> bound1_;
00184   const Bound2<ElementType> bound2_;
00185 
00186 }; // class MonotoneBoundedVector
00187 
00188 /*--------------------------------------------------------------------------
00189  * MonotoneBoundedVector()
00190  *------------------------------------------------------------------------*/
00191 
00192 template <typename ElementType, typename Monotone,
00193   template <typename> class Bound1, template <typename> class Bound2>
00194 MonotoneBoundedVector<ElementType, Monotone, Bound1,
00195   Bound2>::MonotoneBoundedVector(const string& name, size_t size) :
00196   MonotoneBoundedVector::Traits_(name),
00197   size_(size),
00198   bound1_(),
00199   bound2_() {
00200 }
00201 
00202 template <typename ElementType, typename Monotone,
00203   template <typename> class Bound1, template <typename> class Bound2>
00204 MonotoneBoundedVector<ElementType, Monotone, Bound1,
00205   Bound2>::MonotoneBoundedVector(const string& name,
00206   const ElementType& bound1, size_t size) :
00207   MonotoneBoundedVector::Traits_(name),
00208   size_(size),
00209   bound1_(bound1),
00210   bound2_() {
00211 }
00212 
00213 template <typename ElementType, typename Monotone,
00214   template <typename> class Bound1, template <typename> class Bound2>
00215 MonotoneBoundedVector<ElementType, Monotone, Bound1,
00216   Bound2>::MonotoneBoundedVector(const string& name,
00217   const ElementType& bound1, const ElementType& bound2, size_t size) :
00218   MonotoneBoundedVector::Traits_(name),
00219   size_(size),
00220   bound1_(bound1),
00221   bound2_(bound2) {
00222 }
00223 
00224 /*--------------------------------------------------------------------------
00225  * checkSoFar()
00226  *------------------------------------------------------------------------*/
00227 
00228 template <typename ElementType, typename Monotone,
00229   template <typename> class Bound1, template <typename> class Bound2>
00230 void
00231 MonotoneBoundedVector<ElementType, Monotone, Bound1, Bound2>::checkSoFar(
00232   const StdVector<ElementType>& container) const {
00233 
00234   const vector<ElementType>& vector(*container.getOutput());
00235   const size_t size(vector.size());
00236 
00237   // Check monotonicity
00238   if (size > 1 && !Monotone::check(vector[size-2], vector[size-1]))
00239     throw RuntimeError() & "Values for key '" & Key::getName() &
00240       "' must be in " & Monotone::getName() & " order. Got " &
00241       vector[size-2] & " followed by " & vector[size-1] & "!";
00242 
00243   // Check boundness
00244   if (!bound1_.check(vector.back()))
00245     throw RuntimeError() & "Value for key '" & Key::getName() & "' must "
00246       "be " & bound1_.getName() & " " & bound1_.getBound() & ". Got " &
00247       vector.back() & "!";
00248 
00249   if (!bound2_.check(vector.back()))
00250     throw RuntimeError() & "Value for key '" & Key::getName() & "' must "
00251       "be " & bound2_.getName() & " " & bound2_.getBound() & ". Got " &
00252       vector.back() & "!";
00253 }
00254 
00255 /*--------------------------------------------------------------------------
00256  * checkSize()
00257  *------------------------------------------------------------------------*/
00258 
00259 template <typename ElementType, typename Monotone,
00260   template <typename> class Bound1, template <typename> class Bound2>
00261 void
00262 MonotoneBoundedVector<ElementType, Monotone, Bound1, Bound2>::checkSize(
00263   size_t size) const {
00264 
00265   if (size_ != 0 && size_ != size)
00266     throw RuntimeError() & "Invalid number of values assigned to key '" &
00267       Key::getName() & "'! Expecting " & size_ & " got " & size & "!";
00268 }
00269 
00270 } // namespace key
00271 } // namespace keyvalue
00272 
00273 #endif // KEYVALUE_KEY_GENERIC_MONOTONEBOUNDEDVECTOR_H_