-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* * 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
1 parent
fc64378
commit b499ac2
Showing
9 changed files
with
372 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
agent.0.stability | ||
agent.0.correctness |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.