Skip to content

Commit

Permalink
use martinus/unordered_dense (#15)
Browse files Browse the repository at this point in the history
* add https://raw.githubusercontent.com/gharveymn/small_vector/main/source/include/gch/small_vector.hpp

* add https://raw.githubusercontent.com/martinus/unordered_dense/main/include/ankerl/unordered_dense.h

* just like in maplibre

* add fast float from https://github.com/fastfloat/fast_float/tree/main?tab=readme-ov-file

* compiles

* not good

* should be good

* not needed?

* trivially copiable

* TODO, use string

* add parallel map

* Revert "add parallel map"

This reverts commit 75409b6.

* use string as index

* add poolSTL

* single header

* fix string road id

* revert

* good

* fix test

* clean code

* update packedrtree, add tests

* clean code

* trigger CI

* trigger CI

* fix test

* add test

* fix

* fix lint

---------

Co-authored-by: root <[email protected]>
  • Loading branch information
district10 and root authored Dec 9, 2023
1 parent c24d18d commit 37d2979
Show file tree
Hide file tree
Showing 16 changed files with 3,896 additions and 95 deletions.
1,936 changes: 1,936 additions & 0 deletions 3rdparty/ankerl/unordered_dense.h

Large diffs are not rendered by default.

62 changes: 60 additions & 2 deletions 3rdparty/packedrtree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ struct NodeItem
return false;
return true;
}
std::vector<double> toVector() const
{
return std::vector<double>{minX, minY, maxX, maxY};
}
};

inline bool operator==(const NodeItem &lhs, const NodeItem &rhs)
Expand All @@ -104,6 +108,11 @@ inline bool operator==(const NodeItem &lhs, const NodeItem &rhs)
lhs.offset == rhs.offset;
}

inline bool operator!=(const NodeItem &lhs, const NodeItem &rhs)
{
return !(lhs == rhs);
}

struct Item
{
NodeItem nodeItem;
Expand All @@ -120,6 +129,11 @@ inline bool operator==(const SearchResultItem &lhs, const SearchResultItem &rhs)
return lhs.index == rhs.index && lhs.offset == rhs.offset;
}

inline bool operator!=(const SearchResultItem &lhs, const SearchResultItem &rhs)
{
return !(lhs == rhs);
}

// Based on public domain code at
// https://github.com/rawrunprotected/hilbert_curves
inline uint32_t hilbert(uint32_t x, uint32_t y)
Expand Down Expand Up @@ -206,6 +220,14 @@ inline NodeItem calcExtent(const std::vector<NodeItem> &nodes)
[](NodeItem a, const NodeItem &b) { return a.expand(b); });
}

inline NodeItem calcExtent(const std::vector<std::shared_ptr<Item>> &items)
{
return std::accumulate(items.begin(), items.end(), NodeItem::create(0),
[](NodeItem a, const std::shared_ptr<Item> &b) {
return a.expand(b->nodeItem);
});
}

template <class ITEM_TYPE> void hilbertSort(std::deque<ITEM_TYPE> &items)
{
NodeItem extent = calcExtent(items);
Expand Down Expand Up @@ -243,6 +265,28 @@ inline void hilbertSort(std::vector<NodeItem> &items)
hilbertSort(items, calcExtent(items));
}

inline void hilbertSort(std::vector<std::shared_ptr<Item>> &items, const NodeItem &extent)
{
const double minX = extent.minX;
const double minY = extent.minY;
const double width = extent.width();
const double height = extent.height();
std::sort(items.begin(), items.end(),
[minX, minY, width, height](std::shared_ptr<Item> a,
std::shared_ptr<Item> b) {
uint32_t ha = hilbert(a->nodeItem, HILBERT_MAX, minX, minY,
width, height);
uint32_t hb = hilbert(b->nodeItem, HILBERT_MAX, minX, minY,
width, height);
return ha > hb;
});
}

inline void hilbertSort(std::vector<std::shared_ptr<Item>> &items) {
return hilbertSort(items, calcExtent(items));
}


/**
* Packed R-Tree
* Based on https://github.com/mourner/flatbush
Expand Down Expand Up @@ -294,6 +338,7 @@ class PackedRTree
}

public:
PackedRTree() = default;
PackedRTree(const std::vector<NodeItem> &nodes, const NodeItem &extent,
const uint16_t nodeSize = 16)
: _extent(extent), _numItems(nodes.size())
Expand All @@ -304,6 +349,16 @@ class PackedRTree
generateNodes();
}

PackedRTree(const std::vector<std::shared_ptr<Item>> &items,
const NodeItem &extent, const uint16_t nodeSize = 16)
: _extent(extent), _numItems(items.size())
{
init(nodeSize);
for (size_t i = 0; i < _numItems; i++)
_nodeItems[_numNodes - _numItems + i] = items[i]->nodeItem;
generateNodes();
}

PackedRTree(const void *data, const uint64_t numItems,
const uint16_t nodeSize)
: _extent(NodeItem::create(0)), _numItems(numItems)
Expand Down Expand Up @@ -401,13 +456,16 @@ class PackedRTree
return numNodes * sizeof(NodeItem);
}

void streamWrite(const std::function<void(uint8_t *, size_t)> &writeData)
void streamWrite(const std::function<void(const uint8_t *, size_t)> &writeData) const
{
writeData(reinterpret_cast<uint8_t *>(&_nodeItems[0]),
writeData(reinterpret_cast<const uint8_t *>(_nodeItems.data()),
static_cast<size_t>(_numNodes * sizeof(NodeItem)));
}

NodeItem getExtent() const { return _extent; }
size_t getNumItems() const { return _numItems; }
size_t getNumNodes() const { return _numNodes; }
size_t getNodeSize() const { return _nodeSize; }
};

} // namespace FlatGeobuf
Expand Down
Loading

0 comments on commit 37d2979

Please sign in to comment.