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_CONVERTER_STDVECTOR_H_ 00036 #define KEYVALUE_KEY_CONVERTER_STDVECTOR_H_ 00037 00038 #include "keyvalue/extern/SharedPtr.h" 00039 #include "keyvalue/extern/Vector.h" 00040 #include "keyvalue/sys/exception/KV_ASSERT.h" 00041 #include "keyvalue/value/Variant.h" 00042 #include "keyvalue/value/Vector.h" 00043 00044 namespace keyvalue { 00045 namespace key { 00046 00063 template <typename ElementType> 00064 class StdVector { 00065 00066 public: 00067 00071 typedef value::Vector InputType_; 00072 00079 typedef shared_ptr<vector<ElementType> > OutputType_; 00080 00093 StdVector(const InputType_& input, OutputType_* cache); 00094 00101 bool 00102 isEmpty() const; 00103 00109 value::Variant 00110 pop(); 00111 00118 void 00119 insert(const ElementType& element); 00120 00126 OutputType_ 00127 getOutput() const; 00128 00135 bool 00136 mustUpdate() const; 00137 00138 private: 00139 00140 const InputType_& input_; 00141 OutputType_ output_; 00142 OutputType_* const cache_; 00143 const size_t size_; 00144 size_t i_; 00145 bool mustUpdate_; 00146 00147 }; 00148 00149 /*-------------------------------------------------------------------------- 00150 * StdVector() 00151 *------------------------------------------------------------------------*/ 00152 00153 template <typename ElementType> 00154 StdVector<ElementType>::StdVector(const InputType_& input, 00155 OutputType_* const cache) : 00156 input_(input), 00157 output_(new vector<ElementType>), 00158 cache_(cache), 00159 size_(input.getSize()), 00160 i_(0), 00161 mustUpdate_(!cache) { 00162 00163 output_->reserve(size_); 00164 mustUpdate_ = mustUpdate_ || size_ != (*cache_)->size(); 00165 } 00166 00167 /*-------------------------------------------------------------------------- 00168 * end() 00169 *------------------------------------------------------------------------*/ 00170 00171 template <typename ElementType> 00172 bool 00173 StdVector<ElementType>::isEmpty() const { 00174 return i_ == size_; 00175 } 00176 00177 /*-------------------------------------------------------------------------- 00178 * pop() 00179 *------------------------------------------------------------------------*/ 00180 00181 template <typename ElementType> 00182 value::Variant 00183 StdVector<ElementType>::pop() { 00184 KV_ASSERT(!isEmpty(), "All elements have been processed!"); 00185 return input_(i_++); 00186 } 00187 00188 /*-------------------------------------------------------------------------- 00189 * insert() 00190 *------------------------------------------------------------------------*/ 00191 00192 template <typename ElementType> 00193 void 00194 StdVector<ElementType>::insert(const ElementType& element) { 00195 output_->push_back(element); 00196 KV_ASSERT(i_ <= size_, "Calls to StdVector.pop() and StdVector.insert() " 00197 "must be intercalated!"); 00198 mustUpdate_ = mustUpdate_ || (*(*cache_))[i_-1] != element; 00199 } 00200 00201 /*-------------------------------------------------------------------------- 00202 * getOutput() 00203 *------------------------------------------------------------------------*/ 00204 00205 template <typename ElementType> 00206 typename StdVector<ElementType>::OutputType_ 00207 StdVector<ElementType>::getOutput() const { 00208 return output_; 00209 } 00210 00211 /*-------------------------------------------------------------------------- 00212 * mustUpdate() 00213 *------------------------------------------------------------------------*/ 00214 00215 template <typename ElementType> 00216 bool 00217 StdVector<ElementType>::mustUpdate() const { 00218 return mustUpdate_; 00219 } 00220 00221 } // namespace key 00222 } // namespace keyvalue 00223 00224 #endif // KEYVALUE_KEY_CONVERTER_STDVECTOR_H_