00001 /*************************************************************************** 00002 * 00003 * Copyright (C) 2009-2010 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 **************************************************************************/ 00021 00029 #ifndef KEYVALUE_KEY_CONVERTER_STDVECTOR_H_ 00030 #define KEYVALUE_KEY_CONVERTER_STDVECTOR_H_ 00031 00032 #include "keyvalue/extern/SharedPtr.h" 00033 #include "keyvalue/extern/Vector.h" 00034 #include "keyvalue/sys/exception/KV_ASSERT.h" 00035 #include "keyvalue/value/Variant.h" 00036 #include "keyvalue/value/Vector.h" 00037 00038 namespace keyvalue { 00039 namespace key { 00040 00057 template <typename ElementType> 00058 class StdVector { 00059 00060 public: 00061 00065 typedef value::Vector InputType_; 00066 00073 typedef shared_ptr<std::vector<ElementType> > OutputType_; 00074 00087 StdVector(const InputType_& input, OutputType_* cache); 00088 00095 bool 00096 isEmpty() const; 00097 00103 value::Variant 00104 pop(); 00105 00112 void 00113 insert(const ElementType& element); 00114 00120 OutputType_ 00121 getOutput() const; 00122 00129 bool 00130 mustUpdate() const; 00131 00132 private: 00133 00134 const InputType_& input_; 00135 OutputType_ output_; 00136 OutputType_* const cache_; 00137 const size_t size_; 00138 size_t i_; 00139 bool mustUpdate_; 00140 00141 }; 00142 00143 /*-------------------------------------------------------------------------- 00144 * StdVector() 00145 *------------------------------------------------------------------------*/ 00146 00147 template <typename ElementType> 00148 StdVector<ElementType>::StdVector(const InputType_& input, 00149 OutputType_* const cache) : input_(input), 00150 output_(new std::vector<ElementType>), cache_(cache), 00151 size_(input.getSize()), i_(0), mustUpdate_(!cache) { 00152 00153 output_->reserve(size_); 00154 mustUpdate_ = mustUpdate_ || size_ != (*cache_)->size(); 00155 } 00156 00157 /*-------------------------------------------------------------------------- 00158 * end() 00159 *------------------------------------------------------------------------*/ 00160 00161 template <typename ElementType> 00162 bool 00163 StdVector<ElementType>::isEmpty() const { 00164 return i_ == size_; 00165 } 00166 00167 /*-------------------------------------------------------------------------- 00168 * pop() 00169 *------------------------------------------------------------------------*/ 00170 00171 template <typename ElementType> 00172 value::Variant 00173 StdVector<ElementType>::pop() { 00174 KV_ASSERT(!isEmpty(), "All elements have been processed!"); 00175 return input_(i_++); 00176 } 00177 00178 /*-------------------------------------------------------------------------- 00179 * insert() 00180 *------------------------------------------------------------------------*/ 00181 00182 template <typename ElementType> 00183 void 00184 StdVector<ElementType>::insert(const ElementType& element) { 00185 output_->push_back(element); 00186 KV_ASSERT(i_ <= size_, "Calls to StdVector.pop() and StdVector.insert() " 00187 "must be intercalated!"); 00188 mustUpdate_ = mustUpdate_ || (*(*cache_))[i_-1] != element; 00189 } 00190 00191 /*-------------------------------------------------------------------------- 00192 * getOutput() 00193 *------------------------------------------------------------------------*/ 00194 00195 template <typename ElementType> 00196 typename StdVector<ElementType>::OutputType_ 00197 StdVector<ElementType>::getOutput() const { 00198 return output_; 00199 } 00200 00201 /*-------------------------------------------------------------------------- 00202 * mustUpdate() 00203 *------------------------------------------------------------------------*/ 00204 00205 template <typename ElementType> 00206 bool 00207 StdVector<ElementType>::mustUpdate() const { 00208 return mustUpdate_; 00209 } 00210 00211 } // namespace key 00212 } // namespace keyvalue 00213 00214 #endif // KEYVALUE_KEY_CONVERTER_STDVECTOR_H_