Savarese Software Research Corporation
CircularFind.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2006-2009 Savarese Software Research Corporation
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     https://www.savarese.com/software/ApacheLicense-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00022 #ifndef __SSRC_WSPR_UTILITY_CIRCULAR_FIND_H
00023 #define __SSRC_WSPR_UTILITY_CIRCULAR_FIND_H
00024 
00025 #include <algorithm>
00026 
00027 #include <ssrc/wispers-packages.h>
00028 
00029 __BEGIN_NS_SSRC_WSPR_UTILITY
00030 
00038 template<typename Iterator>
00039 class CircularFind {
00040 
00041   Iterator _begin, _end;
00042   mutable Iterator _pos;
00043 
00044 public:
00045   typedef Iterator iterator;
00046   typedef typename std::iterator_traits<iterator>::value_type value_type;
00047 
00048   CircularFind(const iterator & begin, const iterator & end) :
00049     _begin(begin), _end(end), _pos(begin)
00050   { }
00051 
00052   const iterator & begin() const {
00053     return _begin;
00054   }
00055 
00056   const iterator & end() const {
00057     return _end;
00058   }
00059 
00060   const iterator & find(const value_type & value) const {
00061     _pos = std::find(_pos, _end, value);
00062 
00063     if(_pos == _end) {
00064       _pos = std::find(_begin, _end, value);
00065 
00066       if(_pos == _end) {
00067         _pos = _begin;
00068         return _end;
00069       }
00070     }
00071 
00072     return _pos;
00073   }
00074 
00075   template<typename Predicate>
00076   const iterator & find_if(const Predicate & predicate) const {
00077     _pos = std::find_if(_pos, _end, predicate);
00078 
00079     if(_pos == _end) {
00080       _pos = std::find_if(_begin, _end, predicate);
00081 
00082       if(_pos == _end) {
00083         _pos = _begin;
00084         return _end;
00085       }
00086     }
00087 
00088     return _pos;
00089   }
00090 };
00091 
00092 __END_NS_SSRC_WSPR_UTILITY
00093 
00094 #endif

Savarese Software Research Corporation
Copyright © 2006-2011 Savarese Software Research Corporation. All rights reserved.