Skip to content
Snippets Groups Projects
FastVector.h 2.37 KiB
Newer Older
  • Learn to ignore specific revisions
  • // ************************************************************************** //
    
    //  BornAgain: simulate and fit scattering at grazing incidence
    //
    
    //! @file      Tools/inc/FastVector.h
    
    //! @brief     Defines class KVectorContainer.
    //!
    
    //! @homepage  http://apps.jcns.fz-juelich.de/BornAgain
    
    //! @license   GNU General Public License v3 or higher (see COPYING)
    
    //! @copyright Forschungszentrum Jülich GmbH 2013
    //! @authors   Scientific Computing Group at MLZ Garching
    
    //! @authors   C. Durniak, G. Pospelov, W. Van Herck, J. Wuttke
    
    //
    // ************************************************************************** //
    
    
    pospelov's avatar
    pospelov committed
    #ifndef FASTVECTOR_H
    #define FASTVECTOR_H
    
    #include "Types.h"
    #include <iostream>
    
    
    
    //! @class KVectorContainer
    //! @ingroup tools_internal
    //! @brief A Vector of kvector_t's with optimized location/deallocation.
    
    pospelov's avatar
    pospelov committed
    class KVectorContainer {
    
    pospelov's avatar
    pospelov committed
        typedef std::vector<kvector_t > container_t;
        typedef container_t::const_iterator const_iterator;
    
        KVectorContainer(int buff_size = 3)
            : m_current_position(0), m_max_buff_size(buff_size), m_buffer()
    
    pospelov's avatar
    pospelov committed
        {
            m_buffer.reserve(m_max_buff_size);
    
            for(size_t i=0; i<m_max_buff_size; ++i)
                m_buffer.push_back(kvector_t(0.0, 0.0, 0.0));
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        inline void push_back(const kvector_t& k) {
    
    pospelov's avatar
    pospelov committed
            if(m_current_position == m_max_buff_size) {
    
    //            std::cout << "KVectorContainer::push_back() -> "
    //                         "Info. Increasing size of the buffer from "
    //                      << m_max_buff_size;
    
    pospelov's avatar
    pospelov committed
                m_max_buff_size *=2;
    
    //            std::cout << " to " << m_max_buff_size << std::endl;
    
    pospelov's avatar
    pospelov committed
                m_buffer.resize(m_max_buff_size);
            }
            m_buffer[m_current_position][0] = k[0];
            m_buffer[m_current_position][1] = k[1];
            m_buffer[m_current_position][2] = k[2];
            m_current_position++;
        }
    
        inline void clear() { m_current_position = 0; }
    
        inline size_t size() { return m_current_position; }
    
        void print() {
    
            for(size_t i=0; i<m_max_buff_size; ++i)
                std::cout << i << " " << m_buffer[i] << std::endl;
    
    pospelov's avatar
    pospelov committed
            std::cout << "size:" << size() << std::endl;
        }
    
        const_iterator begin() const { return m_buffer.begin(); }
        const_iterator end() const { return m_buffer.begin()+m_current_position; }
    
    
    pospelov's avatar
    pospelov committed
        size_t m_current_position;
        size_t m_max_buff_size;
        container_t m_buffer;
    };
    
    #endif // FASTVECTOR_H