compartment_sets.h
1 #pragma once
2 
3 #include <bbp/sonata/nodes.h>
4 
5 #include <nlohmann/json.hpp>
6 
7 namespace bbp {
8 namespace sonata {
9 namespace detail {
10 class CompartmentSetFilteredIterator;
11 class CompartmentSet;
12 class CompartmentSets;
13 } // namespace detail
14 
31  public:
32  uint64_t nodeId = 0;
33  uint64_t sectionIndex = 0;
34  double offset = 0.0;
35 
38  bool operator==(const CompartmentLocation& other) const {
39  return nodeId == other.nodeId && sectionIndex == other.sectionIndex &&
40  offset == other.offset;
41  }
42 
43  bool operator!=(const CompartmentLocation& other) const {
44  return !(*this == other);
45  }
46 };
47 
49 inline std::ostream& operator<<(std::ostream& os, const CompartmentLocation& cl) {
50  os << "CompartmentLocation("
51  << "nodeId: " << cl.nodeId << ", "
52  << "sectionIndex: " << cl.sectionIndex << ", "
53  << "offset: " << cl.offset << ")";
54  return os;
55 }
56 
58 public:
59  using iterator_category = std::forward_iterator_tag;
61  using difference_type = std::ptrdiff_t;
62  using pointer = const CompartmentLocation*;
63  using reference = const CompartmentLocation&;
64 
66  std::unique_ptr<detail::CompartmentSetFilteredIterator> impl);
72 
73  const CompartmentLocation& operator*() const;
74  const CompartmentLocation* operator->() const;
75 
76  CompartmentSetFilteredIterator& operator++(); // prefix ++
77  CompartmentSetFilteredIterator operator++(int); // postfix ++
78  bool operator==(const CompartmentSetFilteredIterator& other) const;
79  bool operator!=(const CompartmentSetFilteredIterator& other) const;
80 
81 private:
82  std::unique_ptr<detail::CompartmentSetFilteredIterator> impl_;
83 };
84 
85 
93 class SONATA_API CompartmentSet
94 {
95  public:
96  CompartmentSet() = delete;
97 
98  explicit CompartmentSet(const std::string& json_content);
99  explicit CompartmentSet(std::shared_ptr<detail::CompartmentSet>&& impl);
100 
101  std::pair<CompartmentSetFilteredIterator, CompartmentSetFilteredIterator> filtered_crange(
102  Selection selection = Selection({})) const;
103 
105  std::size_t size(const Selection& selection = Selection({})) const;
106 
107  // Is empty?
108  bool empty() const;
109 
111  const std::string& population() const;
112 
114  CompartmentLocation operator[](std::size_t index) const;
115 
116  Selection nodeIds() const;
117 
118  CompartmentSet filter(const Selection& selection = Selection({})) const;
119 
121  std::string toJSON() const;
122 
123  bool operator==(const CompartmentSet& other) const;
124  bool operator!=(const CompartmentSet& other) const;
125 
126  private:
127  std::shared_ptr<detail::CompartmentSet> impl_;
128 };
129 
130 
141 class SONATA_API CompartmentSets
142 {
143 public:
144 
145  CompartmentSets(const std::string& content);
146  CompartmentSets(std::unique_ptr<detail::CompartmentSets>&& impl);
147  CompartmentSets(detail::CompartmentSets&& impl);
148  CompartmentSets(CompartmentSets&&) noexcept;
149  CompartmentSets(const CompartmentSets& other) = delete;
150  CompartmentSets& operator=(CompartmentSets&&) noexcept;
151  ~CompartmentSets();
152 
155  static CompartmentSets fromFile(const std::string& path);
156 
158  CompartmentSet getCompartmentSet(const std::string& key) const;
159 
161  std::size_t size() const;
162 
164  bool empty() const;
165 
167  bool contains(const std::string& key) const;
168 
170  std::vector<std::string> names() const;
171 
173  std::vector<CompartmentSet> getAllCompartmentSets() const;
174 
176  std::vector<std::pair<std::string, CompartmentSet>> items() const;
177 
179  std::string toJSON() const;
180 
181  bool operator==(const CompartmentSets& other) const;
182  bool operator!=(const CompartmentSets& other) const;
183 
184 private:
185  std::unique_ptr<detail::CompartmentSets> impl_;
186 };
187 
188 } // namespace sonata
189 } // namespace bbp
Definition: compartment_sets.h:57
Definition: compartment_sets.h:94
std::size_t size(const Selection &selection=Selection({})) const
Size of the set, optionally filtered by selection.
CompartmentLocation operator[](std::size_t index) const
Access element by index. It returns a copy!
std::string toJSON() const
Serialize to JSON string.
const std::string & population() const
Population name.
A container class that manages a collection of named CompartmentSet objects.
Definition: compartment_sets.h:142
std::vector< std::pair< std::string, CompartmentSet > > items() const
Get items (key + compartment set) as vector of pairs.
std::vector< CompartmentSet > getAllCompartmentSets() const
Get all compartment sets as vector.
CompartmentSet getCompartmentSet(const std::string &key) const
Access element by key (throws if not found)
std::size_t size() const
Number of compartment sets.
std::string toJSON() const
Serialize all compartment sets to JSON string.
bool empty() const
Is empty?
static CompartmentSets fromFile(const std::string &path)
std::vector< std::string > names() const
Get names of CompartmentSet(s) as a vector.
bool contains(const std::string &key) const
Check if key exists.
Definition: selection.h:13
Definition: compartment_sets.h:30
bool operator==(const CompartmentLocation &other) const
Definition: compartment_sets.h:38