library of assembled shared sources

http://lass.cocamware.com

iterator_range.inl

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 namespace lass
00044 {
00045 namespace stde
00046 {
00047 
00048 // --- public --------------------------------------------------------------------------------------
00049 
00050 /** @brief empty range (sort of, read warning).
00051  *
00052  *  @warning Problemski: MSVC has something like _HAS_ITERATOR_DEBUGGING, which means it has the 
00053  *      annoying habit of whenever two iterators come together (e.g. on comparison), to check
00054  *      whether they belong to the same container.  While this makes sense in most situations,
00055  *      (ignoring any possible performance hit), we don't have any container when we create
00056  *      empty ranges.  Oh, did I tell you it also checks if the iterator belongs to ANY container
00057  *      AT ALL?.  So, even while we almost physically force first_ and last_ to be equal (think 
00058  *      Fat Ed's super fix-it way), MSVC will bork on almost any occasion.  
00059  *      Moral of the story: while you can _create_ default constructed ranges, DO NOT USE THEM, 
00060  *      like EVER!
00061  */
00062 #pragma LASS_FIXME("Find a workaround for this containerless _HAS_ITERATOR_DEBUGGING madness [Bramz]")
00063 template <typename I> inline
00064 iterator_range<I>::iterator_range(): 
00065     first_()
00066 {
00067     // a default range is empty, so make sure both iterators are really the same!
00068     //
00069     last_ = first_;
00070 }
00071 
00072 
00073 
00074 template <typename I> inline
00075 iterator_range<I>::iterator_range(iterator first, iterator last): 
00076     first_(first), 
00077     last_(last) 
00078 {
00079 }
00080 
00081 
00082 
00083 template <typename I> inline
00084 typename iterator_range<I>::iterator 
00085 iterator_range<I>::begin() const 
00086 { 
00087     return first_; 
00088 }
00089 
00090 
00091 
00092 template <typename I> inline
00093 typename iterator_range<I>::iterator 
00094 iterator_range<I>::end() const 
00095 { 
00096     return last_; 
00097 }
00098 
00099 
00100 
00101 template <typename I> inline
00102 typename iterator_range<I>::iterator 
00103 iterator_range<I>::operator->() const 
00104 { 
00105     LASS_ASSERT(!this->empty()); 
00106     return first_; 
00107 }
00108 
00109 
00110 
00111 template <typename I> inline
00112 typename iterator_range<I>::reference 
00113 iterator_range<I>::operator*() const 
00114 { 
00115     LASS_ASSERT(!this->empty()); 
00116     return *first_; 
00117 }
00118 
00119 
00120 
00121 template <typename I> inline
00122 typename iterator_range<I>::reference 
00123 iterator_range<I>::operator[](difference_type index) const
00124 { 
00125     LASS_ASSERT(index < this->size());
00126     iterator result = first_;
00127     std::advance(result, index);
00128     return *result;
00129 }
00130 
00131 
00132 
00133 template <typename I> inline
00134 iterator_range<I>& 
00135 iterator_range<I>::operator++() 
00136 { 
00137     LASS_ASSERT(first_ != last_); 
00138     ++first_; 
00139     return *this; 
00140 }
00141 
00142 
00143 
00144 template <typename I> inline
00145 iterator_range<I>
00146 iterator_range<I>::operator++(int) 
00147 { 
00148     iterator_range<I> result(*this); 
00149     this->operator++(); 
00150     return result; 
00151 }
00152 
00153 
00154 
00155 template <typename I> inline
00156 const typename iterator_range<I>::difference_type 
00157 iterator_range<I>::size() const 
00158 {
00159     return std::distance(first_, last_);
00160 }
00161 
00162 
00163 
00164 template <typename I> inline
00165 const bool iterator_range<I>::empty() const 
00166 { 
00167     return first_ == last_; 
00168 }
00169 
00170 
00171 
00172 template <typename I> inline
00173 const bool iterator_range<I>::operator!() const 
00174 { 
00175     return this->empty(); 
00176 }
00177 
00178 
00179 
00180 template <typename I> inline
00181 iterator_range<I>::operator const num::SafeBool() const
00182 { 
00183     return this->empty() ? num::safeFalse : num::safeTrue; 
00184 }
00185 
00186 
00187 
00188 template <typename I> inline
00189 void iterator_range<I>::swap(iterator_range<I>& other)
00190 {
00191     std::swap(first_, other.first_);
00192     std::swap(last_, other.last_);
00193 }
00194 
00195 
00196 
00197 // --- protected -----------------------------------------------------------------------------------
00198 
00199 
00200 
00201 // --- private -------------------------------------------------------------------------------------
00202 
00203 
00204 
00205 // --- free functions ------------------------------------------------------------------------------
00206 
00207 template <typename I> inline
00208 iterator_range<I> range(const I& a, const I& b)
00209 {
00210     return iterator_range<I>(a, b);
00211 }
00212 
00213 
00214 
00215 template <typename I> inline
00216 const bool operator==(const iterator_range<I>& a, const iterator_range<I>& b)
00217 {
00218     return a.begin() == b.begin() && a.end() == b.end();
00219 }
00220 
00221 
00222 
00223 template <typename I> inline
00224 const bool operator!=(const iterator_range<I>& a, const iterator_range<I>& b)
00225 {
00226     return !(a == b);
00227 }
00228 
00229 
00230 
00231 }
00232 
00233 }
00234 
00235 // EOF

Generated on Mon Nov 10 14:20:05 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo