Skip to content

Commit

Permalink
Cleanup buffer_vector and use range std::move.
Browse files Browse the repository at this point in the history
Signed-off-by: vng <[email protected]>
  • Loading branch information
vng committed Apr 23, 2021
1 parent 5613477 commit 9c26c14
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 78 deletions.
92 changes: 24 additions & 68 deletions base/buffer_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,10 @@ template <class T, size_t N> class buffer_vector

inline bool IsDynamic() const { return m_size == USE_DYNAMIC; }

/// @todo clang on linux doesn't have is_trivially_copyable.
#ifndef OMIM_OS_LINUX
template <class U = T>
std::enable_if_t<std::is_trivially_copyable<U>::value, void> MoveStatic(buffer_vector<T, N> & rhs)
void MoveStatic(buffer_vector<T, N> & rhs)
{
memcpy(m_static, rhs.m_static, rhs.m_size*sizeof(T));
std::move(rhs.m_static, rhs.m_static + rhs.m_size, m_static);
}
template <class U = T>
std::enable_if_t<!std::is_trivially_copyable<U>::value, void> MoveStatic(
buffer_vector<T, N> & rhs)
{
for (size_t i = 0; i < rhs.m_size; ++i)
Swap(m_static[i], rhs.m_static[i]);
}
#else
template <class U = T>
std::enable_if_t<std::is_pod<U>::value, void> MoveStatic(buffer_vector<T, N> & rhs)
{
memcpy(m_static, rhs.m_static, rhs.m_size*sizeof(T));
}
template <class U = T>
std::enable_if_t<!std::is_pod<U>::value, void> MoveStatic(buffer_vector<T, N> & rhs)
{
for (size_t i = 0; i < rhs.m_size; ++i)
Swap(m_static[i], rhs.m_static[i]);
}
#endif

public:
typedef T value_type;
Expand Down Expand Up @@ -115,24 +92,23 @@ template <class T, size_t N> class buffer_vector
template <typename TIt>
void append(TIt beg, TIt end)
{
if (IsDynamic())
{
m_dynamic.insert(m_dynamic.end(), beg, end);
return;
}

while (beg != end)
if (!IsDynamic())
{
if (m_size == N)
size_t const newSize = std::distance(beg, end) + m_size;
if (newSize <= N)
{
m_dynamic.reserve(N * 2);
SwitchToDynamic();
while (beg != end)
m_dynamic.push_back(*beg++);
break;
m_static[m_size++] = *beg++;
return;
}
else
{
m_dynamic.reserve(newSize);
SwitchToDynamic();
}
m_static[m_size++] = *beg++;
}

m_dynamic.insert(m_dynamic.end(), beg, end);
}

void append(size_t count, T const & c)
Expand All @@ -147,7 +123,10 @@ template <class T, size_t N> class buffer_vector
return;
}
else
{
m_dynamic.reserve(newSize);
SwitchToDynamic();
}
}

m_dynamic.insert(m_dynamic.end(), count, c);
Expand Down Expand Up @@ -313,36 +292,15 @@ template <class T, size_t N> class buffer_vector
return;
}

if (m_size < N)
{
m_static[m_size++] = t;
}
else
{
ASSERT_EQUAL(m_size, N, ());
SwitchToDynamic();
m_dynamic.push_back(t);
ASSERT_EQUAL(m_dynamic.size(), N + 1, ());
}
}

void push_back(T && t)
{
if (IsDynamic())
{
m_dynamic.push_back(std::move(t));
return;
}

if (m_size < N)
{
Swap(m_static[m_size++], t);
}
else
{
ASSERT_EQUAL(m_size, N, ());
m_dynamic.reserve(N + 1);
SwitchToDynamic();
m_dynamic.push_back(std::move(t));
m_dynamic.push_back(t);
ASSERT_EQUAL(m_dynamic.size(), N + 1, ());
}
}
Expand Down Expand Up @@ -375,7 +333,7 @@ template <class T, size_t N> class buffer_vector
}
else
{
ASSERT_EQUAL(m_size, N, ());
m_dynamic.reserve(N + 1);
SwitchToDynamic();
m_dynamic.emplace_back(std::forward<Args>(args)...);
ASSERT_EQUAL(m_dynamic.size(), N + 1, ());
Expand Down Expand Up @@ -450,12 +408,10 @@ template <class T, size_t N> class buffer_vector
{
ASSERT_NOT_EQUAL(m_size, static_cast<size_t>(USE_DYNAMIC), ());
ASSERT_EQUAL(m_dynamic.size(), 0, ());
m_dynamic.reserve(m_size);
for (size_t i = 0; i < m_size; ++i)
{
m_dynamic.emplace_back();
Swap(m_static[i], m_dynamic.back());
}

m_dynamic.resize(m_size);
std::move(m_static, m_static + m_size, m_dynamic.begin());

m_size = USE_DYNAMIC;
}
};
Expand Down
4 changes: 2 additions & 2 deletions openlr/graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ void GetRegularEdges(geometry::PointWithAltitude const & junction, IRoadGraph co
{
auto & es = cache[junction];
(graph.*edgeGetter)(junction, es);
edges.insert(end(edges), begin(es), end(es));
edges.append(begin(es), end(es));
}
else
{
auto const & es = it->second;
edges.insert(end(edges), begin(es), end(es));
edges.append(begin(es), end(es));
}
}
} // namespace
Expand Down
4 changes: 2 additions & 2 deletions openlr/router.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,12 +478,12 @@ void Router::GetEdges(
{
auto & es = cache[u];
(m_graph.*getRegular)(u, es);
edges.insert(edges.end(), es.begin(), es.end());
edges.append(es.begin(), es.end());
}
else
{
auto const & es = it->second;
edges.insert(edges.end(), es.begin(), es.end());
edges.append(es.begin(), es.end());
}
(m_graph.*getFake)(u, edges);
}
Expand Down
2 changes: 1 addition & 1 deletion routing/index_graph_starter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ void IndexGraphStarter::AddFakeEdges(Segment const & segment, bool isOutgoing, E
}
}
}
edges.insert(edges.end(), fakeEdges.begin(), fakeEdges.end());
edges.append(fakeEdges.begin(), fakeEdges.end());
}

bool IndexGraphStarter::EndingPassThroughAllowed(Ending const & ending)
Expand Down
4 changes: 2 additions & 2 deletions routing/index_graph_starter_joints.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,13 +456,13 @@ bool IndexGraphStarterJoints<Graph>::FillEdgesAndParentsWeights(
// from start to start or end to end vertex.
if (vertex == GetStartJoint())
{
edges.insert(edges.end(), m_startOutEdges.begin(), m_startOutEdges.end());
edges.append(m_startOutEdges.begin(), m_startOutEdges.end());
parentWeights.append(edges.size(), Weight(0.0));
firstFakeId = edges.size();
}
else if (vertex == GetFinishJoint())
{
edges.insert(edges.end(), m_endOutEdges.begin(), m_endOutEdges.end());
edges.append(m_endOutEdges.begin(), m_endOutEdges.end());
// If vertex is FinishJoint, parentWeight is equal to zero, because the first vertex is zero-weight loop.
parentWeights.append(edges.size(), Weight(0.0));
}
Expand Down
4 changes: 2 additions & 2 deletions routing/road_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,15 @@ void IRoadGraph::GetFakeOutgoingEdges(geometry::PointWithAltitude const & juncti
{
auto const it = m_fakeOutgoingEdges.find(junction);
if (it != m_fakeOutgoingEdges.cend())
edges.insert(edges.end(), it->second.cbegin(), it->second.cend());
edges.append(it->second.cbegin(), it->second.cend());
}

void IRoadGraph::GetFakeIngoingEdges(geometry::PointWithAltitude const & junction,
EdgeListT & edges) const
{
auto const it = m_fakeIngoingEdges.find(junction);
if (it != m_fakeIngoingEdges.cend())
edges.insert(edges.end(), it->second.cbegin(), it->second.cend());
edges.append(it->second.cbegin(), it->second.cend());
}

void IRoadGraph::ResetFakes()
Expand Down
2 changes: 1 addition & 1 deletion routing/transit_world_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void TransitWorldGraph::GetEdgeList(astar::VertexData<Segment, RouteWeight> cons
fakeFromReal.emplace_back(s, edge.GetWeight());
}
}
edges.insert(edges.end(), fakeFromReal.begin(), fakeFromReal.end());
edges.append(fakeFromReal.begin(), fakeFromReal.end());
}

void TransitWorldGraph::GetEdgeList(
Expand Down

0 comments on commit 9c26c14

Please sign in to comment.