Skip to content

Commit

Permalink
Combinator (#118)
Browse files Browse the repository at this point in the history
* * Added new NamedVectorCombinator class to help people build prefixes for CheckpointSettings
* Added tests for NamedVectorCombinator to test_utility
  * Added 3 types of tests for importing from strings, vectors, and files containing newline-delimited lists of strings
* Currently have a compile error in clang about using std::make_move_iterator. Working through that with David

* * Attempting to set distribution to xenial in Travis CI

* Use lambdas instead of pointer-to-members for c_str transform due to
clang linking issue

* * Removed std::move within the merge of NamedVectorCombinator to fix compilation in clang. This will not be supported unless c++17 is enabled

* * Changed the Travis CI names to reflect Ubuntu 16.04 rather than 14.04

* * Removing apt dependency in clang Travis CI Linux install

* Try removing custom compiler installation
  • Loading branch information
jredmondson committed Oct 10, 2018
1 parent fc64378 commit b499ac2
Show file tree
Hide file tree
Showing 9 changed files with 372 additions and 65 deletions.
30 changes: 13 additions & 17 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
sudo: required
dist: xenial
group: travis_latest
language: cpp
os:
Expand Down Expand Up @@ -27,8 +28,8 @@ env:
matrix:
allow_failures:
#- env: NAME="OSX 10.13 clang zmq" CLANG="clang" ZMQ="zmq"
- env: NAME="Ubuntu 14.04 android" ANDROID="android" SSL="ssl" TESTS=""
- env: NAME="Ubuntu 14.04 android zmq" ANDROID="android" ZMQ="zmq" TESTS=""
- env: NAME="Ubuntu 16.04 android" ANDROID="android" SSL="ssl" TESTS=""
- env: NAME="Ubuntu 16.04 android zmq" ANDROID="android" ZMQ="zmq" TESTS=""
include:
################MAC#####################

Expand Down Expand Up @@ -66,21 +67,20 @@ matrix:

# clang, linux, no ssl
- compiler: clang
env: NAME="Ubuntu 14.04 clang" CLANG="clang"
env: NAME="Ubuntu 16.04 clang" CLANG="clang"
addons:
apt:
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-trusty-5.0
packages:
- libssl-dev
- clang-5.0
# - clang-5.0
os: linux

# g++, linux, no ssl
- compiler: gcc
os: linux
env: NAME="Ubuntu 14.04 g++" CLANG=""
env: NAME="Ubuntu 16.04 g++" CLANG=""

# g++, linux, ssl
- compiler: gcc
Expand All @@ -91,37 +91,37 @@ matrix:
- ubuntu-toolchain-r-test
packages:
- libssl-dev
env: NAME="Ubuntu 14.04 g++ ssl" CLANG="" SSL="ssl"
env: NAME="Ubuntu 16.04 g++ ssl" CLANG="" SSL="ssl"

# g++, linux, java
- compiler: gcc
os: linux
env: NAME="Ubuntu 14.04 g++ java" JAVA="java"
env: NAME="Ubuntu 16.04 g++ java" JAVA="java"

# g++, linux, zmq
- compiler: gcc
os: linux
env: NAME="Ubuntu 14.04 g++ zmq" ZMQ="zmq"
env: NAME="Ubuntu 16.04 g++ zmq" ZMQ="zmq"

# g++, linux, zmq
- compiler: gcc
os: linux
env: NAME="Ubuntu 14.04 g++ python" PYTHON="python"
env: NAME="Ubuntu 16.04 g++ python" PYTHON="python"

# g++, linux, simtime
- compiler: gcc
os: linux
env: NAME="Ubuntu 14.04 g++ simtime" SIMTIME="simtime"
env: NAME="Ubuntu 16.04 g++ simtime" SIMTIME="simtime"

# g++, linux, android, zmq
- compiler: gcc
os: linux
env: NAME="Ubuntu 14.04 android zmq" ANDROID="android" ZMQ="zmq" TESTS=""
env: NAME="Ubuntu 16.04 android zmq" ANDROID="android" ZMQ="zmq" TESTS=""

# g++, linux, android, ssl
- compiler: gcc
os: linux
env: NAME="Ubuntu 14.04 android" ANDROID="android" SSL="ssl" TESTS=""
env: NAME="Ubuntu 16.04 android" ANDROID="android" SSL="ssl" TESTS=""

addons:
apt:
Expand All @@ -136,10 +136,6 @@ before_install:
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew install openssl; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew install zeromq; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew link openssl --force; fi
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test; fi
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get -y update; fi
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get -y install gcc-5 g++-5; fi
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 60 --slave /usr/bin/g++ g++ /usr/bin/g++-5; fi

before_script:
- export
Expand Down
136 changes: 136 additions & 0 deletions include/madara/utility/NamedVectorCombinator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#ifndef _MADARA_UTILITY_NAMED_VECTOR_COMBINATOR_H_
#define _MADARA_UTILITY_NAMED_VECTOR_COMBINATOR_H_

#include <map>
#include <set>
#include <vector>
#include <string>
#include <sstream>
#include <iterator>

#include "Utility.h"
#include "madara/exceptions/NameException.h"

namespace madara
{
namespace utility
{
/**
* @class NamedVectorCombinator
* @brief A helper class for combining named vectors of strings into vectors
* of unique strings.
*
*/
class NamedVectorCombinator
{
public:
/**
* Constructor
**/
NamedVectorCombinator() = default;

/**
* Reads a file containing strings separated by newlines into a named vector
* @param name the name of the mapped vector to import into
* @param filename the name of the file to read in the newline-delimited
* strings
**/
inline void from_file(const std::string& name, const std::string& filename)
{
named_vectors_[name] = string_to_vector(file_to_string(filename));
}

/**
* Adds a vector of strings to the mapped vectors with a specific name
* @param name the name of the mapped vector to import into
* @param input a string containing newline-delimited strings to import
**/
inline void add(const std::string& name,
const std::string& input)
{
named_vectors_[name] = string_to_vector(input);
}

/**
* Adds a vector of strings to the mapped vectors with a specific name
* @param name the name of the mapped vector to import into
* @param strings a vector of strings to associate with the name
**/
inline void add(const std::string& name,
const std::vector<std::string>& strings)
{
named_vectors_[name] = strings;
}

/**
* Merges named vectors into a result vector. This is a useful function
* for building prefix lists for CheckpointSettings and other classes
* that have vectors of strings.
* @param named_vectors a vector of named entities that you want to merge.
* Each string should exist in the known map by
* first calling from_file to create the lists
* @param result the result of the merge
* @param throw_on_missing if true, throw an exception if the named entity
* does not exist
* @param make_unique if true, make sure the resulting merge is unique
* @param clear_first if true, clear the result before inserting
* @throw exceptions::NameException if throw_on_missing and named vector
* contains an unknown mapping, throw this exception
**/
inline void merge(const std::vector<std::string> & named_vectors,
std::vector<std::string> & result, bool throw_on_missing = true,
bool make_unique = true, bool clear_first = true)
{
std::set<std::string> unique_list;

if(clear_first)
{
result.clear();
}

for(auto name : named_vectors)
{
auto found = named_vectors_.find(name);
if(found != named_vectors_.end())
{
// if uniqueness is not important, then copy the list over
if(!make_unique)
{
result.insert(
result.end(), found->second.begin(), found->second.end());
}
// if uniqueness is important, use the set instead
else
{
unique_list.insert(found->second.begin(), found->second.end());
}
}
else if(throw_on_missing)
{
std::stringstream buffer;
buffer << "madara::utility::NamedVectorCombinator: ";
buffer << name << " could not be found in mapped named vectors.";

throw exceptions::NameException(buffer.str());
}
}

if(make_unique)
{
result.insert(result.end(), unique_list.begin(), unique_list.end());

// in C++17, this works and reduces copying costs. May switch then.
// std::make_move_iterator(unique_list.begin()),
// std::make_move_iterator(unique_list.end()));
}
}

private:

/// the mapping of named lists to their contents
std::map <std::string, std::vector<std::string>> named_vectors_;
};
}
}

#endif // _MADARA_UTILITY_NAMED_VECTOR_COMBINATOR_H_
5 changes: 5 additions & 0 deletions tests/settings/named_vectors/thread1.read
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
agent.0.location
agent.0.orientation
agent.0.battery
agent.0.imu
agent.0.history
2 changes: 2 additions & 0 deletions tests/settings/named_vectors/thread1.write
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
agent.0.stability
agent.0.correctness
4 changes: 4 additions & 0 deletions tests/settings/named_vectors/thread2.read
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
agent.0.stability
agent.0.correctness
agent.0.location
agent.0.orientation
3 changes: 3 additions & 0 deletions tests/settings/named_vectors/thread2.write
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
agent.0.mission.status
agent.0.mission.next
agent.1.algorithm

0 comments on commit b499ac2

Please sign in to comment.