ZBuffer.h
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of FZIs ic_workspace.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 //
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
22 //----------------------------------------------------------------------
23 #ifndef ICL_CORE_Z_BUFFER_H_INCLUDED
24 #define ICL_CORE_Z_BUFFER_H_INCLUDED
25 
26 #include <vector>
27 #include <cassert>
28 #include <boost/optional.hpp>
29 
30 namespace icl_core {
31 
42 template <typename T, typename TCompare = std::less<T> >
43 class ZBuffer
44 {
45 public:
47  typedef std::vector<boost::optional<T> > Buffer;
49  typedef typename Buffer::size_type size_type;
50 
56  explicit ZBuffer(const std::size_t size, const TCompare& cmp = TCompare())
57  : m_buffer(size),
58  m_cmp(cmp)
59  { }
60 
68  bool update(const std::size_t pos, const T& value)
69  {
70  assert(pos < m_buffer.size());
71  if (!m_buffer[pos] || m_cmp(value, *m_buffer[pos]))
72  {
73  m_buffer[pos] = value;
74  return true;
75  }
76  return false;
77  }
78 
80  void reset() { m_buffer.assign(size); }
82  const size_type size() const { return m_buffer.size(); }
84  const Buffer& buffer() const { return m_buffer; }
86  boost::optional<T> at(const std::size_t pos) const { return m_buffer[pos]; }
88  boost::optional<T> operator [] (const std::size_t pos) const { return m_buffer[pos]; }
89 
90 private:
92  Buffer m_buffer;
94  TCompare m_cmp;
95 };
96 
97 }
98 
99 #endif
Buffer::size_type size_type
Shorthand to vector size type.
Definition: ZBuffer.h:49
const size_type size() const
Returns the buffer size.
Definition: ZBuffer.h:82
std::vector< boost::optional< T > > Buffer
The type of the internal buffer.
Definition: ZBuffer.h:47
Buffer m_buffer
The actual buffer (a vector with optionally empty elements).
Definition: ZBuffer.h:92
boost::optional< T > at(const std::size_t pos) const
Read-only access to a specific buffer element.
Definition: ZBuffer.h:86
const Buffer & buffer() const
Read-only access to the buffer.
Definition: ZBuffer.h:84
void reset()
Empties the Z buffer.
Definition: ZBuffer.h:80
bool update(const std::size_t pos, const T &value)
Definition: ZBuffer.h:68
TCompare m_cmp
The comparison operator.
Definition: ZBuffer.h:94
ZBuffer(const std::size_t size, const TCompare &cmp=TCompare())
Definition: ZBuffer.h:56
boost::optional< T > operator[](const std::size_t pos) const
Read-only access to a specific buffer element.
Definition: ZBuffer.h:88


fzi_icl_core
Author(s):
autogenerated on Mon Jun 10 2019 13:17:58