MADARA  3.4.1
NamedVectorCombinator.h
Go to the documentation of this file.
1 #ifndef _MADARA_UTILITY_NAMED_VECTOR_COMBINATOR_H_
2 #define _MADARA_UTILITY_NAMED_VECTOR_COMBINATOR_H_
3 
4 #include <map>
5 #include <set>
6 #include <vector>
7 #include <string>
8 #include <sstream>
9 #include <iterator>
10 
11 #include "Utility.h"
13 
14 namespace madara
15 {
16 namespace utility
17 {
25 {
26 public:
30  NamedVectorCombinator() = default;
31 
38  inline void from_file(const std::string& name, const std::string& filename)
39  {
41  }
42 
48  inline void add(const std::string& name,
49  const std::string& input)
50  {
51  named_vectors_[name] = string_to_vector(input);
52  }
53 
59  inline void add(const std::string& name,
60  const std::vector<std::string>& strings)
61  {
62  named_vectors_[name] = strings;
63  }
64 
80  inline void merge(const std::vector<std::string> & named_vectors,
81  std::vector<std::string> & result, bool throw_on_missing = true,
82  bool make_unique = true, bool clear_first = true)
83  {
84  std::set<std::string> unique_list;
85 
86  if(clear_first)
87  {
88  result.clear();
89  }
90 
91  for(auto name : named_vectors)
92  {
93  auto found = named_vectors_.find(name);
94  if(found != named_vectors_.end())
95  {
96  // if uniqueness is not important, then copy the list over
97  if(!make_unique)
98  {
99  result.insert(
100  result.end(), found->second.begin(), found->second.end());
101  }
102  // if uniqueness is important, use the set instead
103  else
104  {
105  unique_list.insert(found->second.begin(), found->second.end());
106  }
107  }
108  else if(throw_on_missing)
109  {
110  std::stringstream buffer;
111  buffer << "madara::utility::NamedVectorCombinator: ";
112  buffer << name << " could not be found in mapped named vectors.";
113 
114  throw exceptions::NameException(buffer.str());
115  }
116  }
117 
118  if(make_unique)
119  {
120  result.insert(result.end(), unique_list.begin(), unique_list.end());
121 
122  // in C++17, this works and reduces copying costs. May switch then.
123  // std::make_move_iterator(unique_list.begin()),
124  // std::make_move_iterator(unique_list.end()));
125  }
126  }
127 
128 private:
129 
131  std::map <std::string, std::vector<std::string>> named_vectors_;
132 };
133 }
134 }
135 
136 #endif // _MADARA_UTILITY_NAMED_VECTOR_COMBINATOR_H_
An exception for setting an invalid name in MADARA.
Definition: NameException.h:16
A helper class for combining named vectors of strings into vectors of unique strings.
void add(const std::string &name, const std::string &input)
Adds a vector of strings to the mapped vectors with a specific name.
std::map< std::string, std::vector< std::string > > named_vectors_
the mapping of named lists to their contents
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)
Merges named vectors into a result vector.
void from_file(const std::string &name, const std::string &filename)
Reads a file containing strings separated by newlines into a named vector.
void add(const std::string &name, const std::vector< std::string > &strings)
Adds a vector of strings to the mapped vectors with a specific name.
NamedVectorCombinator()=default
Constructor.
constexpr string_t string
Provides utility functions and classes for common tasks and needs.
Definition: IteratorImpl.h:15
std::string file_to_string(const std::string &filename)
Reads a file into a string.
Definition: Utility.cpp:324
std::vector< std::string > string_to_vector(const std::string &input, const std::string delimiter="\n", bool strip_whitespace=true)
Splits a string into a vector of strings by delimiter.
Definition: Utility.inl:111
Copyright(c) 2020 Galois.