Skip to content

Commit

Permalink
Merge pull request #1104 from Project-OSRM/experimental/mapbox_variant
Browse files Browse the repository at this point in the history
Experimental/mapbox variant
  • Loading branch information
DennisOSRM committed Aug 14, 2014
2 parents b310e0f + 27a367c commit 9d1e21b
Show file tree
Hide file tree
Showing 7 changed files with 903 additions and 61 deletions.
54 changes: 24 additions & 30 deletions DataStructures/JSONContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef JSON_CONTAINER_H
#define JSON_CONTAINER_H

#include "../ThirdParty/variant/variant.hpp"
#include "../Util/StringUtil.h"

#include <boost/variant.hpp>

#include <iostream>
#include <vector>
#include <string>
Expand All @@ -42,21 +41,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
namespace JSON
{

struct String;
struct Number;
struct Object;
struct Array;
struct True;
struct False;
struct Null;

typedef boost::variant<boost::recursive_wrapper<String>,
boost::recursive_wrapper<Number>,
boost::recursive_wrapper<Object>,
boost::recursive_wrapper<Array>,
boost::recursive_wrapper<True>,
boost::recursive_wrapper<False>,
boost::recursive_wrapper<Null> > Value;

struct String
{
Expand All @@ -73,29 +59,37 @@ struct Number
double value;
};

struct Object
struct True
{
std::unordered_map<std::string, Value> values;
};

struct Array
struct False
{
std::vector<Value> values;
};

struct True
struct Null
{
};

struct False
typedef mapbox::util::variant<String,
Number,
mapbox::util::recursive_wrapper<Object>,
mapbox::util::recursive_wrapper<Array>,
True,
False,
Null > Value;

struct Object
{
std::unordered_map<std::string, Value> values;
};

struct Null
struct Array
{
std::vector<Value> values;
};

struct Renderer : boost::static_visitor<>
struct Renderer : mapbox::util::static_visitor<>
{
Renderer(std::ostream &_out) : out(_out) {}

Expand All @@ -114,7 +108,7 @@ struct Renderer : boost::static_visitor<>
while (iterator != object.values.end())
{
out << "\"" << (*iterator).first << "\":";
boost::apply_visitor(Renderer(out), (*iterator).second);
mapbox::util::apply_visitor(Renderer(out), (*iterator).second);
if (++iterator != object.values.end())
{
out << ",";
Expand All @@ -130,7 +124,7 @@ struct Renderer : boost::static_visitor<>
iterator = array.values.begin();
while (iterator != array.values.end())
{
boost::apply_visitor(Renderer(out), *iterator);
mapbox::util::apply_visitor(Renderer(out), *iterator);
if (++iterator != array.values.end())
{
out << ",";
Expand All @@ -149,7 +143,7 @@ struct Renderer : boost::static_visitor<>
std::ostream &out;
};

struct ArrayRenderer : boost::static_visitor<>
struct ArrayRenderer : mapbox::util::static_visitor<>
{
ArrayRenderer(std::vector<char> &_out) : out(_out) {}

Expand All @@ -176,7 +170,7 @@ struct ArrayRenderer : boost::static_visitor<>
out.push_back('\"');
out.push_back(':');

boost::apply_visitor(ArrayRenderer(out), (*iterator).second);
mapbox::util::apply_visitor(ArrayRenderer(out), (*iterator).second);
if (++iterator != object.values.end())
{
out.push_back(',');
Expand All @@ -192,7 +186,7 @@ struct ArrayRenderer : boost::static_visitor<>
iterator = array.values.begin();
while (iterator != array.values.end())
{
boost::apply_visitor(ArrayRenderer(out), *iterator);
mapbox::util::apply_visitor(ArrayRenderer(out), *iterator);
if (++iterator != array.values.end())
{
out.push_back(',');
Expand Down Expand Up @@ -223,13 +217,13 @@ struct ArrayRenderer : boost::static_visitor<>
inline void render(std::ostream &out, const Object &object)
{
Value value = object;
boost::apply_visitor(Renderer(out), value);
mapbox::util::apply_visitor(Renderer(out), value);
}

inline void render(std::vector<char> &out, const Object &object)
{
Value value = object;
boost::apply_visitor(ArrayRenderer(out), value);
mapbox::util::apply_visitor(ArrayRenderer(out), value);
}

} // namespace JSON
Expand Down
25 changes: 5 additions & 20 deletions DataStructures/StaticRTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "SharedMemoryFactory.h"
#include "SharedMemoryVectorWrapper.h"

#include "../ThirdParty/variant/variant.hpp"
#include "../Util/MercatorUtil.h"
#include "../Util/NumericUtil.h"
#include "../Util/OSRMException.h"
Expand All @@ -48,7 +49,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/thread.hpp>
#include <boost/variant.hpp>

#include <tbb/parallel_for.h>
#include <tbb/parallel_sort.h>
Expand Down Expand Up @@ -307,7 +307,7 @@ class StaticRTree
}
};

typedef boost::variant<TreeNode, EdgeDataT> IncrementalQueryNodeType;
typedef mapbox::util::variant<TreeNode, EdgeDataT> IncrementalQueryNodeType;
struct IncrementalQueryCandidate
{
explicit IncrementalQueryCandidate(const float dist, const IncrementalQueryNodeType &node)
Expand All @@ -323,23 +323,8 @@ class StaticRTree
return other.min_dist < min_dist;
}

inline bool RepresentsTreeNode() const
{
return boost::apply_visitor(decide_type_visitor(), node);
}

float min_dist;
IncrementalQueryNodeType node;

private:
class decide_type_visitor : public boost::static_visitor<bool>
{
public:
bool operator()(const TreeNode &) const { return true; }

template<typename AnotherType>
bool operator()(const AnotherType &) const { return false; }
};
};

typename ShM<TreeNode, UseSharedMemory>::vector m_search_tree;
Expand Down Expand Up @@ -700,9 +685,9 @@ class StaticRTree
continue;
}

if (current_query_node.RepresentsTreeNode())
if (current_query_node.node.template is<TreeNode>())
{
const TreeNode & current_tree_node = boost::get<TreeNode>(current_query_node.node);
const TreeNode & current_tree_node = current_query_node.node.template get<TreeNode>();
if (current_tree_node.child_is_on_disk)
{
++loaded_leafs;
Expand Down Expand Up @@ -775,7 +760,7 @@ class StaticRTree
{
++inspected_segments;
// inspecting an actual road segment
const EdgeDataT & current_segment = boost::get<EdgeDataT>(current_query_node.node);
const EdgeDataT & current_segment = current_query_node.node.template get<EdgeDataT>();

// don't collect too many results from small components
if (number_of_results_found_in_big_cc == number_of_results && !current_segment.is_in_tiny_cc)
Expand Down
2 changes: 1 addition & 1 deletion Server/DataStructures/SharedDataType.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <array>

// Added at the start and end of each block as sanity check
constexpr char CANARY[] = "OSRM";
static const char CANARY[] = "OSRM";

struct SharedDataLayout
{
Expand Down
127 changes: 127 additions & 0 deletions ThirdParty/variant/recursive_wrapper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#ifndef MAPBOX_UTIL_VARIANT_RECURSIVE_WRAPPER_HPP
#define MAPBOX_UTIL_VARIANT_RECURSIVE_WRAPPER_HPP

#include <utility>

namespace mapbox { namespace util {

template <typename T>
class recursive_wrapper
{
public:
using type = T;
private:

T* p_;

public:

~recursive_wrapper();
recursive_wrapper();

recursive_wrapper(recursive_wrapper const& operand);
recursive_wrapper(T const& operand);
recursive_wrapper(recursive_wrapper&& operand);
recursive_wrapper(T&& operand);

private:

void assign(const T& rhs);

public:

inline recursive_wrapper& operator=(recursive_wrapper const& rhs)
{
assign( rhs.get() );
return *this;
}

inline recursive_wrapper& operator=(T const& rhs)
{
assign( rhs );
return *this;
}

inline void swap(recursive_wrapper& operand) noexcept
{
T* temp = operand.p_;
operand.p_ = p_;
p_ = temp;
}


recursive_wrapper& operator=(recursive_wrapper&& rhs) noexcept
{
swap(rhs);
return *this;
}

recursive_wrapper& operator=(T&& rhs)
{
get() = std::move(rhs);
return *this;
}


public:

T& get() { return *get_pointer(); }
const T& get() const { return *get_pointer(); }
T* get_pointer() { return p_; }
const T* get_pointer() const { return p_; }
operator T const&() const { return this->get(); }
operator T&() { return this->get(); }
};

template <typename T>
recursive_wrapper<T>::~recursive_wrapper()
{
delete p_;
}

template <typename T>
recursive_wrapper<T>::recursive_wrapper()
: p_(new T)
{
}

template <typename T>
recursive_wrapper<T>::recursive_wrapper(recursive_wrapper const& operand)
: p_(new T( operand.get() ))
{
}

template <typename T>
recursive_wrapper<T>::recursive_wrapper(T const& operand)
: p_(new T(operand))
{
}

template <typename T>
recursive_wrapper<T>::recursive_wrapper(recursive_wrapper&& operand)
: p_(operand.p_)
{
operand.p_ = nullptr;
}

template <typename T>
recursive_wrapper<T>::recursive_wrapper(T&& operand)
: p_(new T( std::move(operand) ))
{
}

template <typename T>
void recursive_wrapper<T>::assign(const T& rhs)
{
this->get() = rhs;
}

template <typename T>
inline void swap(recursive_wrapper<T>& lhs, recursive_wrapper<T>& rhs) noexcept
{
lhs.swap(rhs);
}

}}

#endif // MAPBOX_UTIL_VARIANT_RECURSIVE_WRAPPER_HPP
Loading

0 comments on commit 9d1e21b

Please sign in to comment.