Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

MNRopeConstIterator.h

Go to the documentation of this file.
00001 /* 00002 * Copyright (c) 1997 00003 * Silicon Graphics Computer Systems, Inc. 00004 * 00005 * Permission to use, copy, modify, distribute and sell this software 00006 * and its documentation for any purpose is hereby granted without fee, 00007 * provided that the above copyright notice appear in all copies and 00008 * that both that copyright notice and this permission notice appear 00009 * in supporting documentation. Silicon Graphics makes no 00010 * representations about the suitability of this software for any 00011 * purpose. It is provided "as is" without express or implied warranty. 00012 * 00013 * Heavily modified by KOM/Darmstadt University of Technology, 2000 00014 */ 00015 #ifndef _R_CONST_ITERATOR_H 00016 #define _R_CONST_ITERATOR_H 00017 00018 #include "MNRopeIterator.h" 00019 00024 class MNRopeConstIterator : public MNRopeIteratorBase 00025 { 00026 friend class MNRope; 00027 protected: 00028 MNRopeConstIterator(const MNRopeRep* __root, size_t __pos); 00029 public: 00030 typedef uchar_t reference; // Really a value. Returning a reference 00031 // Would be a mess, since it would have 00032 // to be included in refcount. 00033 typedef const uchar_t* pointer; 00034 00035 public: 00036 MNRopeConstIterator(); 00037 MNRopeConstIterator(const MNRopeConstIterator& __x); 00038 MNRopeConstIterator(const MNRopeIterator& __x); 00039 MNRopeConstIterator(const MNRope& __r, size_t __pos); 00040 00041 MNRopeConstIterator& operator= (const MNRopeConstIterator& __x); 00042 00043 reference operator*() { 00044 if (0 == _M_buf_ptr) _S_setcache(*this); 00045 return *_M_buf_ptr; 00046 } 00047 MNRopeConstIterator& operator++() { 00048 uchar_t* __next; 00049 if (0 != _M_buf_ptr && (__next = _M_buf_ptr + 1) < _M_buf_end) { 00050 _M_buf_ptr = __next; 00051 ++_M_current_pos; 00052 } else { 00053 _M_incr(1); 00054 } 00055 return *this; 00056 } 00057 MNRopeConstIterator& operator+=(ptrdiff_t __n) { 00058 if (__n >= 0) { 00059 _M_incr(__n); 00060 } else { 00061 _M_decr(-__n); 00062 } 00063 return *this; 00064 } 00065 MNRopeConstIterator& operator--() { 00066 _M_decr(1); 00067 return *this; 00068 } 00069 MNRopeConstIterator& operator-=(ptrdiff_t __n) { 00070 if (__n >= 0) { 00071 _M_decr(__n); 00072 } else { 00073 _M_incr(-__n); 00074 } 00075 return *this; 00076 } 00077 MNRopeConstIterator operator++(int) { 00078 size_t __old_pos = _M_current_pos; 00079 _M_incr(1); 00080 return MNRopeConstIterator(_M_root, __old_pos); 00081 // This makes a subsequent dereference expensive. 00082 // Perhaps we should instead copy the iterator 00083 // if it has a valid cache? 00084 } 00085 MNRopeConstIterator operator--(int) { 00086 size_t __old_pos = _M_current_pos; 00087 _M_decr(1); 00088 return MNRopeConstIterator(_M_root, __old_pos); 00089 } 00090 00091 friend MNRopeConstIterator operator- 00092 (const MNRopeConstIterator& __x, 00093 ptrdiff_t __n); 00094 friend MNRopeConstIterator operator+ 00095 (const MNRopeConstIterator& __x, 00096 ptrdiff_t __n); 00097 friend MNRopeConstIterator operator+ 00098 (ptrdiff_t __n, 00099 const MNRopeConstIterator& __x); 00100 00101 reference operator[](size_t __n); 00102 00103 friend bool operator== 00104 (const MNRopeConstIterator& __x, 00105 const MNRopeConstIterator& __y); 00106 friend bool operator< 00107 (const MNRopeConstIterator& __x, 00108 const MNRopeConstIterator& __y); 00109 friend ptrdiff_t operator- 00110 (const MNRopeConstIterator& __x, 00111 const MNRopeConstIterator& __y); 00112 }; 00113 00114 MNRopeConstIterator operator-(const MNRopeConstIterator& __x, ptrdiff_t __n); 00115 00116 MNRopeConstIterator operator+(const MNRopeConstIterator& __x, ptrdiff_t __n); 00117 00118 MNRopeConstIterator operator+(ptrdiff_t __n, const MNRopeConstIterator& __x); 00119 00120 bool operator== (const MNRopeConstIterator& __x, 00121 const MNRopeConstIterator& __y); 00122 00123 bool operator< (const MNRopeConstIterator& __x, 00124 const MNRopeConstIterator& __y); 00125 00126 ptrdiff_t operator- (const MNRopeConstIterator& __x, 00127 const MNRopeConstIterator& __y); 00128 00129 inline bool operator== (const MNRopeConstIterator& __x, 00130 const MNRopeConstIterator& __y) 00131 { 00132 return (__x._M_current_pos == __y._M_current_pos && 00133 __x._M_root == __y._M_root); 00134 } 00135 00136 inline bool operator< (const MNRopeConstIterator& __x, 00137 const MNRopeConstIterator& __y) 00138 { 00139 return (__x._M_current_pos < __y._M_current_pos); 00140 } 00141 00142 inline bool operator!= (const MNRopeConstIterator& __x, 00143 const MNRopeConstIterator& __y) 00144 { 00145 return !(__x == __y); 00146 } 00147 00148 inline bool operator> (const MNRopeConstIterator& __x, 00149 const MNRopeConstIterator& __y) 00150 { 00151 return __y < __x; 00152 } 00153 00154 inline bool operator<= (const MNRopeConstIterator& __x, 00155 const MNRopeConstIterator& __y) 00156 { 00157 return !(__y < __x); 00158 } 00159 00160 inline bool operator>= (const MNRopeConstIterator& __x, 00161 const MNRopeConstIterator& __y) 00162 { 00163 return !(__x < __y); 00164 } 00165 00166 inline ptrdiff_t operator-(const MNRopeConstIterator& __x, 00167 const MNRopeConstIterator& __y) 00168 { 00169 return (ptrdiff_t)__x._M_current_pos - (ptrdiff_t)__y._M_current_pos; 00170 } 00171 00172 inline MNRopeConstIterator 00173 operator-(const MNRopeConstIterator& __x, ptrdiff_t __n) 00174 { 00175 return MNRopeConstIterator( 00176 __x._M_root, __x._M_current_pos - __n); 00177 } 00178 00179 inline MNRopeConstIterator 00180 operator+(const MNRopeConstIterator& __x, ptrdiff_t __n) 00181 { 00182 return MNRopeConstIterator( 00183 __x._M_root, __x._M_current_pos + __n); 00184 } 00185 00186 inline MNRopeConstIterator 00187 operator+(ptrdiff_t __n, const MNRopeConstIterator& __x) 00188 { 00189 return MNRopeConstIterator( 00190 __x._M_root, __x._M_current_pos + __n); 00191 } 00192 00193 inline MNRopeConstIterator find( MNRopeConstIterator __first, 00194 MNRopeConstIterator __last, 00195 uchar_t __val ) 00196 { 00197 while (__first != __last && *__first != __val) 00198 ++__first; 00199 return __first; 00200 } 00201 00202 inline MNRopeConstIterator search( MNRopeConstIterator __first1, 00203 MNRopeConstIterator __last1, 00204 const uchar_t* __first2, 00205 const uchar_t* __last2 ) 00206 { 00207 // Test for empty ranges 00208 if (__first1 == __last1 || __first2 == __last2) 00209 return __first1; 00210 00211 // Test for a pattern of length 1. 00212 const uchar_t* __tmp(__first2); 00213 ++__tmp; 00214 if (__tmp == __last2) 00215 return find(__first1, __last1, *__first2); 00216 00217 // General case. 00218 00219 const uchar_t* __p1; 00220 const uchar_t* __p; 00221 00222 __p1 = __first2; ++__p1; 00223 00224 MNRopeConstIterator __current = __first1; 00225 00226 while (__first1 != __last1) { 00227 __first1 = find(__first1, __last1, *__first2); 00228 if (__first1 == __last1) 00229 return __last1; 00230 00231 __p = __p1; 00232 __current = __first1; 00233 if (++__current == __last1) 00234 return __last1; 00235 00236 while (*__current == *__p) { 00237 if (++__p == __last2) 00238 return __first1; 00239 if (++__current == __last1) 00240 return __last1; 00241 } 00242 00243 ++__first1; 00244 } 00245 return __first1; 00246 } 00247 00248 namespace Rope 00249 { 00250 00251 inline 00252 int lexicographical_compare_3way( const uchar_t* __first1, 00253 const uchar_t* __last1, 00254 const uchar_t* __first2, 00255 const uchar_t* __last2 ) 00256 { 00257 const ptrdiff_t __len1 = __last1 - __first1; 00258 const ptrdiff_t __len2 = __last2 - __first2; 00259 const int __result = memcmp(__first1, __first2, min(__len1, __len2)); 00260 return __result != 0 ? __result 00261 : (__len1 == __len2 ? 0 : (__len1 < __len2 ? -1 : 1)); 00262 } 00263 00264 inline 00265 int lexicographical_compare_3way( const uchar_t* __first1, 00266 const uchar_t* __last1, 00267 MNRopeConstIterator __first2, 00268 MNRopeConstIterator __last2 ) 00269 { 00270 while (__first1 != __last1 && __first2 != __last2) { 00271 if (*__first1 < *__first2) 00272 return -1; 00273 if (*__first2 < *__first1) 00274 return 1; 00275 ++__first1; 00276 ++__first2; 00277 } 00278 if (__first2 == __last2) { 00279 return !(__first1 == __last1); 00280 } 00281 else { 00282 return -1; 00283 } 00284 } 00285 00286 inline 00287 int lexicographical_compare_3way( MNRopeConstIterator __first1, 00288 MNRopeConstIterator __last1, 00289 const uchar_t* __first2, 00290 const uchar_t* __last2 ) 00291 { 00292 while (__first1 != __last1 && __first2 != __last2) { 00293 if (*__first1 < *__first2) 00294 return -1; 00295 if (*__first2 < *__first1) 00296 return 1; 00297 ++__first1; 00298 ++__first2; 00299 } 00300 if (__first2 == __last2) { 00301 return !(__first1 == __last1); 00302 } 00303 else { 00304 return -1; 00305 } 00306 } 00307 00308 inline 00309 int lexicographical_compare_3way( MNRopeConstIterator __first1, 00310 MNRopeConstIterator __last1, 00311 MNRopeConstIterator __first2, 00312 MNRopeConstIterator __last2 ) 00313 { 00314 while (__first1 != __last1 && __first2 != __last2) { 00315 if (*__first1 < *__first2) 00316 return -1; 00317 if (*__first2 < *__first1) 00318 return 1; 00319 ++__first1; 00320 ++__first2; 00321 } 00322 if (__first2 == __last2) { 00323 return !(__first1 == __last1); 00324 } 00325 else { 00326 return -1; 00327 } 00328 } 00329 00330 }; // namespace Rope 00331 00333 00334 #endif /* _R_CONST_ITERATOR_H */ 00335

Generated on Sun Mar 6 13:35:49 2005 for Komssys by doxygen 1.3.8