mcpele  1.0.0
The Monte Carlo Python Energy Landscape Explorer
mcpele/pattern_manager.h
00001 #ifndef _MCPELE_PATTERN_MANAGER_H__
00002 #define _MCPELE_PATTERN_MANAGER_H__
00003 
00004 #include <vector>
00005 #include <utility>
00006 #include <stdexcept>
00007 
00008 #include "mc.h"
00009 
00010 namespace mcpele {
00011 
00012 template <class T>
00013 class PatternManager {
00014 public:
00015     typedef typename std::vector<std::pair<size_t, T> > vec_t;
00016 private:
00017     vec_t m_step_repetitions;
00018     size_t m_current_step_index;
00019     size_t m_current_step_count;
00020     bool m_initialized;
00021 public:
00022     virtual ~PatternManager() {}
00023     PatternManager()
00024         : m_initialized(false)
00025     {}
00026     const PatternManager& operator ++ ()
00027     {
00028         --m_current_step_count;
00029         if (m_current_step_count == 0) {
00030             ++m_current_step_index;
00031             if (m_current_step_index == m_step_repetitions.size()) {
00032                 m_current_step_index = 0;
00033             }
00034             m_current_step_count = m_step_repetitions.at(m_current_step_index).first;
00035             assert(m_current_step_count);
00036         }
00037         return *this;
00038     }
00039     void add(const T index_input, const size_t repetitions_input=1)
00040     {
00041         if (repetitions_input < 1) {
00042             throw std::range_error("PatternManager::add: illegal input");
00043         }
00044         m_step_repetitions.push_back(std::make_pair(repetitions_input, index_input));
00045         m_current_step_index = 0;
00046         m_current_step_count = m_step_repetitions.at(0).first;
00047         if (!m_initialized) {
00048             m_initialized = true;
00049         }
00050     }
00051     T get_step_ptr() const
00052     {
00053         if (!m_initialized) {
00054             throw std::runtime_error("PatternManager::get_step_index: illegal access");
00055         }
00056         return m_step_repetitions.at(m_current_step_index).second;
00057     }
00063     std::vector<size_t> get_pattern() const
00064     {
00065         std::vector<size_t> result;
00066         for (typename vec_t::const_iterator i = m_step_repetitions.begin(); i != m_step_repetitions.end(); ++i) {
00067             const std::vector<size_t> tmp(i->first, static_cast<size_t>(i - m_step_repetitions.begin()));
00068             result.insert(result.end(), tmp.begin(), tmp.end());
00069         }
00070         result.swap(result);
00071         assert(result.front() == 0);
00072         assert(result.back() == m_step_repetitions.size() - 1);
00073         return result;
00074     }
00075     std::vector<size_t> get_pattern_direct()
00076     {
00077         m_current_step_index = 0;
00078         m_current_step_count = m_step_repetitions.at(0).first;
00079         assert(m_current_step_index == 0);
00080         size_t pattern_length = 0;
00081         for (typename vec_t::const_iterator i = m_step_repetitions.begin(); i != m_step_repetitions.end(); ++i) {
00082             pattern_length += i->first;
00083         }
00084         assert(m_current_step_index == 0);
00085         std::vector<size_t> result;
00086         for (size_t i = 0; i < pattern_length; ++i, ++*this) {
00087             result.push_back(m_current_step_index);
00088         }
00089         assert(result.size() == pattern_length);
00090         assert(result.front() == 0);
00091         assert(result.back() == m_step_repetitions.size() - 1);
00092         return result;
00093     }
00094 };
00095 
00096 } // namespace mcpele
00097 
00098 #endif // #ifndef _MCPELE_PATTERN_MANAGER_H__
 All Classes Namespaces Functions Variables Typedefs