Skip to content

Commit

Permalink
#128 Extract node_id_vector definition in graph_interface.inl
Browse files Browse the repository at this point in the history
  • Loading branch information
YarikTH committed Aug 24, 2023
1 parent 614b6d6 commit c61f5c5
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 41 deletions.
55 changes: 14 additions & 41 deletions include/ureact/detail/graph_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
#ifndef UREACT_DETAIL_GRAPH_INTERFACE_HPP
#define UREACT_DETAIL_GRAPH_INTERFACE_HPP

#include <cassert>
#include <cstddef>
#include <vector>

#include <ureact/detail/algorithm.hpp>
#include <ureact/detail/defines.hpp>

UREACT_BEGIN_NAMESPACE
Expand Down Expand Up @@ -63,49 +61,20 @@ class node_id
class node_id_vector
{
public:
void add( node_id id )
{
m_data.push_back( id );
}

void remove( node_id id )
{
const auto it = detail::find( m_data.begin(), m_data.end(), id );
assert( it != m_data.end() );

// Unstable erase algorithm
// If we remove not the last element, then copy last element on erased position and remove last element
const auto last_it = m_data.begin() + m_data.size() - 1;
if( it != last_it )
{
*it = *last_it;
}

m_data.resize( m_data.size() - 1 );
}

UREACT_WARN_UNUSED_RESULT auto begin()
{
return m_data.begin();
}
using value_type = node_id;
using container_type = std::vector<value_type>;
using iterator = container_type::iterator;

UREACT_WARN_UNUSED_RESULT auto end()
{
return m_data.end();
}

void clear()
{
m_data.clear();
}
void add( node_id id );
void remove( node_id id );
void clear();
UREACT_WARN_UNUSED_RESULT bool empty() const;

UREACT_WARN_UNUSED_RESULT bool empty() const
{
return m_data.empty();
}
UREACT_WARN_UNUSED_RESULT iterator begin();
UREACT_WARN_UNUSED_RESULT iterator end();

private:
std::vector<node_id> m_data;
container_type m_data;
};

enum class update_result
Expand Down Expand Up @@ -137,4 +106,8 @@ struct observer_interface

UREACT_END_NAMESPACE

#if UREACT_HEADER_ONLY
# include <ureact/detail/graph_interface.inl>
#endif

#endif //UREACT_DETAIL_GRAPH_INTERFACE_HPP
68 changes: 68 additions & 0 deletions include/ureact/detail/graph_interface.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// Copyright (C) 2014-2017 Sebastian Jeckel.
// Copyright (C) 2020-2023 Krylov Yaroslav.
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//

#ifndef UREACT_DETAIL_GRAPH_INTERFACE_INL
#define UREACT_DETAIL_GRAPH_INTERFACE_INL

#include <cassert>

#include <ureact/detail/algorithm.hpp>
#include <ureact/detail/defines.hpp>

UREACT_BEGIN_NAMESPACE

namespace detail
{

UREACT_FUNC void node_id_vector::add( const node_id id )
{
m_data.push_back( id );
}

UREACT_FUNC void node_id_vector::remove( const node_id id )
{
const auto it = detail::find( m_data.begin(), m_data.end(), id );
assert( it != m_data.end() );

// Unstable erase algorithm
// If we remove not the last element, then copy last element on erased position and remove last element
const auto last_it = m_data.begin() + m_data.size() - 1;
if( it != last_it )
{
*it = *last_it;
}

m_data.resize( m_data.size() - 1 );
}

UREACT_FUNC void node_id_vector::clear()
{
m_data.clear();
}

UREACT_FUNC bool node_id_vector::empty() const
{
return m_data.empty();
}

UREACT_FUNC node_id_vector::iterator node_id_vector::begin()
{
return m_data.begin();
}

UREACT_FUNC node_id_vector::iterator node_id_vector::end()
{
return m_data.end();
}

} // namespace detail

UREACT_END_NAMESPACE

#endif //UREACT_DETAIL_GRAPH_INTERFACE_INL

0 comments on commit c61f5c5

Please sign in to comment.