Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Algorithms #2161

Merged
merged 9 commits into from
Jul 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Applications/ApplicationsLib/ProjectData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

#include <logog/include/logog.hpp>

#include "BaseLib/Algorithm.h"
#include "BaseLib/FileTools.h"
#include "BaseLib/uniqueInsert.h"

#include "GeoLib/GEOObjects.h"
#include "MathLib/Curve/CreatePiecewiseLinearCurve.h"
Expand Down
4 changes: 1 addition & 3 deletions Applications/DataHolderLib/Project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@
#include "Project.h"

#include <algorithm>

#include <logog/include/logog.hpp>

#include "BaseLib/Algorithm.h"
#include "BaseLib/FileTools.h"
#include "BaseLib/uniqueInsert.h"

#include "MeshLib/Mesh.h"

namespace DataHolderLib
Expand Down
2 changes: 1 addition & 1 deletion Applications/Utils/MeshEdit/NodeReordering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#include "Applications/ApplicationsLib/LogogSetup.h"

#include "BaseLib/makeVectorUnique.h"
#include "BaseLib/Algorithm.h"

#include "MeshLib/IO/readMeshFromFile.h"
#include "MeshLib/IO/writeMeshToFile.h"
Expand Down
113 changes: 102 additions & 11 deletions BaseLib/uniqueInsert.h → BaseLib/Algorithm.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/**
* \file
* \author Thomas Fischer
* \date 2011-02-23
* \brief Definition of the uniquePushBack function.
*
* \copyright
* Copyright (c) 2012-2018, OpenGeoSys Community (http://www.opengeosys.org)
Expand All @@ -15,19 +12,67 @@
#pragma once

#include <algorithm>
#include <typeinfo>
#include <cassert>
#include <typeindex>
#include <typeinfo>
#include "Error.h"

namespace BaseLib
{
template <typename Container>
void uniquePushBack(Container& container,
typename Container::value_type const& element)

/// excludeObjectCopy copies only those objects that position within the source
/// vector is not in the exclude_positions vector. The implementation of the
/// algorithm requires that the given positions in exclude_positions are sorted
/// in ascending order.
/// @param src_vec the vector of source objects
/// @param exclude_positions the positions of objects in the source vector that
/// do not have to be copied
/// @return vector that contains the copied objects
template <typename T>
std::vector<T> excludeObjectCopy(std::vector<T> const& src_vec,
std::vector<std::size_t> const& exclude_positions)
{
if (std::find(container.begin(), container.end(), element) ==
container.end())
container.push_back(element);
std::vector<T> dest_vec;
if (exclude_positions.empty()) {
dest_vec = src_vec;
return dest_vec;
}

assert (exclude_positions.back() < src_vec.size());

std::copy_n(src_vec.cbegin(), exclude_positions[0], std::back_inserter(dest_vec));
for (std::size_t i=1; i<exclude_positions.size(); ++i) {
std::copy_n(
src_vec.cbegin()+exclude_positions[i-1]+1,
exclude_positions[i]-(exclude_positions[i-1]+1),
std::back_inserter(dest_vec)
);
}
std::copy(src_vec.cbegin()+exclude_positions.back()+1,
src_vec.cend(), std::back_inserter(dest_vec));

return dest_vec;
}

template <typename T>
void excludeObjectCopy(std::vector<T> const& src_vec,
std::vector<std::size_t> const& exclude_positions,
std::vector<T> & dest_vec)
{
dest_vec = excludeObjectCopy(src_vec, exclude_positions);
}

template <typename InputIt, typename Predicate>
typename std::iterator_traits<InputIt>::reference findElementOrError(
InputIt begin, InputIt end, Predicate predicate,
std::string const& error = "")
{
auto it = std::find_if(begin, end, predicate);
if (it == end)
{
OGS_FATAL("Element not found in the input range; %s", error.c_str());
}
return *it;
}

//! Inserts the given \c key with the given \c value into the \c map if an entry
Expand Down Expand Up @@ -125,4 +170,50 @@ typename Map::mapped_type const& getOrError(Map const& map, Key const& key,
return it->second;
}

} // end namespace BaseLib
/// Make the entries of the std::vector \c v unique. The remaining entries will
/// be sorted.
template <typename T>
void makeVectorUnique(std::vector<T>& v)
{
std::sort(v.begin(), v.end());
auto it = std::unique(v.begin(), v.end());
v.erase(it, v.end());
}

/// Make the entries of the std::vector \c v unique using the given binary
/// function. The remaining entries will be sorted.
template <typename T, class Compare>
void makeVectorUnique(std::vector<T>& v, Compare comp)
{
std::sort(v.begin(), v.end(), comp);
auto it = std::unique(v.begin(), v.end());
v.erase(it, v.end());
}

/**
* Reorder a vector by a given index vector.
*
* Note: It is good enough in performance for medium size vectors.
*/
template <typename ValueType, typename IndexType>
void reorderVector(std::vector<ValueType>& v,
std::vector<IndexType> const& order)
{
std::vector<ValueType> temp_v(v.size());
temp_v.swap(v);

for (std::size_t i=0; i<order.size(); i++)
{
std::swap(v[i], temp_v[order[i]]);
}
}

template <typename Container>
void uniquePushBack(Container& container,
typename Container::value_type const& element)
{
if (std::find(container.begin(), container.end(), element) ==
container.end())
container.push_back(element);
}
} // namespace BaseLib
9 changes: 4 additions & 5 deletions BaseLib/Functional.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#include <functional>
#include "baselib_export.h"
#include "BaseLib/TMPUtil.h"

namespace BaseLib
{
Expand Down Expand Up @@ -101,7 +100,7 @@ template <int... Indices, typename Object, typename MethodClass,
typename ReturnType, typename... Args>
std::function<ReturnType(Args...)> easyBind_inner(
ReturnType (MethodClass::*method)(Args...), Object&& obj,
IntegerSequence<Indices...>)
std::integer_sequence<int, Indices...>)
{
return easyBind_innermost<Indices...>(method, std::forward<Object>(obj));
}
Expand All @@ -110,7 +109,7 @@ template <int... Indices, typename Object, typename MethodClass,
typename ReturnType, typename... Args>
std::function<ReturnType(Args...)> easyBind_inner(
ReturnType (MethodClass::*method)(Args...) const, Object&& obj,
IntegerSequence<Indices...>)
std::integer_sequence<int, Indices...>)
{
return easyBind_innermost<Indices...>(method, std::forward<Object>(obj));
}
Expand Down Expand Up @@ -182,7 +181,7 @@ easyBind(ReturnType (MethodClass::*method)(Args...), Object&& obj)
{
return detail::easyBind_inner(
method, std::forward<Object>(obj),
typename GenerateIntegerSequence<sizeof...(Args)>::type{});
std::make_integer_sequence<int, sizeof...(Args)>{});
}

//! \overload
Expand All @@ -197,7 +196,7 @@ easyBind(ReturnType (MethodClass::*method)(Args...) const, Object&& obj)
{
return detail::easyBind_inner(
method, std::forward<Object>(obj),
typename GenerateIntegerSequence<sizeof...(Args)>::type{});
std::make_integer_sequence<int, sizeof...(Args)>{});
}

//! Wraps a callable object in a std::function.
Expand Down
3 changes: 0 additions & 3 deletions BaseLib/README.md

This file was deleted.

File renamed without changes.
43 changes: 0 additions & 43 deletions BaseLib/TMPUtil.h

This file was deleted.

62 changes: 0 additions & 62 deletions BaseLib/excludeObjectCopy.h

This file was deleted.

39 changes: 0 additions & 39 deletions BaseLib/makeVectorUnique.h

This file was deleted.

Loading