XNetwork 1.7.5; VERSION ${PROJECT_VERSION}
Loading...
Searching...
No Matches
coreviews.hpp
Go to the documentation of this file.
1
9#pragma once
10// from collections import Mapping
11// #include <xnetwork.hpp> // as xn
12#include <iterator>
13
14/*
15static const auto __all__ = {
16 "AtlasView",
17 "AdjacencyView",
18 "MultiAdjacencyView",
19 "UnionAtlas",
20 "UnionAdjacency",
21 "UnionMultiInner",
22 "UnionMultiAdjacency",
23 "FilterAtlas",
24 "FilterAdjacency",
25 "FilterMultiInner",
26 "FilterMultiAdjacency",
27 "ReadOnlyGraph",
28};
29*/
30
34template <typename Atlas> class AtlasView {
35 public:
36 using size_type = typename std::remove_reference_t<Atlas>::size_type;
37 Atlas& _atlas;
38
43 explicit AtlasView(Atlas& d) : _atlas{d} {}
44
49 auto size() const -> size_t { return this->_atlas.size(); }
50
55 auto begin() const { return std::begin(this->_atlas); }
56
61 auto end() const { return std::end(this->_atlas); }
62
69 template <typename T> auto operator[](const T& key) const -> const auto& {
70 return this->_atlas.at(key);
71 }
72
79 template <typename T> auto at(const T& key) const -> const auto& {
80 return this->_atlas.at(key);
81 }
82
89 template <typename T> auto operator[](const T& key) -> auto& {
90 return this->_atlas[static_cast<size_type>(key)];
91 }
92
93 // auto copy( ) {
94 // return std::tuple{n: self[n].copy() for n : this->_atlas};
95 // }
96
97 // auto __str__( ) {
98 // return str(this->_atlas); // {nbr: self[nbr] for nbr : self});
99 // }
100
101 // auto __repr__( ) {
102 // return "%s(%r)" % (this->__class__.__name__, this->_atlas);
103 // }
104};
105
109template <typename Atlas> class AdjacencyView : public AtlasView<Atlas> {
110 public:
115 explicit AdjacencyView(Atlas& d) : AtlasView<Atlas>{d} {}
116
117 // template <typename T>
118 // auto operator[](const T& name) const
119 // {
120 // return AtlasView(this->_atlas[name]);
121 // }
122
123 // auto copy( ) {
124 // return std::tuple{n: self[n].copy() for n : this->_atlas};
125 // }
126};
127
128// class MultiAdjacencyView(AdjacencyView) {
129// /** An MultiAdjacencyView is a Read-only Map of Maps of Maps of Maps.
130
131// It is a View into a dict-of-dict-of-dict-of-dict data structure.
132// The inner level of dict is read-write. But the
133// outer levels are read-only.
134
135// See Also
136// ========
137// AtlasView - View into dict-of-dict
138// AdjacencyView - View into dict-of-dict-of-dict
139// */
140// // static const auto __slots__ = (); // Still uses AtlasView slots
141// names _atlas static const char *__slots__ = nullptr; // Still uses
142// AtlasView slots names _atlas
143
144// auto operator[]( name) {
145// return AdjacencyView(this->_atlas[name]);
146// }
147
148// auto copy( ) {
149// return std::tuple{n: self[n].copy() for n : this->_atlas};
150// }
151// };
152
153// class UnionAtlas : public Mapping {
154// /** A read-only union of two atlases (dict-of-dict).
155
156// The two dict-of-dicts represent the inner dict of
157// an Adjacency: `gra.succ[node]` and `gra.pred[node]`.
158// The inner level of dict of both hold attribute key:value
159// pairs and is read-write. But the outer level is read-only.
160
161// See Also
162// ========
163// UnionAdjacency - View into dict-of-dict-of-dict
164// UnionMultiAdjacency - View into dict-of-dict-of-dict-of-dict
165// */
166// using _Self = UnionAtlas;
167
168// static const auto __slots__ = {"_succ", "_pred"};
169
170// auto __getstate__( ) {
171// return std::tuple{"_succ": this->_succ, "_pred": this->_pred};
172// }
173
174// auto __setstate__( state) {
175// this->_succ = state["_succ"];
176// this->_pred = state["_pred"];
177// }
178
179// explicit _Self( succ, pred) {
180// this->_succ = succ;
181// this->_pred = pred;
182// }
183
184// auto size( ) {
185// return this->_succ.size() + this->_pred.size();
186// }
187
188// auto __iter__( ) {
189// return iter(set(this->_succ.keys()) | set(this->_pred.keys()));
190// }
191
192// auto operator[]( key) {
193// try {
194// return this->_succ[key];
195// } catch (KeyError) {
196// return this->_pred[key];
197// }
198// }
199
200// auto copy( ) {
201// result = {nbr: dd.copy() for nbr, dd : this->_succ.items()}
202// for (auto nbr, dd : this->_pred.items()) {
203// if (nbr : result) {
204// result[nbr].update(dd);
205// } else {
206// result[nbr] = dd.copy();
207// }
208// }
209// return result;
210// }
211
212// auto __str__( ) {
213// return str({nbr: self[nbr] for nbr : self});
214// }
215
216// auto __repr__( ) {
217// return "%s(%r, %r)" % (this->__class__.__name__, this->_succ,
218// this->_pred);
219// }
220// };
221
222// class UnionAdjacency : public Mapping {
223// /** A read-only union of dict Adjacencies as a Map of Maps of Maps.
224
225// The two input dict-of-dict-of-dicts represent the union of
226// `gra.succ` and `gra.pred`. Return values are UnionAtlas
227// The inner level of dict is read-write. But the
228// middle and outer levels are read-only.
229
230// succ : a dict-of-dict-of-dict {node: nbrdict}
231// pred : a dict-of-dict-of-dict {node: nbrdict}
232// The keys for the two dicts should be the same
233
234// See Also
235// ========
236// UnionAtlas - View into dict-of-dict
237// UnionMultiAdjacency - View into dict-of-dict-of-dict-of-dict
238// */
239// using _Self = UnionAdjacency;
240
241// static const auto __slots__ = {"_succ", "_pred"};
242
243// auto __getstate__( ) {
244// return std::tuple{"_succ": this->_succ, "_pred": this->_pred};
245// }
246
247// auto __setstate__( state) {
248// this->_succ = state["_succ"];
249// this->_pred = state["_pred"];
250// }
251
252// explicit _Self( succ, pred) {
253// // keys must be the same for two input dicts
254// assert(set(succ.keys(.size()) ^ set(pred.keys())) == 0);
255// this->_succ = succ;
256// this->_pred = pred;
257// }
258
259// auto size( ) {
260// return this->_succ.size(); // length of each dict should be the
261// same
262// }
263
264// auto __iter__( ) {
265// return iter(this->_succ);
266// }
267
268// auto operator[]( nbr) {
269// return UnionAtlas(this->_succ[nbr], this->_pred[nbr]);
270// }
271
272// auto copy( ) {
273// return std::tuple{n: self[n].copy() for n : this->_succ};
274// }
275
276// auto __str__( ) {
277// return str({nbr: self[nbr] for nbr : self});
278// }
279
280// auto __repr__( ) {
281// return "%s(%r, %r)" % (this->__class__.__name__, this->_succ,
282// this->_pred);
283// }
284// };
285
286// class UnionMultiInner(UnionAtlas) {
287// /** A read-only union of two inner dicts of MultiAdjacencies.
288
289// The two input dict-of-dict-of-dicts represent the union of
290// `gra.succ[node]` and `gra.pred[node]` for MultiDiGraphs.
291// Return values are UnionAtlas.
292// The inner level of dict is read-write. But the outer levels are
293// read-only.
294
295// See Also
296// ========
297// UnionAtlas - View into dict-of-dict
298// UnionAdjacency - View into dict-of-dict-of-dict
299// UnionMultiAdjacency - View into dict-of-dict-of-dict-of-dict
300// */
301// static const auto __slots__ = () // Still uses UnionAtlas slots names
302// _succ, _pred;
303
304// auto operator[]( node) {
305// in_succ = node : this->_succ;
306// in_pred = node : this->_pred;
307// if (in_succ) {
308// if (in_pred) {
309// return UnionAtlas(this->_succ[node], this->_pred[node]);
310// }
311// return UnionAtlas(this->_succ[node], {});
312// }
313// return UnionAtlas({}, this->_pred[node]);
314// }
315
316// auto copy( ) {
317// nodes = set(this->_succ.keys()) | set(this->_pred.keys());
318// return std::tuple{n: self[n].copy() for n : nodes};
319// }
320
321// class UnionMultiAdjacency(UnionAdjacency) {
322// /** A read-only union of two dict MultiAdjacencies.
323
324// The two input dict-of-dict-of-dict-of-dicts represent the union of
325// `gra.succ` and `gra.pred` for MultiDiGraphs. Return values are
326// UnionAdjacency. The inner level of dict is read-write. But the outer
327// levels are read-only.
328
329// See Also
330// ========
331// UnionAtlas - View into dict-of-dict
332// UnionMultiInner - View into dict-of-dict-of-dict
333// */
334// static const auto __slots__ = (); // Still uses UnionAdjacency slots
335// names _succ, _pred;
336
337// auto operator[]( node) {
338// return UnionMultiInner(this->_succ[node], this->_pred[node]);
339// }
340
341// class ReadOnlyGraph: public object {
342// /** A Mixin Class to mask the write methods of a graph class. */
343
344// auto not_allowed( *args, **kwds) {
345// const auto msg = "SubGraph Views are readonly. Mutations not
346// allowed"; throw xnetwork::XNetworkError(msg);
347// }
348
349// add_node = not_allowed;
350// remove_node = not_allowed;
351// add_nodes_from = not_allowed;
352// remove_nodes_from = not_allowed;
353
354// add_edge = not_allowed;
355// remove_edge = not_allowed;
356// add_edges_from = not_allowed;
357// add_weighted_edges_from = not_allowed;
358// remove_edges_from = not_allowed;
359
360// clear = not_allowed;
361// };
362
363// class FilterAtlas : public Mapping { // nodedict, nbrdict, keydict
364// using _Self = FilterAtlas;
365
366// explicit _Self( d, NODE_OK) {
367// this->_atlas = d;
368// this->NODE_OK = NODE_OK;
369// }
370
371// auto size( ) {
372// return sum(1 for n : *this);
373// }
374
375// auto __iter__( ) {
376// if (hasattr(this->NODE_OK, "nodes") {
377// return (n for n : this->NODE_OK.nodes if (n : this->_atlas);
378// }
379// return (n for n : this->_atlas if (this->NODE_OK(n));
380// }
381
382// auto operator[]( key) {
383// if (key : this->_atlas and this->NODE_OK(key) {
384// return this->_atlas[key];
385// }
386// throw KeyError("Key {} not found".format(key));
387// }
388
389// auto copy( ) {
390// if (hasattr(this->NODE_OK, "nodes") {
391// return std::tuple{u: this->_atlas[u] for u : this->NODE_OK.nodes
392// if (u : this->_atlas};
393// }
394// return std::tuple{u: d for u, d : this->_atlas.items();
395// if (this->NODE_OK(u)};
396// }
397
398// auto __str__( ) {
399// return str({nbr: self[nbr] for nbr : self});
400// }
401
402// auto __repr__( ) {
403// return "%s(%r, %r)" % (this->__class__.__name__, this->_atlas,
404// this->NODE_OK);
405// }
406
407// class FilterAdjacency : public Mapping { // edgedict
408// using _Self = FilterAdjacency;
409
410// explicit _Self( d, NODE_OK, EDGE_OK) {
411// this->_atlas = d;
412// this->NODE_OK = NODE_OK;
413// this->EDGE_OK = EDGE_OK;
414// }
415
416// auto size( ) {
417// return sum(1 for n : *this);
418// }
419
420// auto __iter__( ) {
421// if (hasattr(this->NODE_OK, "nodes") {
422// return (n for n : this->NODE_OK.nodes if (n : this->_atlas);
423// }
424// return (n for n : this->_atlas if (this->NODE_OK(n));
425// }
426
427// auto operator[]( node) {
428// if (node : this->_atlas and this->NODE_OK(node) {
429// auto new_node_ok(nbr) {
430// return this->NODE_OK(nbr) and this->EDGE_OK(node, nbr);
431// }
432// return FilterAtlas(this->_atlas[node], new_node_ok);
433// }
434// throw KeyError("Key {} not found".format(node));
435// }
436
437// auto copy( ) {
438// if (hasattr(this->NODE_OK, "nodes") {
439// return std::tuple{u: {v: d for v, d : this->_atlas[u].items()
440// if (this->NODE_OK(v) if (this->EDGE_OK(u, v)}
441// for (auto u : this->NODE_OK.nodes if (u : this->_atlas};
442// }
443// return std::tuple{u: {v: d for v, d : nbrs.items() if
444// (this->NODE_OK(v)
445// if (this->EDGE_OK(u, v)}
446// for (auto u, nbrs : this->_atlas.items()
447// if (this->NODE_OK(u)};
448// }
449
450// auto __str__( ) {
451// return str({nbr: self[nbr] for nbr : self});
452// }
453
454// auto __repr__( ) {
455// return "%s(%r, %r, %r)" % (this->__class__.__name__, this->_atlas,
456// this->NODE_OK, this->EDGE_OK);
457// }
458// };
459
460// class FilterMultiInner(FilterAdjacency) { // muliedge_seconddict
461// auto __iter__( ) {
462// if (hasattr(this->NODE_OK, "nodes") {
463// my_nodes = (n for n : this->NODE_OK.nodes if (n : this->_atlas);
464// } else {
465// my_nodes = (n for n : this->_atlas if (this->NODE_OK(n));
466// }
467// for (auto n : my_nodes) {
468// some_keys_ok = false;
469// for (auto key : this->_atlas[n]) {
470// if (this->EDGE_OK(n, key) {
471// some_keys_ok = true;
472// break;
473// }
474// }
475// if (some_keys_ok == true) {
476// yield n
477// }
478// }
479// }
480
481// auto operator[]( nbr) {
482// if (nbr : this->_atlas and this->NODE_OK(nbr) {
483// auto new_node_ok(key) {
484// return this->EDGE_OK(nbr, key);
485// }
486// return FilterAtlas(this->_atlas[nbr], new_node_ok);
487// }
488// throw KeyError("Key {} not found".format(nbr));
489// }
490
491// auto copy( ) {
492// if (hasattr(this->NODE_OK, "nodes") {
493// return std::tuple{v: {k: d for k, d : this->_atlas[v].items()
494// if (this->EDGE_OK(v, k)}
495// for (auto v : this->NODE_OK.nodes if (v : this->_atlas}
496// }
497// return std::tuple{v: {k: d for k, d : nbrs.items() if
498// (this->EDGE_OK(v, k)}
499// for (auto v, nbrs : this->_atlas.items() if
500// (this->NODE_OK(v)};
501// }
502// };
503
504// class FilterMultiAdjacency(FilterAdjacency) { // multiedgedict
505// auto operator[]( node) {
506// if (node : this->_atlas and this->NODE_OK(node) {
507// auto edge_ok(nbr, key) {
508// return this->NODE_OK(nbr) and this->EDGE_OK(node, nbr, key);
509// }
510// return FilterMultiInner(this->_atlas[node], this->NODE_OK,
511// edge_ok);
512// }
513// throw KeyError("Key {} not found".format(node));
514// }
515
516// auto copy( ) {
517// if (hasattr(this->NODE_OK, "nodes") {
518// my_nodes = this->NODE_OK.nodes;
519// return std::tuple{u: {v: {k: d for k, d : kd.items()
520// if (this->EDGE_OK(u, v, k)}
521// for (auto v, kd : this->_atlas[u].items() if (v :
522// my_nodes}
523// for (auto u : my_nodes if (u : this->_atlas};
524// }
525// return std::tuple{u: {v: {k: d for k, d : kd.items()
526// if (this->EDGE_OK(u, v, k)}
527// for (auto v, kd : nbrs.items() if (this->NODE_OK(v)}
528// for (auto u, nbrs : this->_atlas.items() if
529// (this->NODE_OK(u)};
530// }
531// };
Read-only map of maps of maps (view into a dict-of-dict-of-dict structure)
Definition coreviews.hpp:109
AdjacencyView(Atlas &d)
Construct an AdjacencyView from an Atlas container.
Definition coreviews.hpp:115
Read-only mapping of mappings (view into a dict-of-dict data structure)
Definition coreviews.hpp:34
typename std::remove_reference_t< Atlas >::size_type size_type
Definition coreviews.hpp:36
auto at(const T &key) const -> const auto &
Access element at specified key (const version)
Definition coreviews.hpp:79
auto begin() const
Get iterator to the beginning of the view.
Definition coreviews.hpp:55
auto operator[](const T &key) const -> const auto &
Access element at specified key (const version)
Definition coreviews.hpp:69
auto size() const -> size_t
Get the number of elements in the view.
Definition coreviews.hpp:49
auto end() const
Get iterator to the end of the view.
Definition coreviews.hpp:61
auto operator[](const T &key) -> auto &
Access element at specified key (non-const version)
Definition coreviews.hpp:89
AtlasView(Atlas &d)
Construct an AtlasView from an Atlas container.
Definition coreviews.hpp:43
Atlas & _atlas
Definition coreviews.hpp:37