mapnik/include/mapnik/vertex_vector.hpp

186 lines
5.7 KiB
C++
Raw Normal View History

2006-03-31 12:32:02 +02:00
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
2005-06-14 17:06:59 +02:00
*
2006-03-31 12:32:02 +02:00
* Copyright (C) 2006 Artem Pavlenko
2005-06-14 17:06:59 +02:00
*
2006-03-31 12:32:02 +02:00
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
2005-06-14 17:06:59 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2006-03-31 12:32:02 +02:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2005-06-14 17:06:59 +02:00
*
2006-03-31 12:32:02 +02:00
*****************************************************************************/
2005-06-14 17:06:59 +02:00
// Credits:
// I gratefully acknowledge the inspiring work of Maxim Shemanarev (McSeem),
// author of Anti-Grain Geometry (http://www.antigrain.com). I have used
// the datastructure from AGG as a template for my own.
//$Id: vertex_vector.hpp 39 2005-04-10 20:39:53Z pavlenko $
#ifndef VERTEX_VECTOR_HPP
2007-04-25 10:21:54 +02:00
#define VERTEX_VECTOR_HPP
// mapnik
#include <mapnik/vertex.hpp>
#include <mapnik/ctrans.hpp>
2007-10-08 20:10:31 +02:00
// boost
#include <boost/utility.hpp>
#include <boost/tuple/tuple.hpp>
// stl
#include <vector>
2006-01-23 11:25:38 +01:00
2005-06-14 17:06:59 +02:00
namespace mapnik
{
template <typename T>
2006-01-23 11:25:38 +01:00
class vertex_vector : private boost::noncopyable
2005-06-14 17:06:59 +02:00
{
typedef typename T::type value_type;
typedef vertex<value_type,2> vertex_type;
enum block_e {
block_shift = 8,
block_size = 1<<block_shift,
block_mask = block_size - 1,
grow_by = 256
};
2005-06-14 17:06:59 +02:00
private:
unsigned num_blocks_;
unsigned max_blocks_;
value_type** vertexs_;
unsigned char** commands_;
unsigned pos_;
2005-06-14 17:06:59 +02:00
public:
vertex_vector()
: num_blocks_(0),
max_blocks_(0),
vertexs_(0),
commands_(0),
pos_(0) {}
2005-06-14 17:06:59 +02:00
~vertex_vector()
{
if ( num_blocks_ )
{
value_type** vertexs=vertexs_ + num_blocks_ - 1;
while ( num_blocks_-- )
{
delete [] *vertexs;
--vertexs;
}
delete [] vertexs_;
}
}
unsigned size() const
{
return pos_;
}
2005-06-14 17:06:59 +02:00
void push_back (value_type x,value_type y,unsigned command)
{
unsigned block = pos_ >> block_shift;
if (block >= num_blocks_)
{
allocate_block(block);
}
value_type* vertex = vertexs_[block] + ((pos_ & block_mask) << 1);
unsigned char* cmd= commands_[block] + (pos_ & block_mask);
2005-06-14 17:06:59 +02:00
*cmd = static_cast<unsigned char>(command);
*vertex++ = x;
*vertex = y;
++pos_;
}
unsigned get_vertex(unsigned pos,value_type* x,value_type* y) const
{
if (pos >= pos_) return SEG_END;
unsigned block = pos >> block_shift;
const value_type* vertex = vertexs_[block] + (( pos & block_mask) << 1);
*x = (*vertex++);
*y = (*vertex);
return commands_[block] [pos & block_mask];
}
2006-01-23 11:25:38 +01:00
void set_capacity(size_t)
{
//do nothing
}
2005-06-14 17:06:59 +02:00
private:
void allocate_block(unsigned block)
{
if (block >= max_blocks_)
{
value_type** new_vertexs = new value_type* [(max_blocks_ + grow_by) * 2];
unsigned char** new_commands = (unsigned char**)(new_vertexs + max_blocks_ + grow_by);
if (vertexs_)
{
std::memcpy(new_vertexs,vertexs_,max_blocks_ * sizeof(value_type*));
std::memcpy(new_commands,commands_,max_blocks_ * sizeof(unsigned char*));
delete [] vertexs_;
}
vertexs_ = new_vertexs;
commands_ = new_commands;
max_blocks_ += grow_by;
}
vertexs_[block] = new value_type [block_size * 2 + block_size / (sizeof(value_type))];
commands_[block] = (unsigned char*)(vertexs_[block] + block_size*2);
++num_blocks_;
}
2005-06-14 17:06:59 +02:00
};
2006-01-23 11:25:38 +01:00
template <typename T>
struct vertex_vector2 : boost::noncopyable
{
typedef typename T::type value_type;
typedef boost::tuple<value_type,value_type,char> vertex_type;
typedef typename std::vector<vertex_type>::const_iterator const_iterator;
vertex_vector2() {}
unsigned size() const
{
return cont_.size();
}
2006-01-23 11:25:38 +01:00
void push_back (value_type x,value_type y,unsigned command)
{
cont_.push_back(vertex_type(x,y,command));
}
unsigned get_vertex(unsigned pos,value_type* x,value_type* y) const
{
if (pos >= cont_.size()) return SEG_END;
vertex_type const& c = cont_[pos];
*x = boost::get<0>(c);
*y = boost::get<1>(c);
return boost::get<2>(c);
}
const_iterator begin() const
{
return cont_.begin();
}
const_iterator end() const
{
return cont_.end();
}
void set_capacity(size_t size)
{
cont_.reserve(size);
}
2006-01-23 11:25:38 +01:00
private:
std::vector<vertex_type> cont_;
2006-01-23 11:25:38 +01:00
};
2005-06-14 17:06:59 +02:00
}
#endif //VERTEX_VECTOR_HPP