Qucs-core  0.0.19
history.h
Go to the documentation of this file.
00001 /*
00002  * history.h - history class definitions
00003  *
00004  * Copyright (C) 2006, 2007 Stefan Jahn <stefan@lkcc.org>
00005  *
00006  * This is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2, or (at your option)
00009  * any later version.
00010  *
00011  * This software is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this package; see the file COPYING.  If not, write to
00018  * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020  *
00021  * $Id$
00022  *
00023  */
00024 
00025 #ifndef __HISTORY_H__
00026 #define __HISTORY_H__
00027 
00028 #include <memory>
00029 #include <vector>
00030 #include <utility>
00031 
00032 namespace qucs {
00033 
00034 class history
00035 {
00036 public:
00038   history ():
00039     sign(false),
00040     age(0),
00041     values(std::make_shared<std::vector<nr_double_t>>()),
00042     t(std::make_shared<std::vector<nr_double_t>>()) 
00043   {};
00044   
00047   history (const history &h)
00048   {
00049       this->age = h.age;
00050       this->t = std::make_shared<std::vector<nr_double_t>>(*(h.t));
00051       this->values = std::make_shared<std::vector<nr_double_t>>(*(h.values));
00052   }
00053 
00055   void push_back (const nr_double_t val) {
00056     this->values->push_back(val);
00057     if (this->values != this->t) 
00058       this->drop ();
00059   }
00060 
00061   /* This function drops the most recent n values in the history. */
00062   void truncate (const std::size_t n) = delete;
00063   void resize(const std::size_t n)
00064   {
00065     this->t->resize (n);
00066     this->values->resize (n);
00067   }
00068 
00069   std::size_t size (void) const
00070   {
00071     return t->size ();
00072   }
00073 
00074   void setAge (const nr_double_t a) { this->age = a; }
00075   nr_double_t getAge (void) const { return this->age; }
00076 
00077   // apply history
00078   void apply (const history & h) {
00079     this->t = h.t;
00080   }
00081 
00083   nr_double_t last (void) const {
00084     return this->t->empty() ? 0.0 : this->t->back();
00085   }
00086 
00088   nr_double_t first (void) const {
00089     return this->t->empty() ? 0.0 : (*this->t)[leftidx ()];
00090   }
00091 
00092   // Returns left-most valid index into the time value vector.
00093   unsigned int leftidx (void) const {
00094     int ts = this->t->size ();
00095     int vs = this->values->size ();
00096     return ts - vs > 0 ? ts - vs : 0;
00097   }
00098 
00101   std::size_t unused (void) {
00102     int ts = t->size ();
00103     int vs = values->size ();
00104     return vs - ts > 0 ? vs - ts : 0;
00105   }
00106   
00108   nr_double_t duration(void) const {
00109      return last () - first ();
00110   }
00111   
00112   void truncate (const nr_double_t);
00113   
00114   void drop (void);
00115   void self (void) { this->t = this->values; }
00116 
00117   nr_double_t interpol (nr_double_t, int, bool);
00118   nr_double_t nearest (nr_double_t, bool interpolate = true);
00119   int seek (nr_double_t, int, int, nr_double_t&, int);
00120 
00121   nr_double_t getTfromidx (const int idx)  {
00122     return this->t == NULL ? 0.0 : (*this->t)[idx];
00123   }
00124   nr_double_t getValfromidx (const int idx) {
00125     return this->values == NULL ? 0.0 : (*this->values)[idx];
00126   }
00127 
00128  private:
00129   bool sign;
00130   nr_double_t age;
00131   std::shared_ptr<std::vector<nr_double_t>> values;
00132   std::shared_ptr<std::vector<nr_double_t>> t;
00133 };
00134 
00135 } // namespace qucs
00136 
00137 #endif /* __HISTORY_H__ */