Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
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