00001 /* 00002 * File: TimeManager.h 00003 * Author: Dorian Galvez-Lopez 00004 * Date: February 2011 00005 * Description: allows to sort a collection of timestamps and get them 00006 * at a desired frequency 00007 * 00008 * 00009 * This program is free software: you can redistribute it and/or modify 00010 * it under the terms of the GNU Lesser General Public License as published by 00011 * the Free Software Foundation, either version 3 of the License, or 00012 * any later version. 00013 * 00014 * This program is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU Lesser General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU Lesser General Public License 00020 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00021 * 00022 */ 00023 00024 #ifndef __D_TIME_MANAGER__ 00025 #define __D_TIME_MANAGER__ 00026 00027 #include <vector> 00028 #include "Timestamp.h" 00029 00030 namespace DUtils 00031 { 00032 00034 class TimeManager 00035 { 00036 public: 00037 00039 class iterator 00040 { 00041 public: 00042 00046 int index; 00047 00049 Timestamp timestamp; 00050 00051 public: 00052 00058 inline bool good() const { return index >= 0; } 00059 00066 void operator++(); 00067 00074 void operator--(); 00075 00080 void operator+=(int n); 00081 00086 void step(double secs); 00087 00092 inline void setFrequency(float frequency) 00093 { 00094 m_frequency = frequency; 00095 } 00096 00097 protected: 00098 00105 void set(const Timestamp &desired_time, bool moving_backwards = false); 00106 00114 //void set(const Timestamp &desired_time, bool geq_order = true); 00115 00116 protected: 00117 friend class TimeManager; 00118 00120 float m_frequency; // can be -1 to mean all the timestamps 00122 const TimeManager *m_tm; 00123 }; 00124 00125 public: 00126 00127 TimeManager(); 00128 ~TimeManager(); 00129 00134 Timestamp operator[](unsigned int idx); 00135 00141 void add(const Timestamp &t); 00142 00150 void remove(const Timestamp &t, bool decrease_indexes); 00151 00156 void clear(); 00157 00162 inline bool empty() const 00163 { 00164 return m_entries.empty(); 00165 } 00166 00171 inline unsigned int size() const 00172 { 00173 return m_entries.size(); 00174 } 00175 00182 iterator begin(float frequency = -1); 00183 00190 iterator beginAfter(double seconds, float frequency = -1); 00191 00197 iterator beginAt(const Timestamp &t, float frequency = -1); 00198 00203 Timestamp getFirstTimestamp(); 00204 00209 Timestamp getLastTimestamp(); 00210 00211 protected: 00212 00216 void sort(); 00217 00218 protected: 00219 friend class iterator; 00220 00222 struct tEntry 00223 { 00225 Timestamp timestamp; 00227 unsigned int index; // index got when the timestamp was added 00228 00232 tEntry(){} 00233 00238 tEntry(const Timestamp &t): timestamp(t), index(-1){} 00239 00245 tEntry(const Timestamp &t, unsigned int i): timestamp(t), index(i){} 00246 }; 00247 00249 std::vector<tEntry> m_entries; 00251 bool m_is_sorted; 00252 00253 protected: 00254 00261 static bool le(const tEntry &a, const tEntry &b); 00262 00263 00264 }; 00265 00266 } 00267 00268 #endif