ZBuffer.h
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 
00003 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00004 // This file is part of FZIs ic_workspace.
00005 //
00006 // This program is free software licensed under the LGPL
00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
00008 // You can find a copy of this license in LICENSE folder in the top
00009 // directory of the source code.
00010 //
00011 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
00012 //
00013 // -- END LICENSE BLOCK ------------------------------------------------
00014 
00015 //----------------------------------------------------------------------
00022 //----------------------------------------------------------------------
00023 #ifndef ICL_CORE_Z_BUFFER_H_INCLUDED
00024 #define ICL_CORE_Z_BUFFER_H_INCLUDED
00025 
00026 #include <vector>
00027 #include <cassert>
00028 #include <boost/optional.hpp>
00029 
00030 namespace icl_core {
00031 
00042 template <typename T, typename TCompare = std::less<T> >
00043 class ZBuffer
00044 {
00045 public:
00047   typedef std::vector<boost::optional<T> > Buffer;
00049   typedef typename Buffer::size_type size_type;
00050 
00056   explicit ZBuffer(const std::size_t size, const TCompare& cmp = TCompare())
00057     : m_buffer(size),
00058       m_cmp(cmp)
00059   { }
00060 
00068   bool update(const std::size_t pos, const T& value)
00069   {
00070     assert(pos < m_buffer.size());
00071     if (!m_buffer[pos] || m_cmp(value, *m_buffer[pos]))
00072     {
00073       m_buffer[pos] = value;
00074       return true;
00075     }
00076     return false;
00077   }
00078 
00080   void reset() { m_buffer.assign(size); }
00082   const size_type size() const { return m_buffer.size(); }
00084   const Buffer& buffer() const { return m_buffer; }
00086   boost::optional<T> at(const std::size_t pos) const { return m_buffer[pos]; }
00088   boost::optional<T> operator [] (const std::size_t pos) const { return m_buffer[pos]; }
00089 
00090 private:
00092   Buffer m_buffer;
00094   TCompare m_cmp;
00095 };
00096 
00097 }
00098 
00099 #endif


fzi_icl_core
Author(s):
autogenerated on Tue Aug 8 2017 02:28:04