SNAP Library 6.0, Developer Reference  2020-12-09 16:24:20
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
network.cpp
Go to the documentation of this file.
1 void TNEANet::LoadNetworkShM(TShMIn& ShMIn) {
3  MxNId = TInt(ShMIn);
4  MxEId = TInt(ShMIn);
5 
6  LoadTNodeFunctor NodeFn;
7  NodeH.LoadShM(ShMIn, NodeFn);
8 
9  EdgeH.LoadShM(ShMIn);
10  KeyToIndexTypeN.LoadShM(ShMIn);
11  KeyToIndexTypeE.LoadShM(ShMIn);
12 
13  KeyToDenseN.LoadShM(ShMIn);
14  KeyToDenseE.LoadShM(ShMIn);
15 
16  IntDefaultsN.LoadShM(ShMIn);
17  IntDefaultsE.LoadShM(ShMIn);
18  StrDefaultsN.LoadShM(ShMIn);
19  StrDefaultsE.LoadShM(ShMIn);
20  FltDefaultsE.LoadShM(ShMIn);
21  FltDefaultsE.LoadShM(ShMIn);
22 
23  LoadVecFunctor vec_fn;
24  VecOfIntVecsN.LoadShM(ShMIn, vec_fn);
25  VecOfIntVecsE.LoadShM(ShMIn, vec_fn);
26  /* Strings and floats are complicated, so don't optimize on these */
27  VecOfStrVecsN.Load(ShMIn);
28  VecOfStrVecsE.Load(ShMIn);
29  VecOfFltVecsN.Load(ShMIn);
30  VecOfFltVecsE.Load(ShMIn);
31 
32  LoadVecOfVecFunctor vec_of_vec_fn;
33  VecOfIntVecVecsN.LoadShM(ShMIn, vec_of_vec_fn);
34  VecOfIntVecVecsE.LoadShM(ShMIn, vec_of_vec_fn);
35  VecOfFltVecVecsN.LoadShM(ShMIn, vec_of_vec_fn);
36  VecOfFltVecVecsE.LoadShM(ShMIn, vec_of_vec_fn);
37 
38  LoadHashOfVecFunctor hash_of_vec_fn;
39  VecOfIntHashVecsN.LoadShM(ShMIn, hash_of_vec_fn);
40  VecOfIntHashVecsE.LoadShM(ShMIn, hash_of_vec_fn);
41  VecOfFltHashVecsN.LoadShM(ShMIn, hash_of_vec_fn);
42  VecOfFltHashVecsE.LoadShM(ShMIn, hash_of_vec_fn);
43 
44  /* Attributes are complicated so load these straight */
45  SAttrN.Load(ShMIn);
46  SAttrE.Load(ShMIn);
47 }
48 
49 // Attribute Node Edge Network
50 bool TNEANet::HasFlag(const TGraphFlag& Flag) const {
51  return HasGraphFlag(TNEANet::TNet, Flag);
52 }
53 
54 bool TNEANet::TNodeI::IsInNId(const int& NId) const {
55  const TNode& Node = NodeHI.GetDat();
56  for (int edge = 0; edge < Node.GetInDeg(); edge++) {
57  if (NId == Graph->GetEdge(Node.GetInEId(edge)).GetSrcNId())
58  return true;
59  }
60  return false;
61 }
62 
63 bool TNEANet::TNodeI::IsOutNId(const int& NId) const {
64  const TNode& Node = NodeHI.GetDat();
65  for (int edge = 0; edge < Node.GetOutDeg(); edge++) {
66  if (NId == Graph->GetEdge(Node.GetOutEId(edge)).GetDstNId())
67  return true;
68  }
69  return false;
70 }
71 
72 void TNEANet::AttrNameNI(const TInt& NId, TStrIntPrH::TIter NodeHI, TStrV& Names) const {
73  Names = TVec<TStr>();
74  while (!NodeHI.IsEnd()) {
75  if (!NodeAttrIsDeleted(NId, NodeHI)) {
76  Names.Add(NodeHI.GetKey());
77  }
78  NodeHI++;
79  }
80 }
81 
82 void TNEANet::AttrValueNI(const TInt& NId , TStrIntPrH::TIter NodeHI, TStrV& Values) const {
83  Values = TVec<TStr>();
84  while (!NodeHI.IsEnd()) {
85  if (!NodeAttrIsDeleted(NId, NodeHI)) {
86  Values.Add(GetNodeAttrValue(NId, NodeHI));
87  }
88  NodeHI++;
89  }
90 }
91 
92 void TNEANet::IntAttrNameNI(const TInt& NId, TStrIntPrH::TIter NodeHI, TStrV& Names) const {
93  Names = TVec<TStr>();
94  while (!NodeHI.IsEnd()) {
95  if (NodeHI.GetDat().Val1 == IntType && !NodeAttrIsIntDeleted(NId, NodeHI)) {
96  Names.Add(NodeHI.GetKey());
97  }
98  NodeHI++;
99  }
100 }
101 
102 void TNEANet::IntAttrValueNI(const TInt& NId, TStrIntPrH::TIter NodeHI, TIntV& Values) const {
103  Values = TVec<TInt>();
104  while (!NodeHI.IsEnd()) {
105  if (NodeHI.GetDat().Val1 == IntType && !NodeAttrIsIntDeleted(NId, NodeHI)) {
106  TInt val = this->VecOfIntVecsN.GetVal(NodeHI.GetDat().Val2).GetVal(NodeH.GetKeyId(NId));
107  Values.Add(val);
108  }
109  NodeHI++;
110  }
111 }
112 
113 void TNEANet::IntVAttrNameNI(const TInt& NId, TStrIntPrH::TIter NodeHI, TStrV& Names) const {
114  Names = TVec<TStr>();
115  while (!NodeHI.IsEnd()) {
116  if (NodeHI.GetDat().Val1 == IntVType) {
117  Names.Add(NodeHI.GetKey());
118  }
119  NodeHI++;
120  }
121 }
122 
123 void TNEANet::IntVAttrValueNI(const TInt& NId, TStrIntPrH::TIter NodeHI, TVec<TIntV>& Values) const {
124  Values = TVec<TIntV>();
125  while (!NodeHI.IsEnd()) {
126  if (NodeHI.GetDat().Val1 == IntVType) {
127  TInt index = NodeHI.GetDat().Val2;
128  TStr attr = NodeHI.GetKey();
129  TInt loc = CheckDenseOrSparseN(attr);
130  if (loc == 1) {
131  TIntV val = this->VecOfIntVecVecsN.GetVal(index).GetVal(NodeH.GetKeyId(NId));
132  if (val.Len() != 0) Values.Add(val);
133  } else {
134  const THash<TInt, TIntV>& NewHash = VecOfIntHashVecsN[index];
135  if (NewHash.IsKey(NodeH.GetKeyId(NId))) {
136  Values.Add(NewHash[NodeH.GetKeyId(NId)]);
137  }
138  }
139  }
140  NodeHI++;
141  }
142 }
143 
144 void TNEANet::FltVAttrNameNI(const TInt& NId, TStrIntPrH::TIter NodeHI, TStrV& Names) const {
145  Names = TVec<TStr>();
146  while (!NodeHI.IsEnd()) {
147  if (NodeHI.GetDat().Val1 == FltVType) {
148  Names.Add(NodeHI.GetKey());
149  }
150  NodeHI++;
151  }
152 }
153 
154 void TNEANet::FltVAttrValueNI(const TInt& NId, TStrIntPrH::TIter NodeHI, TVec<TFltV>& Values) const {
155  Values = TVec<TFltV>();
156  while (!NodeHI.IsEnd()) {
157  if (NodeHI.GetDat().Val1 == FltVType) {
158  TInt index = NodeHI.GetDat().Val2;
159  TStr attr = NodeHI.GetKey();
160  TInt loc = CheckDenseOrSparseN(attr);
161  if (loc == 1) {
162  TFltV val = this->VecOfFltVecVecsN.GetVal(index).GetVal(NodeH.GetKeyId(NId));
163  if (val.Len() != 0) Values.Add(val);
164  } else {
165  const THash<TInt, TFltV>& NewHash = VecOfFltHashVecsN[index];
166  if (NewHash.IsKey(NodeH.GetKeyId(NId))) {
167  Values.Add(NewHash[NodeH.GetKeyId(NId)]);
168  }
169  }
170  }
171  NodeHI++;
172  }
173 }
174 
175 void TNEANet::StrAttrNameNI(const TInt& NId, TStrIntPrH::TIter NodeHI, TStrV& Names) const {
176  Names = TVec<TStr>();
177  while (!NodeHI.IsEnd()) {
178  if (NodeHI.GetDat().Val1 == StrType && !NodeAttrIsStrDeleted(NId, NodeHI)) {
179  Names.Add(NodeHI.GetKey());
180  }
181  NodeHI++;
182  }
183 }
184 
185 void TNEANet::StrAttrValueNI(const TInt& NId, TStrIntPrH::TIter NodeHI, TStrV& Values) const {
186  Values = TVec<TStr>();
187  while (!NodeHI.IsEnd()) {
188  if (NodeHI.GetDat().Val1 == StrType && !NodeAttrIsStrDeleted(NId, NodeHI)) {
189  TStr val = this->VecOfStrVecsN.GetVal(NodeHI.GetDat().Val2).GetVal(NodeH.GetKeyId(NId));
190  Values.Add(val);
191  }
192  NodeHI++;
193  }
194 }
195 
196 void TNEANet::FltAttrNameNI(const TInt& NId, TStrIntPrH::TIter NodeHI, TStrV& Names) const {
197  Names = TVec<TStr>();
198  while (!NodeHI.IsEnd()) {
199  if (NodeHI.GetDat().Val1 == FltType && !NodeAttrIsFltDeleted(NId, NodeHI)) {
200  Names.Add(NodeHI.GetKey());
201  }
202  NodeHI++;
203  }
204 }
205 
206 void TNEANet::FltAttrValueNI(const TInt& NId, TStrIntPrH::TIter NodeHI, TFltV& Values) const {
207  Values = TVec<TFlt>();
208  while (!NodeHI.IsEnd()) {
209  if (NodeHI.GetDat().Val1 == FltType && !NodeAttrIsFltDeleted(NId, NodeHI)) {
210  TFlt val = (this->VecOfFltVecsN.GetVal(NodeHI.GetDat().Val2).GetVal(NodeH.GetKeyId(NId)));
211  Values.Add(val);
212  }
213  NodeHI++;
214  }
215 }
216 
217 bool TNEANet::IsAttrDeletedN(const int& NId, const TStr& attr) const {
218  bool IntDel = IsIntAttrDeletedN(NId, attr);
219  bool StrDel = IsStrAttrDeletedN(NId, attr);
220  bool FltDel = IsFltAttrDeletedN(NId, attr);
221  bool IntVDel = IsIntVAttrDeletedN(NId, attr);
222  bool FltVDel = IsFltVAttrDeletedN(NId, attr);
223  return IntDel || StrDel || FltDel || IntVDel || FltVDel;
224 }
225 
226 bool TNEANet::IsIntAttrDeletedN(const int& NId, const TStr& attr) const {
227  return NodeAttrIsIntDeleted(NId, KeyToIndexTypeN.GetI(attr));
228 }
229 
230 bool TNEANet::IsIntVAttrDeletedN(const int& NId, const TStr& attr) const {
231  return NodeAttrIsIntVDeleted(NId, KeyToIndexTypeN.GetI(attr));
232 }
233 
234 bool TNEANet::IsFltVAttrDeletedN(const int& NId, const TStr& attr) const {
235  return NodeAttrIsFltVDeleted(NId, KeyToIndexTypeN.GetI(attr));
236 }
237 
238 bool TNEANet::IsStrAttrDeletedN(const int& NId, const TStr& attr) const {
239  return NodeAttrIsStrDeleted(NId, KeyToIndexTypeN.GetI(attr));
240 }
241 
242 bool TNEANet::IsFltAttrDeletedN(const int& NId, const TStr& attr) const {
243  return NodeAttrIsFltDeleted(NId, KeyToIndexTypeN.GetI(attr));
244 }
245 
246 bool TNEANet::NodeAttrIsDeleted(const int& NId, const TStrIntPrH::TIter& NodeHI) const {
247  bool IntDel = NodeAttrIsIntDeleted(NId, NodeHI);
248  bool StrDel = NodeAttrIsStrDeleted(NId, NodeHI);
249  bool FltDel = NodeAttrIsFltDeleted(NId, NodeHI);
250  bool IntVDel = NodeAttrIsIntVDeleted(NId, NodeHI);
251  return IntDel || StrDel || FltDel || IntVDel;
252 }
253 
254 bool TNEANet::NodeAttrIsIntDeleted(const int& NId, const TStrIntPrH::TIter& NodeHI) const {
255  if (NodeHI.GetDat().Val1 != IntType) {
256  return false;
257  }
258  return (GetIntAttrDefaultN(NodeHI.GetKey()) == this->VecOfIntVecsN.GetVal(
259  this->KeyToIndexTypeN.GetDat(NodeHI.GetKey()).Val2).GetVal(NodeH.GetKeyId(NId)));
260 }
261 
262 bool TNEANet::NodeAttrIsIntVDeleted(const int& NId, const TStrIntPrH::TIter& NodeHI) const {
263  if (NodeHI.GetDat().Val1 != IntVType) {
264  return false;
265  }
266  return (TIntV() == this->VecOfIntVecVecsN.GetVal(
267  this->KeyToIndexTypeN.GetDat(NodeHI.GetKey()).Val2).GetVal(NodeH.GetKeyId(NId)));
268 }
269 
270 bool TNEANet::NodeAttrIsFltVDeleted(const int& NId, const TStrIntPrH::TIter& NodeHI) const {
271  if (NodeHI.GetDat().Val1 != FltVType) {
272  return false;
273  }
274  return (TFltV() == this->VecOfFltVecVecsN.GetVal(
275  this->KeyToIndexTypeN.GetDat(NodeHI.GetKey()).Val2).GetVal(NodeH.GetKeyId(NId)));
276 }
277 
278 bool TNEANet::NodeAttrIsStrDeleted(const int& NId, const TStrIntPrH::TIter& NodeHI) const {
279  if (NodeHI.GetDat().Val1 != StrType) {
280  return false;
281  }
282  return (GetStrAttrDefaultN(NodeHI.GetKey()) == this->VecOfStrVecsN.GetVal(
283  this->KeyToIndexTypeN.GetDat(NodeHI.GetKey()).Val2).GetVal(NodeH.GetKeyId(NId)));
284 }
285 
286 bool TNEANet::NodeAttrIsFltDeleted(const int& NId, const TStrIntPrH::TIter& NodeHI) const {
287  if (NodeHI.GetDat().Val1 != FltType) {
288  return false;
289  }
290  return (GetFltAttrDefaultN(NodeHI.GetKey()) == this->VecOfFltVecsN.GetVal(
291  this->KeyToIndexTypeN.GetDat(NodeHI.GetKey()).Val2).GetVal(NodeH.GetKeyId(NId)));
292 }
293 
294 TStr TNEANet::GetNodeAttrValue(const int& NId, const TStrIntPrH::TIter& NodeHI) const {
295  if (NodeHI.GetDat().Val1 == IntType) {
296  return (this->VecOfIntVecsN.GetVal(
297  this->KeyToIndexTypeN.GetDat(NodeHI.GetKey()).Val2).GetVal(NodeH.GetKeyId(NId))).GetStr();
298  } else if(NodeHI.GetDat().Val1 == StrType) {
299  return this->VecOfStrVecsN.GetVal(
300  this->KeyToIndexTypeN.GetDat(NodeHI.GetKey()).Val2).GetVal(NodeH.GetKeyId(NId));
301  } else if (NodeHI.GetDat().Val1 == FltType) {
302  return (this->VecOfFltVecsN.GetVal(
303  this->KeyToIndexTypeN.GetDat(NodeHI.GetKey()).Val2).GetVal(NodeH.GetKeyId(NId))).GetStr();
304  }
305  return TStr::GetNullStr();
306 }
307 
308 void TNEANet::AttrNameEI(const TInt& EId, TStrIntPrH::TIter EdgeHI, TStrV& Names) const {
309  Names = TVec<TStr>();
310  while (!EdgeHI.IsEnd()) {
311  if (!EdgeAttrIsDeleted(EId, EdgeHI)) {
312  Names.Add(EdgeHI.GetKey());
313  }
314  EdgeHI++;
315  }
316 }
317 
318 void TNEANet::AttrValueEI(const TInt& EId, TStrIntPrH::TIter EdgeHI, TStrV& Values) const {
319  Values = TVec<TStr>();
320  while (!EdgeHI.IsEnd()) {
321  if (!EdgeAttrIsDeleted(EId, EdgeHI)) {
322  Values.Add(GetEdgeAttrValue(EId, EdgeHI));
323  }
324  EdgeHI++;
325  }
326 }
327 
328 void TNEANet::IntAttrNameEI(const TInt& EId, TStrIntPrH::TIter EdgeHI, TStrV& Names) const {
329  Names = TVec<TStr>();
330  while (!EdgeHI.IsEnd()) {
331  if (EdgeHI.GetDat().Val1 == IntType && !EdgeAttrIsIntDeleted(EId, EdgeHI)) {
332  Names.Add(EdgeHI.GetKey());
333  }
334  EdgeHI++;
335  }
336 }
337 
338 void TNEANet::IntAttrValueEI(const TInt& EId, TStrIntPrH::TIter EdgeHI, TIntV& Values) const {
339  Values = TVec<TInt>();
340  while (!EdgeHI.IsEnd()) {
341  if (EdgeHI.GetDat().Val1 == IntType && !EdgeAttrIsIntDeleted(EId, EdgeHI)) {
342  TInt val = (this->VecOfIntVecsE[EdgeHI.GetDat().Val2].GetVal(EdgeH.GetKeyId(EId)));
343  Values.Add(val);
344  }
345  EdgeHI++;
346  }
347 }
348 
349 void TNEANet::IntVAttrNameEI(const TInt& EId, TStrIntPrH::TIter EdgeHI, TStrV& Names) const {
350  Names = TVec<TStr>();
351  while (!EdgeHI.IsEnd()) {
352  if (EdgeHI.GetDat().Val1 == IntVType) {
353  Names.Add(EdgeHI.GetKey());
354  }
355  EdgeHI++;
356  }
357 }
358 
359 void TNEANet::IntVAttrValueEI(const TInt& EId, TStrIntPrH::TIter EdgeHI, TVec<TIntV>& Values) const {
360  Values = TVec<TIntV>();
361  while (!EdgeHI.IsEnd()) {
362  if (EdgeHI.GetDat().Val1 == IntVType) {
363  TInt index = EdgeHI.GetDat().Val2;
364  TStr attr = EdgeHI.GetKey();
365  TInt loc = CheckDenseOrSparseE(attr);
366  if (loc == 1) {
367  TIntV val = this->VecOfIntVecVecsE.GetVal(index).GetVal(EdgeH.GetKeyId(EId));
368  if (val.Len() != 0) Values.Add(val);
369  } else {
370  const THash<TInt, TIntV>& NewHash = VecOfIntHashVecsE[index];
371  if (NewHash.IsKey(EdgeH.GetKeyId(EId))) {
372  Values.Add(NewHash[EdgeH.GetKeyId(EId)]);
373  }
374  }
375  }
376  EdgeHI++;
377  }
378 }
379 
380 void TNEANet::FltVAttrNameEI(const TInt& EId, TStrIntPrH::TIter EdgeHI, TStrV& Names) const {
381  Names = TVec<TStr>();
382  while (!EdgeHI.IsEnd()) {
383  if (EdgeHI.GetDat().Val1 == FltVType) {
384  Names.Add(EdgeHI.GetKey());
385  }
386  EdgeHI++;
387  }
388 }
389 
390 void TNEANet::FltVAttrValueEI(const TInt& EId, TStrIntPrH::TIter EdgeHI, TVec<TFltV>& Values) const {
391  Values = TVec<TFltV>();
392  while (!EdgeHI.IsEnd()) {
393  if (EdgeHI.GetDat().Val1 == FltVType) {
394  TInt index = EdgeHI.GetDat().Val2;
395  TStr attr = EdgeHI.GetKey();
396  TInt loc = CheckDenseOrSparseE(attr);
397  if (loc == 1) {
398  TFltV val = this->VecOfFltVecVecsE.GetVal(index).GetVal(EdgeH.GetKeyId(EId));
399  if (val.Len() != 0) Values.Add(val);
400  } else {
401  const THash<TInt, TFltV>& NewHash = VecOfFltHashVecsE[index];
402  if (NewHash.IsKey(EdgeH.GetKeyId(EId))) {
403  Values.Add(NewHash[EdgeH.GetKeyId(EId)]);
404  }
405  }
406  }
407  EdgeHI++;
408  }
409 }
410 
411 void TNEANet::StrAttrNameEI(const TInt& EId, TStrIntPrH::TIter EdgeHI, TStrV& Names) const {
412  Names = TVec<TStr>();
413  while (!EdgeHI.IsEnd()) {
414  if (EdgeHI.GetDat().Val1 == StrType && !EdgeAttrIsStrDeleted(EId, EdgeHI)) {
415  Names.Add(EdgeHI.GetKey());
416  }
417  EdgeHI++;
418  }
419 }
420 
421 void TNEANet::StrAttrValueEI(const TInt& EId, TStrIntPrH::TIter EdgeHI, TStrV& Values) const {
422  Values = TVec<TStr>();
423  while (!EdgeHI.IsEnd()) {
424  if (EdgeHI.GetDat().Val1 == StrType && !EdgeAttrIsStrDeleted(EId, EdgeHI)) {
425  TStr val = this->VecOfStrVecsE.GetVal(EdgeHI.GetDat().Val2).GetVal(EId);
426  Values.Add(val);
427  }
428  EdgeHI++;
429  }
430 }
431 
432 void TNEANet::FltAttrNameEI(const TInt& EId, TStrIntPrH::TIter EdgeHI, TStrV& Names) const {
433  Names = TVec<TStr>();
434  while (!EdgeHI.IsEnd()) {
435  if (EdgeHI.GetDat().Val1 == FltType && !EdgeAttrIsFltDeleted(EId, EdgeHI)) {
436  Names.Add(EdgeHI.GetKey());
437  }
438  EdgeHI++;
439  }
440 }
441 
442 void TNEANet::FltAttrValueEI(const TInt& EId, TStrIntPrH::TIter EdgeHI, TFltV& Values) const {
443  Values = TVec<TFlt>();
444  while (!EdgeHI.IsEnd()) {
445  if (EdgeHI.GetDat().Val1 == FltType && !EdgeAttrIsFltDeleted(EId, EdgeHI)) {
446  TFlt val = (this->VecOfFltVecsE.GetVal(EdgeHI.GetDat().Val2).GetVal(EId));
447  Values.Add(val);
448  }
449  EdgeHI++;
450  }
451 }
452 
453 bool TNEANet::IsAttrDeletedE(const int& EId, const TStr& attr) const {
454  bool IntDel = IsIntAttrDeletedE(EId, attr);
455  bool IntVDel = IsIntVAttrDeletedE(EId, attr);
456  bool StrDel = IsStrAttrDeletedE(EId, attr);
457  bool FltDel = IsFltAttrDeletedE(EId, attr);
458  bool FltVDel = IsFltVAttrDeletedE(EId, attr);
459  return IntDel || StrDel || FltDel || IntVDel || FltVDel;
460 }
461 
462 bool TNEANet::IsIntAttrDeletedE(const int& EId, const TStr& attr) const {
463  return EdgeAttrIsIntDeleted(EId, KeyToIndexTypeE.GetI(attr));
464 }
465 
466 bool TNEANet::IsIntVAttrDeletedE(const int& EId, const TStr& attr) const {
467  return EdgeAttrIsIntVDeleted(EId, KeyToIndexTypeE.GetI(attr));
468 }
469 
470 bool TNEANet::IsFltVAttrDeletedE(const int& EId, const TStr& attr) const {
471  return EdgeAttrIsFltVDeleted(EId, KeyToIndexTypeE.GetI(attr));
472 }
473 
474 bool TNEANet::IsStrAttrDeletedE(const int& EId, const TStr& attr) const {
475  return EdgeAttrIsStrDeleted(EId, KeyToIndexTypeE.GetI(attr));
476 }
477 
478 bool TNEANet::IsFltAttrDeletedE(const int& EId, const TStr& attr) const {
479  return EdgeAttrIsFltDeleted(EId, KeyToIndexTypeE.GetI(attr));
480 }
481 
482 bool TNEANet::EdgeAttrIsDeleted(const int& EId, const TStrIntPrH::TIter& EdgeHI) const {
483  bool IntDel = EdgeAttrIsIntDeleted(EId, EdgeHI);
484  bool IntVDel = EdgeAttrIsIntVDeleted(EId, EdgeHI);
485  bool StrDel = EdgeAttrIsStrDeleted(EId, EdgeHI);
486  bool FltDel = EdgeAttrIsFltDeleted(EId, EdgeHI);
487  bool FltVDel = EdgeAttrIsFltVDeleted(EId, EdgeHI);
488  return IntDel || StrDel || FltDel || IntVDel || FltVDel;
489 }
490 
491 bool TNEANet::EdgeAttrIsIntDeleted(const int& EId, const TStrIntPrH::TIter& EdgeHI) const {
492  return (EdgeHI.GetDat().Val1 == IntType &&
493  GetIntAttrDefaultE(EdgeHI.GetKey()) == this->VecOfIntVecsE.GetVal(
494  this->KeyToIndexTypeE.GetDat(EdgeHI.GetKey()).Val2).GetVal(EdgeH.GetKeyId(EId)));
495 }
496 
497 bool TNEANet::EdgeAttrIsIntVDeleted(const int& EId, const TStrIntPrH::TIter& EdgeHI) const {
498  return (EdgeHI.GetDat().Val1 == IntVType &&
499  TIntV() == this->VecOfIntVecVecsE.GetVal(
500  this->KeyToIndexTypeE.GetDat(EdgeHI.GetKey()).Val2).GetVal(EdgeH.GetKeyId(EId)));
501 }
502 
503 bool TNEANet::EdgeAttrIsFltVDeleted(const int& EId, const TStrIntPrH::TIter& EdgeHI) const {
504  return (EdgeHI.GetDat().Val1 == FltVType &&
505  TFltV() == this->VecOfFltVecVecsE.GetVal(
506  this->KeyToIndexTypeE.GetDat(EdgeHI.GetKey()).Val2).GetVal(EdgeH.GetKeyId(EId)));
507 }
508 
509 bool TNEANet::EdgeAttrIsStrDeleted(const int& EId, const TStrIntPrH::TIter& EdgeHI) const {
510  return (EdgeHI.GetDat().Val1 == StrType &&
511  GetStrAttrDefaultE(EdgeHI.GetKey()) == this->VecOfStrVecsE.GetVal(
512  this->KeyToIndexTypeE.GetDat(EdgeHI.GetKey()).Val2).GetVal(EdgeH.GetKeyId(EId)));
513 }
514 
515 bool TNEANet::EdgeAttrIsFltDeleted(const int& EId, const TStrIntPrH::TIter& EdgeHI) const {
516  return (EdgeHI.GetDat().Val1 == FltType &&
517  GetFltAttrDefaultE(EdgeHI.GetKey()) == this->VecOfFltVecsE.GetVal(
518  this->KeyToIndexTypeE.GetDat(EdgeHI.GetKey()).Val2).GetVal(EdgeH.GetKeyId(EId)));
519 }
520 
521 TStr TNEANet::GetEdgeAttrValue(const int& EId, const TStrIntPrH::TIter& EdgeHI) const {
522  if (EdgeHI.GetDat().Val1 == IntType) {
523  return (this->VecOfIntVecsE.GetVal(
524  this->KeyToIndexTypeE.GetDat(EdgeHI.GetKey()).Val2).GetVal(EdgeH.GetKeyId(EId))).GetStr();
525  } else if(EdgeHI.GetDat().Val1 == StrType) {
526  return this->VecOfStrVecsE.GetVal(
527  this->KeyToIndexTypeE.GetDat(EdgeHI.GetKey()).Val2).GetVal(EdgeH.GetKeyId(EId));
528  } else if (EdgeHI.GetDat().Val1 == FltType) {
529  return (this->VecOfFltVecsE.GetVal(
530  this->KeyToIndexTypeE.GetDat(EdgeHI.GetKey()).Val2).GetVal(EdgeH.GetKeyId(EId))).GetStr();
531  }
532  return TStr::GetNullStr();
533 }
534 
535 int TNEANet::AddNode(int NId) {
536  if (NId == -1) {
537  NId = MxNId; MxNId++;
538  } else {
539  IAssertR(!IsNode(NId), TStr::Fmt("NodeId %d already exists", NId));
540  MxNId = TMath::Mx(NId+1, MxNId());
541  }
542  NodeH.AddDat(NId, TNode(NId));
543  AddAttributes(NId);
544  return NId;
545 }
546 
548  if (NId == -1) {
549  NId = MxNId; MxNId++;
550  } else {
551  if (IsNode(NId)) { return -1;}
552  MxNId = TMath::Mx(NId+1, MxNId());
553  }
554  NodeH.AddDat(NId, TNode(NId));
555  AddAttributes(NId);
556  return NId;
557 }
558 
559 int TNEANet::AddAttributes(const int NId) {
560  int i;
561  // update attribute columns
562  for (i = 0; i < VecOfIntVecsN.Len(); i++) {
563  TVec<TInt>& IntVec = VecOfIntVecsN[i];
564  int KeyId = NodeH.GetKeyId(NId);
565  if (IntVec.Len() > KeyId) {
566  IntVec[KeyId] = TInt::Mn;
567  } else {
568  IntVec.Ins(KeyId, TInt::Mn);
569  }
570  }
571  TVec<TStr> DefIntVec = TVec<TStr>();
572  IntDefaultsN.GetKeyV(DefIntVec);
573  for (i = 0; i < DefIntVec.Len(); i++) {
574  TStr attr = DefIntVec[i];
575  TVec<TInt>& IntVec = VecOfIntVecsN[KeyToIndexTypeN.GetDat(DefIntVec[i]).Val2];
576  IntVec[NodeH.GetKeyId(NId)] = GetIntAttrDefaultN(attr);
577  }
578  for (i = 0; i < VecOfStrVecsN.Len(); i++) {
579  TVec<TStr>& StrVec = VecOfStrVecsN[i];
580  int KeyId = NodeH.GetKeyId(NId);
581  if (StrVec.Len() > KeyId) {
582  StrVec[KeyId] = TStr::GetNullStr();
583  } else {
584  StrVec.Ins(KeyId, TStr::GetNullStr());
585  }
586  }
587  TVec<TStr> DefStrVec = TVec<TStr>();
588  StrDefaultsN.GetKeyV(DefStrVec);
589  for (i = 0; i < DefStrVec.Len(); i++) {
590  TStr attr = DefStrVec[i];
591  TVec<TStr>& StrVec = VecOfStrVecsN[KeyToIndexTypeN.GetDat(DefStrVec[i]).Val2];
592  StrVec[NodeH.GetKeyId(NId)] = GetStrAttrDefaultN(attr);
593  }
594  for (i = 0; i < VecOfFltVecsN.Len(); i++) {
595  TVec<TFlt>& FltVec = VecOfFltVecsN[i];
596  int KeyId = NodeH.GetKeyId(NId);
597  if (FltVec.Len() > KeyId) {
598  FltVec[KeyId] = TFlt::Mn;
599  } else {
600  FltVec.Ins(KeyId, TFlt::Mn);
601  }
602  }
603  TVec<TStr> DefFltVec = TVec<TStr>();
604  FltDefaultsN.GetKeyV(DefFltVec);
605  for (i = 0; i < DefFltVec.Len(); i++) {
606  TStr attr = DefFltVec[i];
607  TVec<TFlt>& FltVec = VecOfFltVecsN[KeyToIndexTypeN.GetDat(DefFltVec[i]).Val2];
608  FltVec[NodeH.GetKeyId(NId)] = GetFltAttrDefaultN(attr);
609  }
610  for (i = 0; i < VecOfIntVecVecsN.Len(); i++) {
611  TVec<TIntV>& IntVecV = VecOfIntVecVecsN[i];
612  int KeyId = NodeH.GetKeyId(NId);
613  if (IntVecV.Len() > KeyId) {
614  IntVecV[KeyId] = TIntV();
615  } else {
616  IntVecV.Ins(KeyId, TIntV());
617  }
618  }
619  for (i = 0; i < VecOfFltVecVecsN.Len(); i++) {
620  TVec<TFltV>& FltVecV = VecOfFltVecVecsN[i];
621  int KeyId = NodeH.GetKeyId(NId);
622  if (FltVecV.Len() > KeyId) {
623  FltVecV[KeyId] = TFltV();
624  } else {
625  FltVecV.Ins(KeyId, TFltV());
626  }
627  }
628  return NId;
629 }
630 
631 void TNEANet::DelNode(const int& NId) {
632  int i;
633  TInt Id(NId);
634  SAttrN.DelSAttrId(Id);
635  const TNode& Node = GetNode(NId);
636  for (int out = 0; out < Node.GetOutDeg(); out++) {
637  const int EId = Node.GetOutEId(out);
638  const TEdge& Edge = GetEdge(EId);
639  IAssert(Edge.GetSrcNId() == NId);
640  GetNode(Edge.GetDstNId()).InEIdV.DelIfIn(EId);
641  // Delete from Edge Attributes
642  for (i = 0; i < VecOfIntVecsE.Len(); i++) {
643  TVec<TInt>& IntVec = VecOfIntVecsE[i];
644  IntVec[EdgeH.GetKeyId(EId)] = TInt::Mn;
645  }
646  for (i = 0; i < VecOfStrVecsE.Len(); i++) {
647  TVec<TStr>& StrVec = VecOfStrVecsE[i];
648  StrVec[EdgeH.GetKeyId(EId)] = TStr::GetNullStr();
649  }
650  for (i = 0; i < VecOfFltVecsE.Len(); i++) {
651  TVec<TFlt>& FltVec = VecOfFltVecsE[i];
652  FltVec[EdgeH.GetKeyId(EId)] = TFlt::Mn;
653  }
654  for (i = 0; i < VecOfIntVecVecsE.Len(); i++) {
655  TVec<TIntV>& IntVecV = VecOfIntVecVecsE[i];
656  IntVecV[EdgeH.GetKeyId(EId)] = TIntV();
657  }
658  for (i = 0; i < VecOfFltVecVecsE.Len(); i++) {
659  TVec<TFltV>& FltVecV = VecOfFltVecVecsE[i];
660  FltVecV[EdgeH.GetKeyId(EId)] = TFltV();
661  }
662  for (i = 0; i < VecOfIntHashVecsE.Len(); i++) {
663  THash<TInt, TIntV>& IntHashV = VecOfIntHashVecsE[i];
664  if (IntHashV.IsKey(EdgeH.GetKeyId(EId))) {
665  IntHashV.DelKey(EdgeH.GetKeyId(EId));
666  }
667  }
668  for (i = 0; i < VecOfFltHashVecsE.Len(); i++) {
669  THash<TInt, TFltV>& FltHashV = VecOfFltHashVecsE[i];
670  if (FltHashV.IsKey(EdgeH.GetKeyId(EId))) {
671  FltHashV.DelKey(EdgeH.GetKeyId(EId));
672  }
673  }
674  EdgeH.DelKey(EId);
675 
676  }
677  for (int in = 0; in < Node.GetInDeg(); in++) {
678  const int EId = Node.GetInEId(in);
679  const TEdge& Edge = GetEdge(EId);
680  IAssert(Edge.GetDstNId() == NId);
681  GetNode(Edge.GetSrcNId()).OutEIdV.DelIfIn(EId);
682  // Delete from Edge Attributes
683  for (i = 0; i < VecOfIntVecsE.Len(); i++) {
684  TVec<TInt>& IntVec = VecOfIntVecsE[i];
685  IntVec[EdgeH.GetKeyId(EId)] = TInt::Mn;
686  }
687  for (i = 0; i < VecOfStrVecsE.Len(); i++) {
688  TVec<TStr>& StrVec = VecOfStrVecsE[i];
689  StrVec[EdgeH.GetKeyId(EId)] = TStr::GetNullStr();
690  }
691  for (i = 0; i < VecOfFltVecsE.Len(); i++) {
692  TVec<TFlt>& FltVec = VecOfFltVecsE[i];
693  FltVec[EdgeH.GetKeyId(EId)] = TFlt::Mn;
694  }
695  for (i = 0; i < VecOfIntVecVecsE.Len(); i++) {
696  TVec<TIntV>& IntVecV = VecOfIntVecVecsE[i];
697  IntVecV[EdgeH.GetKeyId(EId)] = TIntV();
698  }
699  for (i = 0; i < VecOfFltVecVecsE.Len(); i++) {
700  TVec<TFltV>& FltVecV = VecOfFltVecVecsE[i];
701  FltVecV[EdgeH.GetKeyId(EId)] = TFltV();
702  }
703  for (i = 0; i < VecOfIntHashVecsE.Len(); i++) {
704  THash<TInt, TIntV>& IntHashV = VecOfIntHashVecsE[i];
705  if (IntHashV.IsKey(EdgeH.GetKeyId(EId))) {
706  IntHashV.DelKey(EdgeH.GetKeyId(EId));
707  }
708  }
709  for (i = 0; i < VecOfFltHashVecsE.Len(); i++) {
710  THash<TInt, TFltV>& FltHashV = VecOfFltHashVecsE[i];
711  if (FltHashV.IsKey(EdgeH.GetKeyId(EId))) {
712  FltHashV.DelKey(EdgeH.GetKeyId(EId));
713  }
714  }
715  EdgeH.DelKey(EId);
716  }
717 
718  for (i = 0; i < VecOfIntVecsN.Len(); i++) {
719  TVec<TInt>& IntVec = VecOfIntVecsN[i];
720  IntVec[NodeH.GetKeyId(NId)] = TInt::Mn;
721  }
722  for (i = 0; i < VecOfStrVecsN.Len(); i++) {
723  TVec<TStr>& StrVec = VecOfStrVecsN[i];
724  StrVec[NodeH.GetKeyId(NId)] = TStr::GetNullStr();
725  }
726  for (i = 0; i < VecOfFltVecsN.Len(); i++) {
727  TVec<TFlt>& FltVec = VecOfFltVecsN[i];
728  FltVec[NodeH.GetKeyId(NId)] = TFlt::Mn;
729  }
730  for (i = 0; i < VecOfIntVecVecsN.Len(); i++) {
731  TVec<TIntV>& IntVecV = VecOfIntVecVecsN[i];
732  IntVecV[NodeH.GetKeyId(NId)] = TIntV();
733  }
734  for (i = 0; i < VecOfFltVecVecsN.Len(); i++) {
735  TVec<TFltV>& FltVecV = VecOfFltVecVecsN[i];
736  FltVecV[NodeH.GetKeyId(NId)] = TFltV();
737  }
738  for (i = 0; i < VecOfIntHashVecsN.Len(); i++) {
739  THash<TInt, TIntV>& IntHashV = VecOfIntHashVecsN[i];
740  if (IntHashV.IsKey(NodeH.GetKeyId(NId))) {
741  IntHashV.DelKey(NodeH.GetKeyId(NId));
742  }
743  }
744  for (i = 0; i < VecOfFltHashVecsN.Len(); i++) {
745  THash<TInt, TFltV>& FltHashV = VecOfFltHashVecsN[i];
746  if (FltHashV.IsKey(NodeH.GetKeyId(NId))) {
747  FltHashV.DelKey(NodeH.GetKeyId(NId));
748  }
749  }
750  NodeH.DelKey(NId);
751 }
752 
753 int TNEANet::AddEdge(const int& SrcNId, const int& DstNId, int EId) {
754  int i;
755 
756  if (EId == -1) { EId = MxEId; MxEId++; }
757  else { MxEId = TMath::Mx(EId+1, MxEId()); }
758  IAssertR(!IsEdge(EId), TStr::Fmt("EdgeId %d already exists", EId));
759  IAssertR(IsNode(SrcNId) && IsNode(DstNId), TStr::Fmt("%d or %d not a node.", SrcNId, DstNId).CStr());
760  EdgeH.AddDat(EId, TEdge(EId, SrcNId, DstNId));
761  GetNode(SrcNId).OutEIdV.AddSorted(EId);
762  GetNode(DstNId).InEIdV.AddSorted(EId);
763 
764  // update attribute columns
765  for (i = 0; i < VecOfIntVecsE.Len(); i++) {
766  TVec<TInt>& IntVec = VecOfIntVecsE[i];
767  int KeyId = EdgeH.GetKeyId(EId);
768  if (IntVec.Len() > KeyId) {
769  IntVec[KeyId] = TInt::Mn;
770  } else {
771  IntVec.Ins(KeyId, TInt::Mn);
772  }
773  }
774  TVec<TStr> DefIntVec = TVec<TStr>();
775  IntDefaultsE.GetKeyV(DefIntVec);
776  for (i = 0; i < DefIntVec.Len(); i++) {
777  TStr attr = DefIntVec[i];
778  TVec<TInt>& IntVec = VecOfIntVecsE[KeyToIndexTypeE.GetDat(DefIntVec[i]).Val2];
779  IntVec[EdgeH.GetKeyId(EId)] = GetIntAttrDefaultE(attr);
780  }
781 
782  for (i = 0; i < VecOfIntVecVecsE.Len(); i++) {
783  TVec<TIntV>& IntVecV = VecOfIntVecVecsE[i];
784  IntVecV.Ins(EdgeH.GetKeyId(EId), TIntV());
785  }
786 
787  for (i = 0; i < VecOfFltVecVecsE.Len(); i++) {
788  TVec<TFltV>& FltVecV = VecOfFltVecVecsE[i];
789  FltVecV.Ins(EdgeH.GetKeyId(EId), TFltV());
790  }
791 
792  for (i = 0; i < VecOfStrVecsE.Len(); i++) {
793  TVec<TStr>& StrVec = VecOfStrVecsE[i];
794  int KeyId = EdgeH.GetKeyId(EId);
795  if (StrVec.Len() > KeyId) {
796  StrVec[KeyId] = TStr::GetNullStr();
797  } else {
798  StrVec.Ins(KeyId, TStr::GetNullStr());
799  }
800  }
801  TVec<TStr> DefStrVec = TVec<TStr>();
802  StrDefaultsE.GetKeyV(DefStrVec);
803  for (i = 0; i < DefStrVec.Len(); i++) {
804  TStr attr = DefStrVec[i];
805  TVec<TStr>& StrVec = VecOfStrVecsE[KeyToIndexTypeE.GetDat(DefStrVec[i]).Val2];
806  StrVec[EdgeH.GetKeyId(EId)] = GetStrAttrDefaultE(attr);
807  }
808 
809  for (i = 0; i < VecOfFltVecsE.Len(); i++) {
810  TVec<TFlt>& FltVec = VecOfFltVecsE[i];
811  int KeyId = EdgeH.GetKeyId(EId);
812  if (FltVec.Len() > KeyId) {
813  FltVec[KeyId] = TFlt::Mn;
814  } else {
815  FltVec.Ins(KeyId, TFlt::Mn);
816  }
817  }
818  TVec<TStr> DefFltVec = TVec<TStr>();
819  FltDefaultsE.GetKeyV(DefFltVec);
820  for (i = 0; i < DefFltVec.Len(); i++) {
821  TStr attr = DefFltVec[i];
822  TVec<TFlt>& FltVec = VecOfFltVecsE[KeyToIndexTypeE.GetDat(DefFltVec[i]).Val2];
823  FltVec[EdgeH.GetKeyId(EId)] = GetFltAttrDefaultE(attr);
824  }
825  return EId;
826 }
827 
828 void TNEANet::DelEdge(const int& EId) {
829  int i;
830 
831  IAssert(IsEdge(EId));
832  const int SrcNId = GetEdge(EId).GetSrcNId();
833  const int DstNId = GetEdge(EId).GetDstNId();
834  GetNode(SrcNId).OutEIdV.DelIfIn(EId);
835  GetNode(DstNId).InEIdV.DelIfIn(EId);
836  TInt Id(EId);
837  SAttrE.DelSAttrId(Id);
838 
839  for (i = 0; i < VecOfIntVecsE.Len(); i++) {
840  TVec<TInt>& IntVec = VecOfIntVecsE[i];
841  IntVec[EdgeH.GetKeyId(EId)] = TInt::Mn;
842  }
843  for (i = 0; i < VecOfStrVecsE.Len(); i++) {
844  TVec<TStr>& StrVec = VecOfStrVecsE[i];
845  StrVec[EdgeH.GetKeyId(EId)] = TStr::GetNullStr();
846  }
847  for (i = 0; i < VecOfFltVecsE.Len(); i++) {
848  TVec<TFlt>& FltVec = VecOfFltVecsE[i];
849  FltVec[EdgeH.GetKeyId(EId)] = TFlt::Mn;
850  }
851 
852  EdgeH.DelKey(EId);
853 }
854 
855 // delete all edges between the two nodes
856 void TNEANet::DelEdge(const int& SrcNId, const int& DstNId, const bool& IsDir) {
857  int EId = 0;
858  bool Edge = IsEdge(SrcNId, DstNId, EId, IsDir);
859  IAssert(Edge); // there is at least one edge
860  while (Edge) {
861  DelEdge(EId);
862  Edge = IsEdge(SrcNId, DstNId, EId, IsDir);
863  }
864 }
865 
866 bool TNEANet::IsEdge(const int& SrcNId, const int& DstNId, int& EId, const bool& IsDir) const {
867  const TNode& SrcNode = GetNode(SrcNId);
868  for (int edge = 0; edge < SrcNode.GetOutDeg(); edge++) {
869  const TEdge& Edge = GetEdge(SrcNode.GetOutEId(edge));
870  if (DstNId == Edge.GetDstNId()) {
871  EId = Edge.GetId();
872  return true;
873  }
874  }
875  if (! IsDir) {
876  for (int edge = 0; edge < SrcNode.GetInDeg(); edge++) {
877  const TEdge& Edge = GetEdge(SrcNode.GetInEId(edge));
878  if (DstNId == Edge.GetSrcNId()) {
879  EId = Edge.GetId();
880  return true;
881  }
882  }
883  }
884  return false;
885 }
886 
887 void TNEANet::GetNIdV(TIntV& NIdV) const {
888  NIdV.Gen(GetNodes(), 0);
889  for (int N=NodeH.FFirstKeyId(); NodeH.FNextKeyId(N); ) {
890  NIdV.Add(NodeH.GetKey(N));
891  }
892 }
893 
894 void TNEANet::GetEIdV(TIntV& EIdV) const {
895  EIdV.Gen(GetEdges(), 0);
896  for (int E=EdgeH.FFirstKeyId(); EdgeH.FNextKeyId(E); ) {
897  EIdV.Add(EdgeH.GetKey(E));
898  }
899 }
900 
901 void TNEANet::Defrag(const bool& OnlyNodeLinks) {
902  for (int kid = NodeH.FFirstKeyId(); NodeH.FNextKeyId(kid); ) {
903  TNode& Node = NodeH[kid];
904  Node.InEIdV.Pack(); Node.OutEIdV.Pack();
905  }
906  if (! OnlyNodeLinks && ! NodeH.IsKeyIdEqKeyN()) { NodeH.Defrag(); }
907  if (! OnlyNodeLinks && ! EdgeH.IsKeyIdEqKeyN()) { EdgeH.Defrag(); }
908 }
909 
910 bool TNEANet::IsOk(const bool& ThrowExcept) const {
911  bool RetVal = true;
912  for (int N = NodeH.FFirstKeyId(); NodeH.FNextKeyId(N); ) {
913  const TNode& Node = NodeH[N];
914  if (! Node.OutEIdV.IsSorted()) {
915  const TStr Msg = TStr::Fmt("Out-edge list of node %d is not sorted.", Node.GetId());
916  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
917  }
918  if (! Node.InEIdV.IsSorted()) {
919  const TStr Msg = TStr::Fmt("In-edge list of node %d is not sorted.", Node.GetId());
920  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
921  }
922  // check out-edge ids
923  int prevEId = -1;
924  for (int e = 0; e < Node.GetOutDeg(); e++) {
925  if (! IsEdge(Node.GetOutEId(e))) {
926  const TStr Msg = TStr::Fmt("Out-edge id %d of node %d does not exist.", Node.GetOutEId(e), Node.GetId());
927  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
928  }
929  if (e > 0 && prevEId == Node.GetOutEId(e)) {
930  const TStr Msg = TStr::Fmt("Node %d has duplidate out-edge id %d.", Node.GetId(), Node.GetOutEId(e));
931  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
932  }
933  prevEId = Node.GetOutEId(e);
934  }
935  // check in-edge ids
936  prevEId = -1;
937  for (int e = 0; e < Node.GetInDeg(); e++) {
938  if (! IsEdge(Node.GetInEId(e))) {
939  const TStr Msg = TStr::Fmt("Out-edge id %d of node %d does not exist.", Node.GetInEId(e), Node.GetId());
940  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
941  }
942  if (e > 0 && prevEId == Node.GetInEId(e)) {
943  const TStr Msg = TStr::Fmt("Node %d has duplidate out-edge id %d.", Node.GetId(), Node.GetInEId(e));
944  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
945  }
946  prevEId = Node.GetInEId(e);
947  }
948  }
949  for (int E = EdgeH.FFirstKeyId(); EdgeH.FNextKeyId(E); ) {
950  const TEdge& Edge = EdgeH[E];
951  if (! IsNode(Edge.GetSrcNId())) {
952  const TStr Msg = TStr::Fmt("Edge %d source node %d does not exist.", Edge.GetId(), Edge.GetSrcNId());
953  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
954  }
955  if (! IsNode(Edge.GetDstNId())) {
956  const TStr Msg = TStr::Fmt("Edge %d destination node %d does not exist.", Edge.GetId(), Edge.GetDstNId());
957  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
958  }
959  }
960  return RetVal;
961 }
962 
963 void TNEANet::Dump(FILE *OutF) const {
964  const int NodePlaces = (int) ceil(log10((double) GetNodes()));
965  const int EdgePlaces = (int) ceil(log10((double) GetEdges()));
966  fprintf(OutF, "-------------------------------------------------\nDirected Node-Edge Network with Attributes: nodes: %d, edges: %d\n", GetNodes(), GetEdges());
967  for (TNodeI NodeI = BegNI(); NodeI < EndNI(); NodeI++) {
968  fprintf(OutF, " %*d:", NodePlaces, NodeI.GetId());
969  fprintf(OutF, " in[%d]", NodeI.GetInDeg());
970  for (int edge = 0; edge < NodeI.GetInDeg(); edge++) {
971  fprintf(OutF, " %*d", EdgePlaces, NodeI.GetInEId(edge)); }
972  //fprintf(OutF, "\n");
973  fprintf(OutF, " out[%d]", NodeI.GetOutDeg());
974  for (int edge = 0; edge < NodeI.GetOutDeg(); edge++) {
975  fprintf(OutF, " %*d", EdgePlaces, NodeI.GetOutEId(edge)); }
976  //fprintf(OutF, "\n");
977 
978  // load node attributes
979  TIntV IntAttrN;
980  IntAttrValueNI(NodeI.GetId(), IntAttrN);
981  fprintf(OutF, " nai[%d]", IntAttrN.Len());
982  for (int i = 0; i < IntAttrN.Len(); i++) {
983  fprintf(OutF, " %*i", NodePlaces, IntAttrN[i]()); }
984  //fprintf(OutF, "\n");
985 
986  TStrV StrAttrN;
987  StrAttrValueNI(NodeI.GetId(), StrAttrN);
988  fprintf(OutF, " nas[%d]", StrAttrN.Len());
989  for (int i = 0; i < StrAttrN.Len(); i++) {
990  fprintf(OutF, " %*s", NodePlaces, StrAttrN[i]()); }
991  //fprintf(OutF, "\n");
992 
993  TFltV FltAttrN;
994  FltAttrValueNI(NodeI.GetId(), FltAttrN);
995  fprintf(OutF, " naf[%d]", FltAttrN.Len());
996  for (int i = 0; i < FltAttrN.Len(); i++) {
997  fprintf(OutF, " %*f", NodePlaces, FltAttrN[i]()); }
998  //fprintf(OutF, "\n");
999  fprintf(OutF, "\n");
1000  }
1001  for (TEdgeI EdgeI = BegEI(); EdgeI < EndEI(); EdgeI++) {
1002  fprintf(OutF, " %*d: %*d -> %*d", EdgePlaces, EdgeI.GetId(), NodePlaces, EdgeI.GetSrcNId(), NodePlaces, EdgeI.GetDstNId());
1003 
1004  // load edge attributes
1005  TIntV IntAttrE;
1006  IntAttrValueEI(EdgeI.GetId(), IntAttrE);
1007  fprintf(OutF, " eai[%d]", IntAttrE.Len());
1008  for (int i = 0; i < IntAttrE.Len(); i++) {
1009  fprintf(OutF, " %*i", EdgePlaces, IntAttrE[i]());
1010  }
1011 
1012  TStrV StrAttrE;
1013  StrAttrValueEI(EdgeI.GetId(), StrAttrE);
1014  fprintf(OutF, " eas[%d]", StrAttrE.Len());
1015  for (int i = 0; i < StrAttrE.Len(); i++) {
1016  fprintf(OutF, " %*s", EdgePlaces, StrAttrE[i]());
1017  }
1018  TFltV FltAttrE;
1019  FltAttrValueEI(EdgeI.GetId(), FltAttrE);
1020  fprintf(OutF, " eaf[%d]", FltAttrE.Len());
1021  for (int i = 0; i < FltAttrE.Len(); i++) {
1022  fprintf(OutF, " %*f", EdgePlaces, FltAttrE[i]());
1023  }
1024  fprintf(OutF, "\n");
1025  }
1026  //fprintf(OutF, "\n");
1027 }
1028 
1029 // Attribute related function
1030 
1031 int TNEANet::AddIntAttrDatN(const int& NId, const TInt& value, const TStr& attr) {
1032  int i;
1033  TInt CurrLen;
1034  if (!IsNode(NId)) {
1035  // AddNode(NId);
1036  return -1;
1037  }
1038  if (KeyToIndexTypeN.IsKey(attr)) {
1040  NewVec[NodeH.GetKeyId(NId)] = value;
1041  } else {
1042  CurrLen = VecOfIntVecsN.Len();
1043  KeyToIndexTypeN.AddDat(attr, TIntPr(IntType, CurrLen));
1044  TVec<TInt> NewVec = TVec<TInt>();
1045  for (i = 0; i < MxNId; i++) {
1046  NewVec.Ins(i, GetIntAttrDefaultN(attr));
1047  }
1048  NewVec[NodeH.GetKeyId(NId)] = value;
1049  VecOfIntVecsN.Add(NewVec);
1050  }
1051  return 0;
1052 }
1053 
1054 int TNEANet::AddIntVAttrDatN(const int& NId, const TIntV& value, const TStr& attr, TBool UseDense) {
1055  if (!IsNode(NId)) {
1056  // AddNode(NId);
1057  return -1;
1058  }
1059  TInt location = CheckDenseOrSparseN(attr);
1060  if (location==-1) {
1061  AddIntVAttrN(attr, UseDense);
1062  location = CheckDenseOrSparseN(attr);
1063  }
1064  if (UseDense) {
1065  IAssertR(location != 0, TStr::Fmt("NodeId %d exists for %s in sparse representation", NId, attr.CStr()));
1067  NewVec[NodeH.GetKeyId(NId)] = value;
1068  } else {
1069  IAssertR(location != 1, TStr::Fmt("NodeId %d exists for %s in dense representation", NId, attr.CStr()));
1071  NewHash.AddDat(NodeH.GetKeyId(NId), value);
1072  }
1073 
1074  return 0;
1075 }
1076 
1077 int TNEANet::AddFltVAttrDatN(const int& NId, const TFltV& value, const TStr& attr, TBool UseDense) {
1078  if (!IsNode(NId)) {
1079  // AddNode(NId);
1080  return -1;
1081  }
1082 
1083  TInt location = CheckDenseOrSparseN(attr);
1084  if (location==-1) {
1085  AddFltVAttrN(attr, UseDense);
1086  location = CheckDenseOrSparseN(attr);
1087  }
1088 
1089  if (UseDense) {
1090  IAssertR(location != 0, TStr::Fmt("NodeId %d exists for %s in sparse representation", NId, attr.CStr()));
1092  NewVec[NodeH.GetKeyId(NId)] = value;
1093  } else {
1094  IAssertR(location != 1, TStr::Fmt("NodeId %d exists for %s in dense representation", NId, attr.CStr()));
1096  NewHash.AddDat(NodeH.GetKeyId(NId), value);
1097  }
1098 
1099  return 0;
1100 }
1101 
1102 int TNEANet::AppendIntVAttrDatN(const int& NId, const TInt& value, const TStr& attr, TBool UseDense) {
1103  if (!IsNode(NId)) {
1104  // AddNode(NId);
1105  return -1;
1106  }
1107  TInt location = CheckDenseOrSparseN(attr);
1108  if (location==-1) {
1109  AddIntVAttrN(attr, UseDense);
1110  location = CheckDenseOrSparseN(attr);
1111  }
1112  if (UseDense) {
1113  IAssertR(location != 0, TStr::Fmt("NodeId %d exists for %s in sparse representation", NId, attr.CStr()));
1115  NewVec[NodeH.GetKeyId(NId)].Add(value);
1116  } else {
1117  IAssertR(location != 1, TStr::Fmt("NodeId %d exists for %s in dense representation", NId, attr.CStr()));
1119  NewHash[NodeH.GetKeyId(NId)].Add(value);
1120  }
1121  return 0;
1122 }
1123 
1124 
1125 int TNEANet::AppendFltVAttrDatN(const int& NId, const TFlt& value, const TStr& attr, TBool UseDense) {
1126  if (!IsNode(NId)) {
1127  // AddNode(NId);
1128  return -1;
1129  }
1130 
1131  TInt location = CheckDenseOrSparseN(attr);
1132 
1133 
1134  if (location==-1) {
1135  AddFltVAttrN(attr, UseDense);
1136  location = CheckDenseOrSparseN(attr);
1137  }
1138  if (UseDense) {
1139  IAssertR(location != 0, TStr::Fmt("NodeId %d exists for %s in sparse representation", NId, attr.CStr()));
1141  NewVec[NodeH.GetKeyId(NId)].Add(value);
1142  } else {
1143  IAssertR(location != 1, TStr::Fmt("NodeId %d exists for %s in dense representation", NId, attr.CStr()));
1145  NewHash[NodeH.GetKeyId(NId)].Add(value);
1146  }
1147  return 0;
1148 }
1149 
1150 int TNEANet::DelFromIntVAttrDatN(const int& NId, const TInt& value, const TStr& attr) {
1151  TInt CurrLen;
1152  if (!IsNode(NId)) {
1153  // AddNode(NId);
1154  return -1;
1155  }
1156  TInt location = CheckDenseOrSparseN(attr);
1157  if (location != -1) {
1158  if (location == 1) {
1160  if (!NewVec[NodeH.GetKeyId(NId)].DelIfIn(value)) {
1161  return -1;
1162  }
1163  } else {
1165  if (!NewHash[NodeH.GetKeyId(NId)].DelIfIn(value)) {
1166  return -1;
1167  }
1168  }
1169  } else {
1170  return -1;
1171  }
1172  return 0;
1173 }
1174 
1175 
1176 int TNEANet::DelFromFltVAttrDatN(const int& NId, const TFlt& value, const TStr& attr) {
1177  TInt CurrLen;
1178  if (!IsNode(NId)) {
1179  // AddNode(NId);
1180  return -1;
1181  }
1182  TInt location = CheckDenseOrSparseN(attr);
1183  if (location != -1) {
1184  if (location == 1) {
1186  if (!NewVec[NodeH.GetKeyId(NId)].DelIfIn(value)) {
1187  return -1;
1188  }
1189  } else {
1191  if (!NewHash[NodeH.GetKeyId(NId)].DelIfIn(value)) {
1192  return -1;
1193  }
1194  }
1195  } else {
1196  return -1;
1197  }
1198  return 0;
1199 }
1200 
1201 int TNEANet::AddStrAttrDatN(const int& NId, const TStr& value, const TStr& attr) {
1202  int i;
1203  TInt CurrLen;
1204  if (!IsNode(NId)) {
1205  // AddNode(NId);
1206  return -1;
1207  }
1208  if (KeyToIndexTypeN.IsKey(attr)) {
1210  NewVec[NodeH.GetKeyId(NId)] = value;
1211  } else {
1212  CurrLen = VecOfStrVecsN.Len();
1213  KeyToIndexTypeN.AddDat(attr, TIntPr(StrType, CurrLen));
1214  TVec<TStr> NewVec = TVec<TStr>();
1215  for (i = 0; i < MxNId; i++) {
1216  NewVec.Ins(i, GetStrAttrDefaultN(attr));
1217  }
1218  NewVec[NodeH.GetKeyId(NId)] = value;
1219  VecOfStrVecsN.Add(NewVec);
1220  }
1221  return 0;
1222 }
1223 
1224 int TNEANet::AddFltAttrDatN(const int& NId, const TFlt& value, const TStr& attr) {
1225  int i;
1226  TInt CurrLen;
1227 
1228  if (!IsNode(NId)) {
1229  // AddNode(NId);
1230  return -1;
1231  }
1232  if (KeyToIndexTypeN.IsKey(attr)) {
1234  NewVec[NodeH.GetKeyId(NId)] = value;
1235  } else {
1236  CurrLen = VecOfFltVecsN.Len();
1237  KeyToIndexTypeN.AddDat(attr, TIntPr(FltType, CurrLen));
1238  TVec<TFlt> NewVec = TVec<TFlt>();
1239  for (i = 0; i < MxNId; i++) {
1240  NewVec.Ins(i, GetFltAttrDefaultN(attr));
1241  }
1242  NewVec[NodeH.GetKeyId(NId)] = value;
1243  VecOfFltVecsN.Add(NewVec);
1244  }
1245  return 0;
1246 }
1247 
1248 int TNEANet::AddIntAttrDatE(const int& EId, const TInt& value, const TStr& attr) {
1249  int i;
1250  TInt CurrLen;
1251  if (!IsEdge(EId)) {
1252  //AddEdge(EId);
1253  return -1;
1254  }
1255  if (KeyToIndexTypeE.IsKey(attr)) {
1257  NewVec[EdgeH.GetKeyId(EId)] = value;
1258  } else {
1259  CurrLen = VecOfIntVecsE.Len();
1260  KeyToIndexTypeE.AddDat(attr, TIntPr(IntType, CurrLen));
1261  TVec<TInt> NewVec = TVec<TInt>();
1262  for (i = 0; i < MxEId; i++) {
1263  NewVec.Ins(i, GetIntAttrDefaultE(attr));
1264  }
1265  NewVec[EdgeH.GetKeyId(EId)] = value;
1266  VecOfIntVecsE.Add(NewVec);
1267  }
1268  return 0;
1269 }
1270 
1271 int TNEANet::AddIntVAttrDatE(const int& EId, const TIntV& value, const TStr& attr, TBool UseDense) {
1272  if (!IsEdge(EId)) {
1273  // AddNode(NId);
1274  return -1;
1275  }
1276  TInt location = CheckDenseOrSparseE(attr);
1277  if (location==-1) {
1278  AddIntVAttrE(attr, UseDense);
1279  location = CheckDenseOrSparseE(attr);
1280  }
1281  if (UseDense) {
1282  IAssertR(location != 0, TStr::Fmt("EdgeID %d exists for %s in sparse representation", EId, attr.CStr()));
1284  NewVec[EdgeH.GetKeyId(EId)] = value;
1285  } else {
1286  IAssertR(location != 1, TStr::Fmt("NodeId %d exists for %s in dense representation", EId, attr.CStr()));
1288  NewHash.AddDat(EdgeH.GetKeyId(EId), value);
1289  }
1290  return 0;
1291 }
1292 
1293 
1294 int TNEANet::AddFltVAttrDatE(const int& EId, const TFltV& value, const TStr& attr, TBool UseDense) {
1295  if (!IsEdge(EId)) {
1296  // AddNode(NId);
1297  return -1;
1298  }
1299  TInt location = CheckDenseOrSparseE(attr);
1300 
1301  if (location==-1) {
1302  AddFltVAttrE(attr, UseDense);
1303  location = CheckDenseOrSparseE(attr);
1304  }
1305  if (UseDense) {
1306  IAssertR(location != 0, TStr::Fmt("EdgeID %d exists for %s in sparse representation", EId, attr.CStr()));
1308  NewVec[EdgeH.GetKeyId(EId)] = value;
1309  } else {
1310  IAssertR(location != 1, TStr::Fmt("NodeId %d exists for %s in dense representation", EId, attr.CStr()));
1312  NewHash.AddDat(EdgeH.GetKeyId(EId), value);
1313  }
1314  return 0;
1315 }
1316 
1317 int TNEANet::AppendIntVAttrDatE(const int& EId, const TInt& value, const TStr& attr, TBool UseDense) {
1318  if (!IsEdge(EId)) {
1319  // AddNode(NId);
1320  return -1;
1321  }
1322  TInt location = CheckDenseOrSparseE(attr);
1323  if (location==-1) return -1;
1324  if (UseDense) {
1325  IAssertR(location != 0, TStr::Fmt("Edge %d exists for %s in sparse representation", EId, attr.CStr()));
1327  NewVec[EdgeH.GetKeyId(EId)].Add(value);
1328  } else {
1329  IAssertR(location != 1, TStr::Fmt("Edge %d exists for %s in dense representation", EId, attr.CStr()));
1331  NewHash[EdgeH.GetKeyId(EId)].Add(value);
1332  }
1333  return 0;
1334 }
1335 
1336 
1337 int TNEANet::AppendFltVAttrDatE(const int& EId, const TFlt& value, const TStr& attr, TBool UseDense) {
1338  if (!IsEdge(EId)) {
1339  // AddNode(NId);
1340  return -1;
1341  }
1342  TInt location = CheckDenseOrSparseE(attr);
1343  if (location==-1) return -1;
1344  if (UseDense) {
1345  IAssertR(location != 0, TStr::Fmt("Edge %d exists for %s in sparse representation", EId, attr.CStr()));
1347  NewVec[EdgeH.GetKeyId(EId)].Add(value);
1348  } else {
1349  IAssertR(location != 1, TStr::Fmt("Edge %d exists for %s in dense representation", EId, attr.CStr()));
1351  NewHash[EdgeH.GetKeyId(EId)].Add(value);
1352  }
1353  return 0;
1354 }
1355 
1356 int TNEANet::AddStrAttrDatE(const int& EId, const TStr& value, const TStr& attr) {
1357  int i;
1358  TInt CurrLen;
1359  if (!IsEdge(EId)) {
1360  //AddEdge(EId);
1361  return -1;
1362  }
1363  if (KeyToIndexTypeE.IsKey(attr)) {
1365  NewVec[EdgeH.GetKeyId(EId)] = value;
1366  } else {
1367  CurrLen = VecOfStrVecsE.Len();
1368  KeyToIndexTypeE.AddDat(attr, TIntPr(StrType, CurrLen));
1369  TVec<TStr> NewVec = TVec<TStr>();
1370  for (i = 0; i < MxEId; i++) {
1371  NewVec.Ins(i, GetStrAttrDefaultE(attr));
1372  }
1373  NewVec[EdgeH.GetKeyId(EId)] = value;
1374  VecOfStrVecsE.Add(NewVec);
1375  }
1376  return 0;
1377 }
1378 
1379 int TNEANet::AddFltAttrDatE(const int& EId, const TFlt& value, const TStr& attr) {
1380  int i;
1381  TInt CurrLen;
1382 
1383  if (!IsEdge(EId)) {
1384  //AddEdge(EId);
1385  return -1;
1386  }
1387  if (KeyToIndexTypeE.IsKey(attr)) {
1389  NewVec[EdgeH.GetKeyId(EId)] = value;
1390  } else {
1391  CurrLen = VecOfFltVecsE.Len();
1392  KeyToIndexTypeE.AddDat(attr, TIntPr(FltType, CurrLen));
1393  TVec<TFlt> NewVec = TVec<TFlt>();
1394  for (i = 0; i < MxEId; i++) {
1395  NewVec.Ins(i, GetFltAttrDefaultE(attr));
1396  }
1397  NewVec[EdgeH.GetKeyId(EId)] = value;
1398  VecOfFltVecsE.Add(NewVec);
1399  }
1400  return 0;
1401 }
1402 
1404  return VecOfFltVecsE[KeyToIndexTypeE.GetDat(attr).Val2];
1405 }
1406 
1407 int TNEANet::GetFltKeyIdE(const int& EId) {
1408  return EdgeH.GetKeyId(EId);
1409 }
1410 
1411 TInt TNEANet::GetIntAttrDatN(const int& NId, const TStr& attr) {
1412  return VecOfIntVecsN[KeyToIndexTypeN.GetDat(attr).Val2][NodeH.GetKeyId(NId)];
1413 }
1414 
1415 TIntV TNEANet::GetIntVAttrDatN(const int& NId, const TStr& attr) const {
1416  TInt location = CheckDenseOrSparseN(attr);
1417  if (location != 0) return VecOfIntVecVecsN[KeyToIndexTypeN.GetDat(attr).Val2][NodeH.GetKeyId(NId)];
1418  else return VecOfIntHashVecsN[KeyToIndexTypeN.GetDat(attr).Val2][NodeH.GetKeyId(NId)];
1419 }
1420 
1421 
1422 TFltV TNEANet::GetFltVAttrDatN(const int& NId, const TStr& attr) const {
1423  TInt location = CheckDenseOrSparseN(attr);
1424  if (location != 0) return VecOfFltVecVecsN[KeyToIndexTypeN.GetDat(attr).Val2][NodeH.GetKeyId(NId)];
1425  else return VecOfFltHashVecsN[KeyToIndexTypeN.GetDat(attr).Val2][NodeH.GetKeyId(NId)];
1426 }
1427 
1428 TStr TNEANet::GetStrAttrDatN(const int& NId, const TStr& attr) {
1429  return VecOfStrVecsN[KeyToIndexTypeN.GetDat(attr).Val2][NodeH.GetKeyId(NId)];
1430 }
1431 
1432 TFlt TNEANet::GetFltAttrDatN(const int& NId, const TStr& attr) {
1433  return VecOfFltVecsN[KeyToIndexTypeN.GetDat(attr).Val2][NodeH.GetKeyId(NId)];
1434 }
1435 
1436 TInt TNEANet::GetIntAttrIndDatN(const int& NId, const int& index) {
1437  return VecOfIntVecsN[index][NodeH.GetKeyId(NId)];
1438 }
1439 
1440 TStr TNEANet::GetStrAttrIndDatN(const int& NId, const int& index) {
1441  return VecOfStrVecsN[index][NodeH.GetKeyId(NId)];
1442 }
1443 
1444 TFlt TNEANet::GetFltAttrIndDatN(const int& NId, const int& index) {
1445  return VecOfFltVecsN[index][NodeH.GetKeyId(NId)];
1446 }
1447 
1448 int TNEANet::GetIntAttrIndN(const TStr& attr) {
1449  return KeyToIndexTypeN.GetDat(attr).Val2.Val;
1450 }
1451 
1452 int TNEANet::GetAttrIndN(const TStr& attr) {
1453  return KeyToIndexTypeN.GetDat(attr).Val2.Val;
1454 }
1455 
1456 TInt TNEANet::GetIntAttrDatE(const int& EId, const TStr& attr) {
1457  return VecOfIntVecsE[KeyToIndexTypeE.GetDat(attr).Val2][EdgeH.GetKeyId(EId)];
1458 }
1459 
1460 TIntV TNEANet::GetIntVAttrDatE(const int& EId, const TStr& attr) {
1461  TInt location = CheckDenseOrSparseE(attr);
1462  if (location != 0) return VecOfIntVecVecsE[KeyToIndexTypeE.GetDat(attr).Val2][EdgeH.GetKeyId(EId)];
1463  else return VecOfIntHashVecsE[KeyToIndexTypeE.GetDat(attr).Val2][EdgeH.GetKeyId(EId)];
1464 }
1465 
1466 
1467 TFltV TNEANet::GetFltVAttrDatE(const int& EId, const TStr& attr) {
1468  TInt location = CheckDenseOrSparseE(attr);
1469  if (location != 0) return VecOfFltVecVecsE[KeyToIndexTypeE.GetDat(attr).Val2][EdgeH.GetKeyId(EId)];
1470  else return VecOfFltHashVecsE[KeyToIndexTypeE.GetDat(attr).Val2][EdgeH.GetKeyId(EId)];
1471 }
1472 
1473 TStr TNEANet::GetStrAttrDatE(const int& EId, const TStr& attr) {
1474  return VecOfStrVecsE[KeyToIndexTypeE.GetDat(attr).Val2][EdgeH.GetKeyId(EId)];
1475 }
1476 
1477 TFlt TNEANet::GetFltAttrDatE(const int& EId, const TStr& attr) {
1478  return VecOfFltVecsE[KeyToIndexTypeE.GetDat(attr).Val2][EdgeH.GetKeyId(EId)];
1479 }
1480 
1481 TInt TNEANet::GetIntAttrIndDatE(const int& EId, const int& index) {
1482  return VecOfIntVecsE[index][EdgeH.GetKeyId(EId)];
1483 }
1484 
1485 TStr TNEANet::GetStrAttrIndDatE(const int& EId, const int& index) {
1486  return VecOfStrVecsE[index][EdgeH.GetKeyId(EId)];
1487 }
1488 
1489 TFlt TNEANet::GetFltAttrIndDatE(const int& EId, const int& index) {
1490  return VecOfFltVecsE[index][EdgeH.GetKeyId(EId)];
1491 }
1492 
1493 int TNEANet::GetIntAttrIndE(const TStr& attr) {
1494  return KeyToIndexTypeE.GetDat(attr).Val2.Val;
1495 }
1496 
1497 int TNEANet::GetAttrIndE(const TStr& attr) {
1498  return KeyToIndexTypeE.GetDat(attr).Val2.Val;
1499 }
1500 
1501 
1502 int TNEANet::DelAttrDatN(const int& NId, const TStr& attr) {
1503  TInt vecType = KeyToIndexTypeN(attr).Val1;
1504  if (vecType == IntType) {
1506  } else if (vecType == StrType) {
1508  } else if (vecType == FltType) {
1510  } else if (vecType ==IntVType) {
1511  TInt location = CheckDenseOrSparseN(attr);
1512  if (location == 0) VecOfIntHashVecsN[KeyToIndexTypeN.GetDat(attr).Val2][NodeH.GetKeyId(NId)] = TIntV();
1514  } else if (vecType == FltVType) { // added by Avery
1515  TInt location = CheckDenseOrSparseN(attr);
1516  if (location == 0) VecOfFltHashVecsN[KeyToIndexTypeN.GetDat(attr).Val2][NodeH.GetKeyId(NId)] = TFltV();
1518  } else {
1519  return -1;
1520  }
1521  return 0;
1522 }
1523 
1524 int TNEANet::DelAttrDatE(const int& EId, const TStr& attr) {
1525  // TODO(nkhadke): add error checking
1526  TInt vecType = KeyToIndexTypeE(attr).Val1;
1527  if (vecType == IntType) {
1529  } else if (vecType == StrType) {
1531  } else if (vecType == FltType) {
1533  } else if (vecType == IntVType) {
1534  TInt location = CheckDenseOrSparseE(attr);
1535  if (location == 0) VecOfIntHashVecsE[KeyToIndexTypeE.GetDat(attr).Val2][EdgeH.GetKeyId(EId)] = TIntV();
1537  } else if (vecType == FltVType) {
1538  TInt location = CheckDenseOrSparseE(attr);
1539  if (location == 0) VecOfFltHashVecsE[KeyToIndexTypeE.GetDat(attr).Val2][EdgeH.GetKeyId(EId)] = TFltV();
1541  } else {
1542  return -1;
1543  }
1544  return 0;
1545 }
1546 
1547 int TNEANet::AddIntAttrN(const TStr& attr, TInt defaultValue){
1548  int i;
1549  TInt CurrLen;
1550  TVec<TInt> NewVec;
1551  CurrLen = VecOfIntVecsN.Len();
1552  KeyToIndexTypeN.AddDat(attr, TIntPr(IntType, CurrLen));
1553  NewVec = TVec<TInt>();
1554  for (i = 0; i < MxNId; i++) {
1555  NewVec.Ins(i, defaultValue);
1556  }
1557  VecOfIntVecsN.Add(NewVec);
1558  if (!IntDefaultsN.IsKey(attr)) {
1559  IntDefaultsN.AddDat(attr, defaultValue);
1560  } else {
1561  return -1;
1562  }
1563  return 0;
1564 }
1565 
1566 int TNEANet::AddIntVAttrN(const TStr& attr, TBool UseDense){
1567  TInt CurrLen;
1568  if (UseDense) {
1569  CurrLen = VecOfIntVecVecsN.Len();
1570  KeyToIndexTypeN.AddDat(attr, TIntPr(IntVType, CurrLen));
1571  KeyToDenseN.AddDat(attr, true);
1572  TVec<TIntV> NewVec = TVec<TIntV>(MxNId);
1573  VecOfIntVecVecsN.Add(NewVec);
1574  } else {
1575  CurrLen = VecOfIntHashVecsN.Len();
1576  KeyToIndexTypeN.AddDat(attr, TIntPr(IntVType, CurrLen));
1577  KeyToDenseN.AddDat(attr, false);
1578  THash<TInt, TIntV> NewHash;
1579  VecOfIntHashVecsN.Add(NewHash);
1580  }
1581  return 0;
1582 }
1583 
1584 int TNEANet::AddFltVAttrN(const TStr& attr, TBool UseDense){
1585  TInt CurrLen;
1586  if (UseDense) {
1587  CurrLen = VecOfFltVecVecsN.Len();
1588  KeyToIndexTypeN.AddDat(attr, TIntPr(FltVType, CurrLen));
1589  KeyToDenseN.AddDat(attr, true);
1590  TVec<TFltV> NewVec = TVec<TFltV>(MxNId);
1591  VecOfFltVecVecsN.Add(NewVec);
1592  } else {
1593  CurrLen = VecOfFltHashVecsN.Len();
1594  KeyToIndexTypeN.AddDat(attr, TIntPr(FltVType, CurrLen));
1595  KeyToDenseN.AddDat(attr, false);
1596  THash<TInt, TFltV> NewHash;
1597  VecOfFltHashVecsN.Add(NewHash);
1598  }
1599  return 0;
1600 }
1601 
1602 int TNEANet::AddStrAttrN(const TStr& attr, TStr defaultValue) {
1603  int i;
1604  TInt CurrLen;
1605  TVec<TStr> NewVec;
1606  CurrLen = VecOfStrVecsN.Len();
1607  KeyToIndexTypeN.AddDat(attr, TIntPr(StrType, CurrLen));
1608  NewVec = TVec<TStr>();
1609  for (i = 0; i < MxNId; i++) {
1610  NewVec.Ins(i, defaultValue);
1611  }
1612  VecOfStrVecsN.Add(NewVec);
1613  if (!StrDefaultsN.IsKey(attr)) {
1614  StrDefaultsN.AddDat(attr, defaultValue);
1615  } else {
1616  return -1;
1617  }
1618  return 0;
1619 }
1620 
1621 int TNEANet::AddFltAttrN(const TStr& attr, TFlt defaultValue) {
1622  // TODO(nkhadke): add error checking
1623  int i;
1624  TInt CurrLen;
1625  TVec<TFlt> NewVec;
1626  CurrLen = VecOfFltVecsN.Len();
1627  KeyToIndexTypeN.AddDat(attr, TIntPr(FltType, CurrLen));
1628  NewVec = TVec<TFlt>();
1629  for (i = 0; i < MxNId; i++) {
1630  NewVec.Ins(i, defaultValue);
1631  }
1632  VecOfFltVecsN.Add(NewVec);
1633  if (!FltDefaultsN.IsKey(attr)) {
1634  FltDefaultsN.AddDat(attr, defaultValue);
1635  } else {
1636  return -1;
1637  }
1638  return 0;
1639 }
1640 
1641 int TNEANet::AddIntAttrE(const TStr& attr, TInt defaultValue){
1642  // TODO(nkhadke): add error checking
1643  int i;
1644  TInt CurrLen;
1645  TVec<TInt> NewVec;
1646  CurrLen = VecOfIntVecsE.Len();
1647  KeyToIndexTypeE.AddDat(attr, TIntPr(IntType, CurrLen));
1648  NewVec = TVec<TInt>();
1649  for (i = 0; i < MxEId; i++) {
1650  NewVec.Ins(i, defaultValue);
1651  }
1652  VecOfIntVecsE.Add(NewVec);
1653  if (!IntDefaultsE.IsKey(attr)) {
1654  IntDefaultsE.AddDat(attr, defaultValue);
1655  } else {
1656  return -1;
1657  }
1658  return 0;
1659 }
1660 
1661 int TNEANet::AddIntVAttrE(const TStr& attr, TBool UseDense){
1662  TInt CurrLen;
1663  if (UseDense) {
1664  CurrLen = VecOfIntVecVecsE.Len();
1665  KeyToIndexTypeE.AddDat(attr, TIntPr(IntVType, CurrLen));
1666  KeyToDenseE.AddDat(attr, true);
1667  TVec<TIntV> NewVec = TVec<TIntV>(MxEId);
1668  VecOfIntVecVecsE.Add(NewVec);
1669  } else {
1670  CurrLen = VecOfIntHashVecsE.Len();
1671  KeyToIndexTypeE.AddDat(attr, TIntPr(IntVType, CurrLen));
1672  KeyToDenseE.AddDat(attr, false);
1673  THash<TInt, TIntV> NewHash;
1674  VecOfIntHashVecsE.Add(NewHash);
1675  }
1676  return 0;
1677 }
1678 
1679 int TNEANet::AddFltVAttrE(const TStr& attr, TBool UseDense){
1680  TInt CurrLen;
1681  if (UseDense) {
1682  CurrLen = VecOfFltVecVecsE.Len();
1683  KeyToIndexTypeE.AddDat(attr, TIntPr(FltVType, CurrLen));
1684  KeyToDenseE.AddDat(attr, true);
1685  TVec<TFltV> NewVec = TVec<TFltV>(MxEId);
1686  VecOfFltVecVecsE.Add(NewVec);
1687  } else {
1688  CurrLen = VecOfFltHashVecsE.Len();
1689  KeyToIndexTypeE.AddDat(attr, TIntPr(FltVType, CurrLen));
1690  KeyToDenseE.AddDat(attr, false);
1691  THash<TInt, TFltV> NewHash;
1692  VecOfFltHashVecsE.Add(NewHash);
1693  }
1694  return 0;
1695 }
1696 
1697 int TNEANet::AddStrAttrE(const TStr& attr, TStr defaultValue) {
1698  int i;
1699  TInt CurrLen;
1700  TVec<TStr> NewVec;
1701  CurrLen = VecOfStrVecsE.Len();
1702  KeyToIndexTypeE.AddDat(attr, TIntPr(StrType, CurrLen));
1703  NewVec = TVec<TStr>();
1704  for (i = 0; i < MxEId; i++) {
1705  NewVec.Ins(i, defaultValue);
1706  }
1707  VecOfStrVecsE.Add(NewVec);
1708  if (!StrDefaultsE.IsKey(attr)) {
1709  StrDefaultsE.AddDat(attr, defaultValue);
1710  } else {
1711  return -1;
1712  }
1713  return 0;
1714 }
1715 
1716 int TNEANet::AddFltAttrE(const TStr& attr, TFlt defaultValue) {
1717  int i;
1718  TInt CurrLen;
1719  TVec<TFlt> NewVec;
1720  CurrLen = VecOfFltVecsE.Len();
1721  KeyToIndexTypeE.AddDat(attr, TIntPr(FltType, CurrLen));
1722  NewVec = TVec<TFlt>();
1723  for (i = 0; i < MxEId; i++) {
1724  NewVec.Ins(i, defaultValue);
1725  }
1726  VecOfFltVecsE.Add(NewVec);
1727  if (!FltDefaultsE.IsKey(attr)) {
1728  FltDefaultsE.AddDat(attr, defaultValue);
1729  } else {
1730  return -1;
1731  }
1732  return 0;
1733 }
1734 
1735 int TNEANet::DelAttrN(const TStr& attr) {
1736  TInt vecType = KeyToIndexTypeN(attr).Val1;
1737  if (vecType == IntType) {
1739  if (IntDefaultsN.IsKey(attr)) {
1740  IntDefaultsN.DelKey(attr);
1741  }
1742  } else if (vecType == StrType) {
1744  if (StrDefaultsN.IsKey(attr)) {
1745  StrDefaultsN.DelKey(attr);
1746  }
1747  } else if (vecType == FltType) {
1749  if (FltDefaultsN.IsKey(attr)) {
1750  FltDefaultsN.DelKey(attr);
1751  }
1752  } else if (vecType == IntVType) {
1753  TInt location = CheckDenseOrSparseN(attr);
1754  if (location == 1) VecOfIntVecVecsN[KeyToIndexTypeN.GetDat(attr).Val2] = TVec<TIntV>();
1756  KeyToDenseN.DelKey(attr);
1757  } else if (vecType == FltVType) {
1758  TInt location = CheckDenseOrSparseN(attr);
1759  if (location == 1) VecOfFltVecVecsN[KeyToIndexTypeN.GetDat(attr).Val2] = TVec<TFltV>();
1761  KeyToDenseN.DelKey(attr);
1762  } else {
1763  return -1;
1764  }
1765  KeyToIndexTypeN.DelKey(attr);
1766  return 0;
1767 }
1768 
1769 int TNEANet::DelAttrE(const TStr& attr) {
1770  TInt vecType = KeyToIndexTypeE(attr).Val1;
1771  if (vecType == IntType) {
1773  if (IntDefaultsE.IsKey(attr)) {
1774  IntDefaultsE.DelKey(attr);
1775  }
1776  } else if (vecType == StrType) {
1778  if (StrDefaultsE.IsKey(attr)) {
1779  StrDefaultsE.DelKey(attr);
1780  }
1781  } else if (vecType == FltType) {
1783  if (FltDefaultsE.IsKey(attr)) {
1784  FltDefaultsE.DelKey(attr);
1785  }
1786  } else {
1787  return -1;
1788  }
1789  KeyToIndexTypeE.DelKey(attr);
1790  return 0;
1791 }
1792 
1793 // Return a small graph on 5 nodes and 6 edges.
1795  PNEANet Net = TNEANet::New();
1796  for (int i = 0; i < 5; i++) { Net->AddNode(i); }
1797  Net->AddEdge(0,1); Net->AddEdge(0,2);
1798  Net->AddEdge(0,3); Net->AddEdge(0,4);
1799  Net->AddEdge(1,2); Net->AddEdge(1,2);
1800  return Net;
1801 }
1802 
1803 void TNEANet::GetAttrNNames(TStrV& IntAttrNames, TStrV& FltAttrNames, TStrV& StrAttrNames) const {
1804  for (TStrIntPrH::TIter it = KeyToIndexTypeN.BegI(); it < KeyToIndexTypeN.EndI(); it++) {
1805  if (it.GetDat().GetVal1() == IntType) {
1806  IntAttrNames.Add(it.GetKey());
1807  }
1808  if (it.GetDat().GetVal1() == FltType) {
1809  FltAttrNames.Add(it.GetKey());
1810  }
1811  if (it.GetDat().GetVal1() == StrType) {
1812  StrAttrNames.Add(it.GetKey());
1813  }
1814  }
1815 }
1816 
1817 void TNEANet::GetAttrENames(TStrV& IntAttrNames, TStrV& FltAttrNames, TStrV& StrAttrNames) const {
1818  for (TStrIntPrH::TIter it = KeyToIndexTypeE.BegI(); it < KeyToIndexTypeE.EndI(); it++) {
1819  if (it.GetDat().GetVal1() == IntType) {
1820  IntAttrNames.Add(it.GetKey());
1821  }
1822  if (it.GetDat().GetVal1() == FltType) {
1823  FltAttrNames.Add(it.GetKey());
1824  }
1825  if (it.GetDat().GetVal1() == StrType) {
1826  StrAttrNames.Add(it.GetKey());
1827  }
1828  }
1829 }
1830 
1831 TFlt TNEANet::GetWeightOutEdges(const TNodeI& NI, const TStr& attr) {
1832  TNode Node = GetNode(NI.GetId());
1833  TIntV OutEIdV = Node.OutEIdV;
1834  TFlt total = 0;
1835  int len = Node.OutEIdV.Len();
1836  for (int i = 0; i < len; i++) {
1837  total += GetFltAttrDatE(Node.OutEIdV[i], attr);
1838  }
1839  return total;
1840 }
1841 
1842 void TNEANet::GetWeightOutEdgesV(TFltV& OutWeights, const TFltV& AttrVal) {
1843  for (TEdgeI it = BegEI(); it < EndEI(); it++) {
1844  int EId = it.GetId();
1845  int SrcId = it.GetSrcNId();
1846  OutWeights[SrcId] +=AttrVal[GetFltKeyIdE(EId)];
1847  }
1848 }
1849 
1850 bool TNEANet::IsFltAttrE(const TStr& attr) {
1851  return (KeyToIndexTypeE.IsKey(attr) &&
1852  KeyToIndexTypeE.GetDat(attr).Val1 == FltType);
1853 }
1854 
1855 bool TNEANet::IsIntAttrE(const TStr& attr) {
1856  return (KeyToIndexTypeE.IsKey(attr) &&
1857  KeyToIndexTypeE.GetDat(attr).Val1 == IntType);
1858 }
1859 
1860 bool TNEANet::IsStrAttrE(const TStr& attr) {
1861  return (KeyToIndexTypeE.IsKey(attr) &&
1862  KeyToIndexTypeE.GetDat(attr).Val1 == StrType);
1863 }
1864 
1865 int TNEANet::AddSAttrDatN(const TInt& NId, const TStr& AttrName, const TInt& Val) {
1866  if (!IsNode(NId)) {
1867  return -1;
1868  }
1869  return SAttrN.AddSAttrDat(NId, AttrName, Val);
1870 }
1871 
1872 int TNEANet::AddSAttrDatN(const TInt& NId, const TInt& AttrId, const TInt& Val) {
1873  if (!IsNode(NId)) {
1874  return -1;
1875  }
1876  return SAttrN.AddSAttrDat(NId, AttrId, Val);
1877 }
1878 
1879 int TNEANet::AddSAttrDatN(const TInt& NId, const TStr& AttrName, const TFlt& Val) {
1880  if (!IsNode(NId)) {
1881  return -1;
1882  }
1883  return SAttrN.AddSAttrDat(NId, AttrName, Val);
1884 }
1885 
1886 int TNEANet::AddSAttrDatN(const TInt& NId, const TInt& AttrId, const TFlt& Val) {
1887  if (!IsNode(NId)) {
1888  return -1;
1889  }
1890  return SAttrN.AddSAttrDat(NId, AttrId, Val);
1891 }
1892 
1893 int TNEANet::AddSAttrDatN(const TInt& NId, const TStr& AttrName, const TStr& Val) {
1894  if (!IsNode(NId)) {
1895  return -1;
1896  }
1897  return SAttrN.AddSAttrDat(NId, AttrName, Val);
1898 }
1899 
1900 int TNEANet::AddSAttrDatN(const TInt& NId, const TInt& AttrId, const TStr& Val) {
1901  if (!IsNode(NId)) {
1902  return -1;
1903  }
1904  return SAttrN.AddSAttrDat(NId, AttrId, Val);
1905 }
1906 
1907 int TNEANet::GetSAttrDatN(const TInt& NId, const TStr& AttrName, TInt& Val) const {
1908  if (!IsNode(NId)) {
1909  return -1;
1910  }
1911  return SAttrN.GetSAttrDat(NId, AttrName, Val);
1912 }
1913 
1914 int TNEANet::GetSAttrDatN(const TInt& NId, const TInt& AttrId, TInt& Val) const {
1915  if (!IsNode(NId)) {
1916  return -1;
1917  }
1918  return SAttrN.GetSAttrDat(NId, AttrId, Val);
1919 }
1920 
1921 int TNEANet::GetSAttrDatN(const TInt& NId, const TStr& AttrName, TFlt& Val) const {
1922  if (!IsNode(NId)) {
1923  return -1;
1924  }
1925  return SAttrN.GetSAttrDat(NId, AttrName, Val);
1926 }
1927 
1928 int TNEANet::GetSAttrDatN(const TInt& NId, const TInt& AttrId, TFlt& Val) const {
1929  if (!IsNode(NId)) {
1930  return -1;
1931  }
1932  return SAttrN.GetSAttrDat(NId, AttrId, Val);
1933 }
1934 
1935 int TNEANet::GetSAttrDatN(const TInt& NId, const TStr& AttrName, TStr& Val) const {
1936  if (!IsNode(NId)) {
1937  return -1;
1938  }
1939  return SAttrN.GetSAttrDat(NId, AttrName, Val);
1940 }
1941 
1942 int TNEANet::GetSAttrDatN(const TInt& NId, const TInt& AttrId, TStr& Val) const {
1943  if (!IsNode(NId)) {
1944  return -1;
1945  }
1946  return SAttrN.GetSAttrDat(NId, AttrId, Val);
1947 }
1948 
1949 int TNEANet::DelSAttrDatN(const TInt& NId, const TStr& AttrName) {
1950  if (!IsNode(NId)) {
1951  return -1;
1952  }
1953  return SAttrN.DelSAttrDat(NId, AttrName);
1954 }
1955 
1956 int TNEANet::DelSAttrDatN(const TInt& NId, const TInt& AttrId) {
1957  if (!IsNode(NId)) {
1958  return -1;
1959  }
1960  return SAttrN.DelSAttrDat(NId, AttrId);
1961 }
1962 
1963 int TNEANet::GetSAttrVN(const TInt& NId, const TAttrType AttrType, TAttrPrV& AttrV) const {
1964  if (!IsNode(NId)) {
1965  return -1;
1966  }
1967  SAttrN.GetSAttrV(NId, AttrType, AttrV);
1968  return 0;
1969 }
1970 
1971 int TNEANet::GetIdVSAttrN(const TStr& AttrName, TIntV& IdV) const {
1972  return SAttrN.GetIdVSAttr(AttrName, IdV);
1973 }
1974 
1975 int TNEANet::GetIdVSAttrN(const TInt& AttrId, TIntV& IdV) const {
1976  return SAttrN.GetIdVSAttr(AttrId, IdV);
1977 }
1978 
1979 int TNEANet::AddSAttrN(const TStr& Name, const TAttrType& AttrType, TInt& AttrId) {
1980  return SAttrN.AddSAttr(Name, AttrType, AttrId);
1981 }
1982 
1983 int TNEANet::GetSAttrIdN(const TStr& Name, TInt& AttrId, TAttrType& AttrType) const {
1984  return SAttrN.GetSAttrId(Name, AttrId, AttrType);
1985 }
1986 
1987 int TNEANet::GetSAttrNameN(const TInt& AttrId, TStr& Name, TAttrType& AttrType) const {
1988  return SAttrN.GetSAttrName(AttrId, Name, AttrType);
1989 }
1990 
1991 int TNEANet::AddSAttrDatE(const TInt& EId, const TStr& AttrName, const TInt& Val) {
1992  if (!IsEdge(EId)) {
1993  return -1;
1994  }
1995  return SAttrE.AddSAttrDat(EId, AttrName, Val);
1996 }
1997 
1998 int TNEANet::AddSAttrDatE(const TInt& EId, const TInt& AttrId, const TInt& Val) {
1999  if (!IsEdge(EId)) {
2000  return -1;
2001  }
2002  return SAttrE.AddSAttrDat(EId, AttrId, Val);
2003 }
2004 
2005 int TNEANet::AddSAttrDatE(const TInt& EId, const TStr& AttrName, const TFlt& Val) {
2006  if (!IsEdge(EId)) {
2007  return -1;
2008  }
2009  return SAttrE.AddSAttrDat(EId, AttrName, Val);
2010 }
2011 
2012 int TNEANet::AddSAttrDatE(const TInt& EId, const TInt& AttrId, const TFlt& Val) {
2013  if (!IsEdge(EId)) {
2014  return -1;
2015  }
2016  return SAttrE.AddSAttrDat(EId, AttrId, Val);
2017 }
2018 
2019 int TNEANet::AddSAttrDatE(const TInt& EId, const TStr& AttrName, const TStr& Val) {
2020  if (!IsEdge(EId)) {
2021  return -1;
2022  }
2023  return SAttrE.AddSAttrDat(EId, AttrName, Val);
2024 }
2025 
2026 int TNEANet::AddSAttrDatE(const TInt& EId, const TInt& AttrId, const TStr& Val) {
2027  if (!IsEdge(EId)) {
2028  return -1;
2029  }
2030  return SAttrE.AddSAttrDat(EId, AttrId, Val);
2031 }
2032 
2033 int TNEANet::GetSAttrDatE(const TInt& EId, const TStr& AttrName, TInt& Val) const {
2034  if (!IsEdge(EId)) {
2035  return -1;
2036  }
2037  return SAttrE.GetSAttrDat(EId, AttrName, Val);
2038 }
2039 
2040 int TNEANet::GetSAttrDatE(const TInt& EId, const TInt& AttrId, TInt& Val) const {
2041  if (!IsEdge(EId)) {
2042  return -1;
2043  }
2044  return SAttrE.GetSAttrDat(EId, AttrId, Val);
2045 }
2046 
2047 int TNEANet::GetSAttrDatE(const TInt& EId, const TStr& AttrName, TFlt& Val) const {
2048  if (!IsEdge(EId)) {
2049  return -1;
2050  }
2051  return SAttrE.GetSAttrDat(EId, AttrName, Val);
2052 }
2053 
2054 int TNEANet::GetSAttrDatE(const TInt& EId, const TInt& AttrId, TFlt& Val) const {
2055  if (!IsEdge(EId)) {
2056  return -1;
2057  }
2058  return SAttrE.GetSAttrDat(EId, AttrId, Val);
2059 }
2060 
2061 int TNEANet::GetSAttrDatE(const TInt& EId, const TStr& AttrName, TStr& Val) const {
2062  if (!IsEdge(EId)) {
2063  return -1;
2064  }
2065  return SAttrE.GetSAttrDat(EId, AttrName, Val);
2066 }
2067 
2068 int TNEANet::GetSAttrDatE(const TInt& EId, const TInt& AttrId, TStr& Val) const {
2069  if (!IsEdge(EId)) {
2070  return -1;
2071  }
2072  return SAttrE.GetSAttrDat(EId, AttrId, Val);
2073 }
2074 
2075 int TNEANet::DelSAttrDatE(const TInt& EId, const TStr& AttrName) {
2076  if (!IsEdge(EId)) {
2077  return -1;
2078  }
2079  return SAttrE.DelSAttrDat(EId, AttrName);
2080 }
2081 
2082 int TNEANet::DelSAttrDatE(const TInt& EId, const TInt& AttrId) {
2083  if (!IsEdge(EId)) {
2084  return -1;
2085  }
2086  return SAttrE.DelSAttrDat(EId, AttrId);
2087 }
2088 
2089 int TNEANet::GetSAttrVE(const TInt& EId, const TAttrType AttrType, TAttrPrV& AttrV) const {
2090  if (!IsEdge(EId)) {
2091  return -1;
2092  }
2093  SAttrE.GetSAttrV(EId, AttrType, AttrV);
2094  return 0;
2095 }
2096 
2097 int TNEANet::GetIdVSAttrE(const TStr& AttrName, TIntV& IdV) const {
2098  return SAttrE.GetIdVSAttr(AttrName, IdV);
2099 }
2100 
2101 int TNEANet::GetIdVSAttrE(const TInt& AttrId, TIntV& IdV) const {
2102  return SAttrE.GetIdVSAttr(AttrId, IdV);
2103 }
2104 
2105 int TNEANet::AddSAttrE(const TStr& Name, const TAttrType& AttrType, TInt& AttrId) {
2106  return SAttrE.AddSAttr(Name, AttrType, AttrId);
2107 }
2108 
2109 int TNEANet::GetSAttrIdE(const TStr& Name, TInt& AttrId, TAttrType& AttrType) const {
2110  return SAttrE.GetSAttrId(Name, AttrId, AttrType);
2111 }
2112 
2113 int TNEANet::GetSAttrNameE(const TInt& AttrId, TStr& Name, TAttrType& AttrType) const {
2114  return SAttrE.GetSAttrName(AttrId, Name, AttrType);
2115 }
2116 
2118 // Undirected Graph
2119 bool TUndirNet::HasFlag(const TGraphFlag& Flag) const {
2120  return HasGraphFlag(TUndirNet::TNet, Flag);
2121 }
2122 
2123 // Add a node of ID NId to the graph.
2124 int TUndirNet::AddNode(int NId) {
2125  if (NId == -1) {
2126  NId = MxNId; MxNId++;
2127  } else {
2128  IAssertR(!IsNode(NId), TStr::Fmt("NodeId %d already exists", NId));
2129  MxNId = TMath::Mx(NId+1, MxNId());
2130  }
2131  NodeH.AddDat(NId, TNode(NId));
2132  return NId;
2133 }
2134 
2136  if (NId == -1) {
2137  NId = MxNId; MxNId++;
2138  } else {
2139  if (IsNode(NId)) { return -1;}
2140  MxNId = TMath::Mx(NId+1, MxNId());
2141  }
2142  NodeH.AddDat(NId, TNode(NId));
2143  return NId;
2144 }
2145 
2146 // Add a node of ID NId to the graph and create edges to all nodes in vector NbrNIdV.
2147 int TUndirNet::AddNode(const int& NId, const TIntV& NbrNIdV) {
2148  int NewNId;
2149  if (NId == -1) {
2150  NewNId = MxNId; MxNId++;
2151  } else {
2152  IAssertR(! IsNode(NId), TStr::Fmt("NodeId %d already exists", NId));
2153  NewNId = NId;
2154  MxNId = TMath::Mx(NewNId+1, MxNId());
2155  }
2156  TNode& Node = NodeH.AddDat(NewNId);
2157  Node.Id = NewNId;
2158  Node.NIdV = NbrNIdV;
2159  Node.NIdV.Sort();
2160  NEdges += Node.GetDeg();
2161  for (int i = 0; i < NbrNIdV.Len(); i++) {
2162  GetNode(NbrNIdV[i]).NIdV.AddSorted(NewNId);
2163  }
2164  return NewNId;
2165 }
2166 
2167 // Add a node of ID NId to the graph and create edges to all nodes in the vector NIdVId in the vector pool Pool).
2168 int TUndirNet::AddNode(const int& NId, const TVecPool<TInt>& Pool, const int& NIdVId) {
2169  int NewNId;
2170  if (NId == -1) {
2171  NewNId = MxNId; MxNId++;
2172  } else {
2173  IAssertR(!IsNode(NId), TStr::Fmt("NodeId %d already exists", NId));
2174  NewNId = NId;
2175  MxNId = TMath::Mx(NewNId+1, MxNId());
2176  }
2177  TNode& Node = NodeH.AddDat(NewNId);
2178  Node.Id = NewNId;
2179  Node.NIdV.GenExt(Pool.GetValVPt(NIdVId), Pool.GetVLen(NIdVId));
2180  Node.NIdV.Sort();
2181  NEdges += Node.GetDeg();
2182  return NewNId;
2183 }
2184 
2185 // Delete node of ID NId from the graph.
2186 void TUndirNet::DelNode(const int& NId) {
2187  { AssertR(IsNode(NId), TStr::Fmt("NodeId %d does not exist", NId));
2188  TInt Id(NId);
2189  SAttrN.DelSAttrId(Id);
2190  TNode& Node = GetNode(NId);
2191  NEdges -= Node.GetDeg();
2192  for (int e = 0; e < Node.GetDeg(); e++) {
2193  const int nbr = Node.GetNbrNId(e);
2194  if (nbr == NId) { continue; }
2195  TNode& N = GetNode(nbr);
2196  const int n = N.NIdV.SearchBin(NId);
2197  IAssert(n != -1); // if NId points to N, then N also should point back
2198  if (n!= -1) { N.NIdV.Del(n); }
2199  } }
2200  NodeH.DelKey(NId);
2201 }
2202 
2203 int TUndirNet::GetEdges() const {
2204  //int Edges = 0;
2205  //for (int N=NodeH.FFirstKeyId(); NodeH.FNextKeyId(N); ) {
2206  // Edges += NodeH[N].GetDeg();
2207  //}
2208  //return Edges/2;
2209  return NEdges;
2210 }
2211 
2212 // Add an edge between SrcNId and DstNId to the graph.
2213 int TUndirNet::AddEdge(const int& SrcNId, const int& DstNId) {
2214  IAssertR(IsNode(SrcNId) && IsNode(DstNId), TStr::Fmt("%d or %d not a node.", SrcNId, DstNId).CStr());
2215  if (IsEdge(SrcNId, DstNId)) { return -2; } // edge already exists
2216  GetNode(SrcNId).NIdV.AddSorted(DstNId);
2217  if (SrcNId!=DstNId) { // not a self edge
2218  GetNode(DstNId).NIdV.AddSorted(SrcNId); }
2219  NEdges++;
2220  return -1; // no edge id
2221 }
2222 
2223 // Add an edge between SrcNId and DstNId to the graph.
2224 int TUndirNet::AddEdgeUnchecked(const int& SrcNId, const int& DstNId) {
2225  GetNode(SrcNId).NIdV.Add(DstNId);
2226  if (SrcNId!=DstNId) { // not a self edge
2227  GetNode(DstNId).NIdV.Add(SrcNId); }
2228  NEdges++;
2229  return -1; // no edge id
2230 }
2231 
2232 // Delete an edge between node IDs SrcNId and DstNId from the graph.
2233 void TUndirNet::DelEdge(const int& SrcNId, const int& DstNId) {
2234  IAssertR(IsNode(SrcNId) && IsNode(DstNId), TStr::Fmt("%d or %d not a node.", SrcNId, DstNId).CStr());
2235  { TNode& N = GetNode(SrcNId);
2236  const int n = N.NIdV.SearchBin(DstNId);
2237  TIntPr Id(SrcNId, DstNId);
2238  SAttrE.DelSAttrId(Id);
2239  if (n!= -1) { N.NIdV.Del(n); NEdges--; } }
2240  if (SrcNId != DstNId) { // not a self edge
2241  TNode& N = GetNode(DstNId);
2242  const int n = N.NIdV.SearchBin(SrcNId);
2243  if (n!= -1) { N.NIdV.Del(n); }
2244  }
2245 }
2246 
2247 // Test whether an edge between node IDs SrcNId and DstNId exists the graph.
2248 bool TUndirNet::IsEdge(const int& SrcNId, const int& DstNId) const {
2249  if (! IsNode(SrcNId) || ! IsNode(DstNId)) return false;
2250  return GetNode(SrcNId).IsNbrNId(DstNId);
2251 }
2252 
2253 // Return an iterator referring to edge (SrcNId, DstNId) in the graph.
2254 TUndirNet::TEdgeI TUndirNet::GetEI(const int& SrcNId, const int& DstNId) const {
2255  const int MnNId = TMath::Mn(SrcNId, DstNId);
2256  const int MxNId = TMath::Mx(SrcNId, DstNId);
2257  const TNodeI SrcNI = GetNI(MnNId);
2258  const int NodeN = SrcNI.NodeHI.GetDat().NIdV.SearchBin(MxNId);
2259  IAssert(NodeN != -1);
2260  return TEdgeI(SrcNI, EndNI(), NodeN);
2261 }
2262 
2263 
2264 // Get a vector IDs of all nodes in the graph.
2265 void TUndirNet::GetNIdV(TIntV& NIdV) const {
2266  NIdV.Gen(GetNodes(), 0);
2267  for (int N=NodeH.FFirstKeyId(); NodeH.FNextKeyId(N); ) {
2268  NIdV.Add(NodeH.GetKey(N)); }
2269 }
2270 
2271 // Defragment the graph.
2272 void TUndirNet::Defrag(const bool& OnlyNodeLinks) {
2273  for (int n = NodeH.FFirstKeyId(); NodeH.FNextKeyId(n); ) {
2274  NodeH[n].NIdV.Pack();
2275  }
2276  if (! OnlyNodeLinks && ! NodeH.IsKeyIdEqKeyN()) {
2277  NodeH.Defrag();
2278  }
2279 }
2280 
2281 // Check the graph data structure for internal consistency.
2282 bool TUndirNet::IsOk(const bool& ThrowExcept) const {
2283  bool RetVal = true;
2284  for (int N = NodeH.FFirstKeyId(); NodeH.FNextKeyId(N); ) {
2285  const TNode& Node = NodeH[N];
2286  if (! Node.NIdV.IsSorted()) {
2287  const TStr Msg = TStr::Fmt("Neighbor list of node %d is not sorted.", Node.GetId());
2288  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); }
2289  RetVal=false;
2290  }
2291  int prevNId = -1;
2292  for (int e = 0; e < Node.GetDeg(); e++) {
2293  if (! IsNode(Node.GetNbrNId(e))) {
2294  const TStr Msg = TStr::Fmt("Edge %d --> %d: node %d does not exist.",
2295  Node.GetId(), Node.GetNbrNId(e), Node.GetNbrNId(e));
2296  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); }
2297  RetVal=false;
2298  }
2299  if (e > 0 && prevNId == Node.GetNbrNId(e)) {
2300  const TStr Msg = TStr::Fmt("Node %d has duplicate edge %d --> %d.",
2301  Node.GetId(), Node.GetId(), Node.GetNbrNId(e));
2302  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); }
2303  RetVal=false;
2304  }
2305  prevNId = Node.GetNbrNId(e);
2306  }
2307  }
2308  int EdgeCnt = 0;
2309  for (TEdgeI EI = BegEI(); EI < EndEI(); EI++) { EdgeCnt++; }
2310  if (EdgeCnt != GetEdges()) {
2311  const TStr Msg = TStr::Fmt("Number of edges counter is corrupted: GetEdges():%d, EdgeCount:%d.", GetEdges(), EdgeCnt);
2312  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); }
2313  RetVal=false;
2314  }
2315  return RetVal;
2316 }
2317 
2318 // Print the graph in a human readable form to an output stream OutF.
2319 void TUndirNet::Dump(FILE *OutF) const {
2320  const int NodePlaces = (int) ceil(log10((double) GetNodes()));
2321  fprintf(OutF, "-------------------------------------------------\nUndirected Node Graph: nodes: %d, edges: %d\n", GetNodes(), GetEdges());
2322  for (int N = NodeH.FFirstKeyId(); NodeH.FNextKeyId(N); ) {
2323  const TNode& Node = NodeH[N];
2324  fprintf(OutF, " %*d [%d] ", NodePlaces, Node.GetId(), Node.GetDeg());
2325  for (int edge = 0; edge < Node.GetDeg(); edge++) {
2326  fprintf(OutF, " %*d", NodePlaces, Node.GetNbrNId(edge)); }
2327  fprintf(OutF, "\n");
2328  }
2329  fprintf(OutF, "\n");
2330 }
2331 
2332 // Return a small graph on 5 nodes and 5 edges.
2334  PUndirNet Graph = TUndirNet::New();
2335  for (int i = 0; i < 5; i++) { Graph->AddNode(i); }
2336  Graph->AddEdge(0,1); Graph->AddEdge(0,2);
2337  Graph->AddEdge(0,3); Graph->AddEdge(0,4);
2338  Graph->AddEdge(1,2);
2339  return Graph;
2340 }
2341 
2342 int TUndirNet::AddSAttrDatN(const TInt& NId, const TStr& AttrName, const TInt& Val) {
2343  if (!IsNode(NId)) {
2344  return -1;
2345  }
2346  return SAttrN.AddSAttrDat(NId, AttrName, Val);
2347 }
2348 
2349 int TUndirNet::AddSAttrDatN(const TInt& NId, const TInt& AttrId, const TInt& Val) {
2350  if (!IsNode(NId)) {
2351  return -1;
2352  }
2353  return SAttrN.AddSAttrDat(NId, AttrId, Val);
2354 }
2355 
2356 int TUndirNet::AddSAttrDatN(const TInt& NId, const TStr& AttrName, const TFlt& Val) {
2357  if (!IsNode(NId)) {
2358  return -1;
2359  }
2360  return SAttrN.AddSAttrDat(NId, AttrName, Val);
2361 }
2362 
2363 int TUndirNet::AddSAttrDatN(const TInt& NId, const TInt& AttrId, const TFlt& Val) {
2364  if (!IsNode(NId)) {
2365  return -1;
2366  }
2367  return SAttrN.AddSAttrDat(NId, AttrId, Val);
2368 }
2369 
2370 int TUndirNet::AddSAttrDatN(const TInt& NId, const TStr& AttrName, const TStr& Val) {
2371  if (!IsNode(NId)) {
2372  return -1;
2373  }
2374  return SAttrN.AddSAttrDat(NId, AttrName, Val);
2375 }
2376 
2377 int TUndirNet::AddSAttrDatN(const TInt& NId, const TInt& AttrId, const TStr& Val) {
2378  if (!IsNode(NId)) {
2379  return -1;
2380  }
2381  return SAttrN.AddSAttrDat(NId, AttrId, Val);
2382 }
2383 
2384 int TUndirNet::GetSAttrDatN(const TInt& NId, const TStr& AttrName, TInt& Val) const {
2385  if (!IsNode(NId)) {
2386  return -1;
2387  }
2388  return SAttrN.GetSAttrDat(NId, AttrName, Val);
2389 }
2390 
2391 int TUndirNet::GetSAttrDatN(const TInt& NId, const TInt& AttrId, TInt& Val) const {
2392  if (!IsNode(NId)) {
2393  return -1;
2394  }
2395  return SAttrN.GetSAttrDat(NId, AttrId, Val);
2396 }
2397 
2398 int TUndirNet::GetSAttrDatN(const TInt& NId, const TStr& AttrName, TFlt& Val) const {
2399  if (!IsNode(NId)) {
2400  return -1;
2401  }
2402  return SAttrN.GetSAttrDat(NId, AttrName, Val);
2403 }
2404 
2405 int TUndirNet::GetSAttrDatN(const TInt& NId, const TInt& AttrId, TFlt& Val) const {
2406  if (!IsNode(NId)) {
2407  return -1;
2408  }
2409  return SAttrN.GetSAttrDat(NId, AttrId, Val);
2410 }
2411 
2412 int TUndirNet::GetSAttrDatN(const TInt& NId, const TStr& AttrName, TStr& Val) const {
2413  if (!IsNode(NId)) {
2414  return -1;
2415  }
2416  return SAttrN.GetSAttrDat(NId, AttrName, Val);
2417 }
2418 
2419 int TUndirNet::GetSAttrDatN(const TInt& NId, const TInt& AttrId, TStr& Val) const {
2420  if (!IsNode(NId)) {
2421  return -1;
2422  }
2423  return SAttrN.GetSAttrDat(NId, AttrId, Val);
2424 }
2425 
2426 int TUndirNet::DelSAttrDatN(const TInt& NId, const TStr& AttrName) {
2427  if (!IsNode(NId)) {
2428  return -1;
2429  }
2430  return SAttrN.DelSAttrDat(NId, AttrName);
2431 }
2432 
2433 int TUndirNet::DelSAttrDatN(const TInt& NId, const TInt& AttrId) {
2434  if (!IsNode(NId)) {
2435  return -1;
2436  }
2437  return SAttrN.DelSAttrDat(NId, AttrId);
2438 }
2439 
2440 int TUndirNet::GetSAttrVN(const TInt& NId, const TAttrType AttrType, TAttrPrV& AttrV) const {
2441  if (!IsNode(NId)) {
2442  return -1;
2443  }
2444  SAttrN.GetSAttrV(NId, AttrType, AttrV);
2445  return 0;
2446 }
2447 
2448 int TUndirNet::GetIdVSAttrN(const TStr& AttrName, TIntV& IdV) const {
2449  return SAttrN.GetIdVSAttr(AttrName, IdV);
2450 }
2451 
2452 int TUndirNet::GetIdVSAttrN(const TInt& AttrId, TIntV& IdV) const {
2453  return SAttrN.GetIdVSAttr(AttrId, IdV);
2454 }
2455 
2456 int TUndirNet::AddSAttrN(const TStr& Name, const TAttrType& AttrType, TInt& AttrId) {
2457  return SAttrN.AddSAttr(Name, AttrType, AttrId);
2458 }
2459 
2460 int TUndirNet::GetSAttrIdN(const TStr& Name, TInt& AttrId, TAttrType& AttrType) const {
2461  return SAttrN.GetSAttrId(Name, AttrId, AttrType);
2462 }
2463 
2464 int TUndirNet::GetSAttrNameN(const TInt& AttrId, TStr& Name, TAttrType& AttrType) const {
2465  return SAttrN.GetSAttrName(AttrId, Name, AttrType);
2466 }
2467 
2468 TIntPr TUndirNet::OrderEdgeNodes(const int& SrcNId, const int& DstNId) const{
2469  if (SrcNId < DstNId) {
2470  return TIntPr(SrcNId, DstNId);
2471  }
2472  return TIntPr(DstNId, SrcNId);
2473 }
2474 
2475 int TUndirNet::AddSAttrDatE(const int& SrcNId, const int& DstNId, const TStr& AttrName, const TInt& Val) {
2476  if (!IsEdge(SrcNId, DstNId)) {
2477  return -1;
2478  }
2479  TIntPr EId = OrderEdgeNodes(SrcNId, DstNId);
2480  return SAttrE.AddSAttrDat(EId, AttrName, Val);
2481 }
2482 
2483 int TUndirNet::AddSAttrDatE(const int& SrcNId, const int& DstNId, const TInt& AttrId, const TInt& Val) {
2484  if (!IsEdge(SrcNId, DstNId)) {
2485  return -1;
2486  }
2487  TIntPr EId = OrderEdgeNodes(SrcNId, DstNId);
2488  return SAttrE.AddSAttrDat(EId, AttrId, Val);
2489 }
2490 
2491 int TUndirNet::AddSAttrDatE(const int& SrcNId, const int& DstNId, const TStr& AttrName, const TFlt& Val) {
2492  if (!IsEdge(SrcNId, DstNId)) {
2493  return -1;
2494  }
2495  TIntPr EId = OrderEdgeNodes(SrcNId, DstNId);
2496  return SAttrE.AddSAttrDat(EId, AttrName, Val);
2497 }
2498 
2499 int TUndirNet::AddSAttrDatE(const int& SrcNId, const int& DstNId, const TInt& AttrId, const TFlt& Val) {
2500  if (!IsEdge(SrcNId, DstNId)) {
2501  return -1;
2502  }
2503  TIntPr EId = OrderEdgeNodes(SrcNId, DstNId);
2504  return SAttrE.AddSAttrDat(EId, AttrId, Val);
2505 }
2506 
2507 int TUndirNet::AddSAttrDatE(const int& SrcNId, const int& DstNId, const TStr& AttrName, const TStr& Val) {
2508  if (!IsEdge(SrcNId, DstNId)) {
2509  return -1;
2510  }
2511  TIntPr EId = OrderEdgeNodes(SrcNId, DstNId);
2512  return SAttrE.AddSAttrDat(EId, AttrName, Val);
2513 }
2514 
2515 int TUndirNet::AddSAttrDatE(const int& SrcNId, const int& DstNId, const TInt& AttrId, const TStr& Val) {
2516  if (!IsEdge(SrcNId, DstNId)) {
2517  return -1;
2518  }
2519  TIntPr EId = OrderEdgeNodes(SrcNId, DstNId);
2520  return SAttrE.AddSAttrDat(EId, AttrId, Val);
2521 }
2522 
2523 int TUndirNet::GetSAttrDatE(const int& SrcNId, const int& DstNId, const TStr& AttrName, TInt& Val) const {
2524  if (!IsEdge(SrcNId, DstNId)) {
2525  return -1;
2526  }
2527  TIntPr EId = OrderEdgeNodes(SrcNId, DstNId);
2528  return SAttrE.GetSAttrDat(EId, AttrName, Val);
2529 }
2530 
2531 int TUndirNet::GetSAttrDatE(const int& SrcNId, const int& DstNId, const TInt& AttrId, TInt& Val) const {
2532  if (!IsEdge(SrcNId, DstNId)) {
2533  return -1;
2534  }
2535  TIntPr EId = OrderEdgeNodes(SrcNId, DstNId);
2536  return SAttrE.GetSAttrDat(EId, AttrId, Val);
2537 }
2538 
2539 int TUndirNet::GetSAttrDatE(const int& SrcNId, const int& DstNId, const TStr& AttrName, TFlt& Val) const {
2540  if (!IsEdge(SrcNId, DstNId)) {
2541  return -1;
2542  }
2543  TIntPr EId = OrderEdgeNodes(SrcNId, DstNId);
2544  return SAttrE.GetSAttrDat(EId, AttrName, Val);
2545 }
2546 
2547 int TUndirNet::GetSAttrDatE(const int& SrcNId, const int& DstNId, const TInt& AttrId, TFlt& Val) const {
2548  if (!IsEdge(SrcNId, DstNId)) {
2549  return -1;
2550  }
2551  TIntPr EId = OrderEdgeNodes(SrcNId, DstNId);
2552  return SAttrE.GetSAttrDat(EId, AttrId, Val);
2553 }
2554 
2555 int TUndirNet::GetSAttrDatE(const int& SrcNId, const int& DstNId, const TStr& AttrName, TStr& Val) const {
2556  if (!IsEdge(SrcNId, DstNId)) {
2557  return -1;
2558  }
2559  TIntPr EId = OrderEdgeNodes(SrcNId, DstNId);
2560  return SAttrE.GetSAttrDat(EId, AttrName, Val);
2561 }
2562 
2563 int TUndirNet::GetSAttrDatE(const int& SrcNId, const int& DstNId, const TInt& AttrId, TStr& Val) const {
2564  if (!IsEdge(SrcNId, DstNId)) {
2565  return -1;
2566  }
2567  TIntPr EId = OrderEdgeNodes(SrcNId, DstNId);
2568  return SAttrE.GetSAttrDat(EId, AttrId, Val);
2569 }
2570 
2571 int TUndirNet::DelSAttrDatE(const int& SrcNId, const int& DstNId, const TStr& AttrName) {
2572  if (!IsEdge(SrcNId, DstNId)) {
2573  return -1;
2574  }
2575  TIntPr EId = OrderEdgeNodes(SrcNId, DstNId);
2576  return SAttrE.DelSAttrDat(EId, AttrName);
2577 }
2578 
2579 int TUndirNet::DelSAttrDatE(const int& SrcNId, const int& DstNId, const TInt& AttrId) {
2580  if (!IsEdge(SrcNId, DstNId)) {
2581  return -1;
2582  }
2583  TIntPr EId = OrderEdgeNodes(SrcNId, DstNId);
2584  return SAttrE.DelSAttrDat(EId, AttrId);
2585 }
2586 
2587 int TUndirNet::GetSAttrVE(const int& SrcNId, const int& DstNId, const TAttrType AttrType, TAttrPrV& AttrV) const {
2588  if (!IsEdge(SrcNId, DstNId)) {
2589  return -1;
2590  }
2591  TIntPr EId = OrderEdgeNodes(SrcNId, DstNId);
2592  SAttrE.GetSAttrV(EId, AttrType, AttrV);
2593  return 0;
2594 }
2595 
2596 int TUndirNet::GetIdVSAttrE(const TStr& AttrName, TIntPrV& IdV) const {
2597  return SAttrE.GetIdVSAttr(AttrName, IdV);
2598 }
2599 
2600 int TUndirNet::GetIdVSAttrE(const TInt& AttrId, TIntPrV& IdV) const {
2601  return SAttrE.GetIdVSAttr(AttrId, IdV);
2602 }
2603 
2604 int TUndirNet::AddSAttrE(const TStr& Name, const TAttrType& AttrType, TInt& AttrId) {
2605  return SAttrE.AddSAttr(Name, AttrType, AttrId);
2606 }
2607 
2608 int TUndirNet::GetSAttrIdE(const TStr& Name, TInt& AttrId, TAttrType& AttrType) const {
2609  return SAttrE.GetSAttrId(Name, AttrId, AttrType);
2610 }
2611 
2612 int TUndirNet::GetSAttrNameE(const TInt& AttrId, TStr& Name, TAttrType& AttrType) const {
2613  return SAttrE.GetSAttrName(AttrId, Name, AttrType);
2614 }
2615 
2617 // Directed Node Graph
2618 bool TDirNet::HasFlag(const TGraphFlag& Flag) const {
2619  return HasGraphFlag(TDirNet::TNet, Flag);
2620 }
2621 
2622 int TDirNet::AddNode(int NId) {
2623  if (NId == -1) {
2624  NId = MxNId; MxNId++;
2625  } else {
2626  IAssertR(!IsNode(NId), TStr::Fmt("NodeId %d already exists", NId));
2627  MxNId = TMath::Mx(NId+1, MxNId());
2628  }
2629  NodeH.AddDat(NId, TNode(NId));
2630  return NId;
2631 }
2632 
2634  if (NId == -1) {
2635  NId = MxNId; MxNId++;
2636  } else {
2637  if (IsNode(NId)) { return -1;}
2638  MxNId = TMath::Mx(NId+1, MxNId());
2639  }
2640  NodeH.AddDat(NId, TNode(NId));
2641  return NId;
2642 }
2643 
2644 // add a node with a list of neighbors
2645 // (use TDirNet::IsOk to check whether the graph is consistent)
2646 int TDirNet::AddNode(const int& NId, const TIntV& InNIdV, const TIntV& OutNIdV) {
2647  int NewNId;
2648  if (NId == -1) {
2649  NewNId = MxNId; MxNId++;
2650  } else {
2651  IAssertR(!IsNode(NId), TStr::Fmt("NodeId %d already exists", NId));
2652  NewNId = NId;
2653  MxNId = TMath::Mx(NewNId+1, MxNId());
2654  }
2655  TNode& Node = NodeH.AddDat(NewNId);
2656  Node.Id = NewNId;
2657  Node.InNIdV = InNIdV;
2658  Node.OutNIdV = OutNIdV;
2659  Node.InNIdV.Sort();
2660  Node.OutNIdV.Sort();
2661  return NewNId;
2662 }
2663 
2664 // add a node from a vector pool
2665 // (use TDirNet::IsOk to check whether the graph is consistent)
2666 int TDirNet::AddNode(const int& NId, const TVecPool<TInt>& Pool, const int& SrcVId, const int& DstVId) {
2667  int NewNId;
2668  if (NId == -1) {
2669  NewNId = MxNId; MxNId++;
2670  } else {
2671  IAssertR(!IsNode(NId), TStr::Fmt("NodeId %d already exists", NId));
2672  NewNId = NId;
2673  MxNId = TMath::Mx(NewNId+1, MxNId());
2674  }
2675  TNode& Node = NodeH.AddDat(NewNId);
2676  Node.Id = NewNId;
2677  Node.InNIdV.GenExt(Pool.GetValVPt(SrcVId), Pool.GetVLen(SrcVId));
2678  Node.OutNIdV.GenExt(Pool.GetValVPt(DstVId), Pool.GetVLen(DstVId));
2679  Node.InNIdV.Sort();
2680  Node.OutNIdV.Sort();
2681  return NewNId;
2682 }
2683 
2684 void TDirNet::DelNode(const int& NId) {
2685  { TNode& Node = GetNode(NId);
2686  TInt Id(NId);
2687  SAttrN.DelSAttrId(Id);
2688  for (int e = 0; e < Node.GetOutDeg(); e++) {
2689  const int nbr = Node.GetOutNId(e);
2690  if (nbr == NId) { continue; }
2691  TNode& N = GetNode(nbr);
2692  const int n = N.InNIdV.SearchBin(NId);
2693  if (n!= -1) { N.InNIdV.Del(n); }
2694  }
2695  for (int e = 0; e < Node.GetInDeg(); e++) {
2696  const int nbr = Node.GetInNId(e);
2697  if (nbr == NId) { continue; }
2698  TNode& N = GetNode(nbr);
2699  const int n = N.OutNIdV.SearchBin(NId);
2700  if (n!= -1) { N.OutNIdV.Del(n); }
2701  } }
2702  NodeH.DelKey(NId);
2703 }
2704 
2705 int TDirNet::GetEdges() const {
2706  int edges=0;
2707  for (int N=NodeH.FFirstKeyId(); NodeH.FNextKeyId(N); ) {
2708  edges+=NodeH[N].GetOutDeg();
2709  }
2710  return edges;
2711 }
2712 
2713 int TDirNet::AddEdge(const int& SrcNId, const int& DstNId) {
2714  IAssertR(IsNode(SrcNId) && IsNode(DstNId), TStr::Fmt("%d or %d not a node.", SrcNId, DstNId).CStr());
2715  //IAssert(! IsEdge(SrcNId, DstNId));
2716  if (IsEdge(SrcNId, DstNId)) { return -2; }
2717  GetNode(SrcNId).OutNIdV.AddSorted(DstNId);
2718  GetNode(DstNId).InNIdV.AddSorted(SrcNId);
2719  return -1; // no edge id
2720 }
2721 
2722 int TDirNet::AddEdgeUnchecked(const int& SrcNId, const int& DstNId) {
2723  GetNode(SrcNId).OutNIdV.Add(DstNId);
2724  GetNode(DstNId).InNIdV.Add(SrcNId);
2725  return -1; // no edge id
2726 }
2727 
2728 void TDirNet::DelEdge(const int& SrcNId, const int& DstNId, const bool& IsDir) {
2729  IAssertR(IsNode(SrcNId) && IsNode(DstNId), TStr::Fmt("%d or %d not a node.", SrcNId, DstNId).CStr());
2730  { TNode& N = GetNode(SrcNId);
2731  TIntPr Id(SrcNId, DstNId);
2732  SAttrE.DelSAttrId(Id);
2733  const int n = N.OutNIdV.SearchBin(DstNId);
2734  if (n!= -1) { N.OutNIdV.Del(n); } }
2735  { TNode& N = GetNode(DstNId);
2736  const int n = N.InNIdV.SearchBin(SrcNId);
2737  if (n!= -1) { N.InNIdV.Del(n); } }
2738  if (! IsDir) {
2739  { TNode& N = GetNode(SrcNId);
2740  const int n = N.InNIdV.SearchBin(DstNId);
2741  if (n!= -1) { N.InNIdV.Del(n); } }
2742  { TNode& N = GetNode(DstNId);
2743  const int n = N.OutNIdV.SearchBin(SrcNId);
2744  if (n!= -1) { N.OutNIdV.Del(n); } }
2745  }
2746 }
2747 
2748 bool TDirNet::IsEdge(const int& SrcNId, const int& DstNId, const bool& IsDir) const {
2749  if (! IsNode(SrcNId) || ! IsNode(DstNId)) { return false; }
2750  if (IsDir) { return GetNode(SrcNId).IsOutNId(DstNId); }
2751  else { return GetNode(SrcNId).IsOutNId(DstNId) || GetNode(DstNId).IsOutNId(SrcNId); }
2752 }
2753 
2754 TDirNet::TEdgeI TDirNet::GetEI(const int& SrcNId, const int& DstNId) const {
2755  const TNodeI SrcNI = GetNI(SrcNId);
2756  const int NodeN = SrcNI.NodeHI.GetDat().OutNIdV.SearchBin(DstNId);
2757  IAssert(NodeN != -1);
2758  return TEdgeI(SrcNI, EndNI(), NodeN);
2759 }
2760 
2761 void TDirNet::GetNIdV(TIntV& NIdV) const {
2762  NIdV.Gen(GetNodes(), 0);
2763  for (int N=NodeH.FFirstKeyId(); NodeH.FNextKeyId(N); ) {
2764  NIdV.Add(NodeH.GetKey(N)); }
2765 }
2766 
2767 void TDirNet::Defrag(const bool& OnlyNodeLinks) {
2768  for (int n = NodeH.FFirstKeyId(); NodeH.FNextKeyId(n); ) {
2769  TNode& Node = NodeH[n];
2770  Node.InNIdV.Pack(); Node.OutNIdV.Pack();
2771  }
2772  if (! OnlyNodeLinks && ! NodeH.IsKeyIdEqKeyN()) { NodeH.Defrag(); }
2773 }
2774 
2775 // for each node check that their neighbors are also nodes
2776 bool TDirNet::IsOk(const bool& ThrowExcept) const {
2777  bool RetVal = true;
2778  for (int N = NodeH.FFirstKeyId(); NodeH.FNextKeyId(N); ) {
2779  const TNode& Node = NodeH[N];
2780  if (! Node.OutNIdV.IsSorted()) {
2781  const TStr Msg = TStr::Fmt("Out-neighbor list of node %d is not sorted.", Node.GetId());
2782  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
2783  }
2784  if (! Node.InNIdV.IsSorted()) {
2785  const TStr Msg = TStr::Fmt("In-neighbor list of node %d is not sorted.", Node.GetId());
2786  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
2787  }
2788  // check out-edges
2789  int prevNId = -1;
2790  for (int e = 0; e < Node.GetOutDeg(); e++) {
2791  if (! IsNode(Node.GetOutNId(e))) {
2792  const TStr Msg = TStr::Fmt("Out-edge %d --> %d: node %d does not exist.",
2793  Node.GetId(), Node.GetOutNId(e), Node.GetOutNId(e));
2794  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
2795  }
2796  if (e > 0 && prevNId == Node.GetOutNId(e)) {
2797  const TStr Msg = TStr::Fmt("Node %d has duplidate out-edge %d --> %d.",
2798  Node.GetId(), Node.GetId(), Node.GetOutNId(e));
2799  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
2800  }
2801  prevNId = Node.GetOutNId(e);
2802  }
2803  // check in-edges
2804  prevNId = -1;
2805  for (int e = 0; e < Node.GetInDeg(); e++) {
2806  if (! IsNode(Node.GetInNId(e))) {
2807  const TStr Msg = TStr::Fmt("In-edge %d <-- %d: node %d does not exist.",
2808  Node.GetId(), Node.GetInNId(e), Node.GetInNId(e));
2809  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
2810  }
2811  if (e > 0 && prevNId == Node.GetInNId(e)) {
2812  const TStr Msg = TStr::Fmt("Node %d has duplidate in-edge %d <-- %d.",
2813  Node.GetId(), Node.GetId(), Node.GetInNId(e));
2814  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
2815  }
2816  prevNId = Node.GetInNId(e);
2817  }
2818  }
2819  return RetVal;
2820 }
2821 
2822 void TDirNet::Dump(FILE *OutF) const {
2823  const int NodePlaces = (int) ceil(log10((double) GetNodes()));
2824  fprintf(OutF, "-------------------------------------------------\nDirected Node Graph: nodes: %d, edges: %d\n", GetNodes(), GetEdges());
2825  for (int N = NodeH.FFirstKeyId(); NodeH.FNextKeyId(N); ) {
2826  const TNode& Node = NodeH[N];
2827  fprintf(OutF, " %*d]\n", NodePlaces, Node.GetId());
2828  fprintf(OutF, " in [%d]", Node.GetInDeg());
2829  for (int edge = 0; edge < Node.GetInDeg(); edge++) {
2830  fprintf(OutF, " %*d", NodePlaces, Node.GetInNId(edge)); }
2831  fprintf(OutF, "\n out[%d]", Node.GetOutDeg());
2832  for (int edge = 0; edge < Node.GetOutDeg(); edge++) {
2833  fprintf(OutF, " %*d", NodePlaces, Node.GetOutNId(edge)); }
2834  fprintf(OutF, "\n");
2835  }
2836  fprintf(OutF, "\n");
2837 }
2838 
2840  PDirNet G = TDirNet::New();
2841  for (int i = 0; i < 5; i++) { G->AddNode(i); }
2842  G->AddEdge(0,1); G->AddEdge(1,2); G->AddEdge(0,2);
2843  G->AddEdge(1,3); G->AddEdge(3,4); G->AddEdge(2,3);
2844  return G;
2845 }
2846 
2847 int TDirNet::AddSAttrDatN(const TInt& NId, const TStr& AttrName, const TInt& Val) {
2848  if (!IsNode(NId)) {
2849  return -1;
2850  }
2851  return SAttrN.AddSAttrDat(NId, AttrName, Val);
2852 }
2853 
2854 int TDirNet::AddSAttrDatN(const TInt& NId, const TInt& AttrId, const TInt& Val) {
2855  if (!IsNode(NId)) {
2856  return -1;
2857  }
2858  return SAttrN.AddSAttrDat(NId, AttrId, Val);
2859 }
2860 
2861 int TDirNet::AddSAttrDatN(const TInt& NId, const TStr& AttrName, const TFlt& Val) {
2862  if (!IsNode(NId)) {
2863  return -1;
2864  }
2865  return SAttrN.AddSAttrDat(NId, AttrName, Val);
2866 }
2867 
2868 int TDirNet::AddSAttrDatN(const TInt& NId, const TInt& AttrId, const TFlt& Val) {
2869  if (!IsNode(NId)) {
2870  return -1;
2871  }
2872  return SAttrN.AddSAttrDat(NId, AttrId, Val);
2873 }
2874 
2875 int TDirNet::AddSAttrDatN(const TInt& NId, const TStr& AttrName, const TStr& Val) {
2876  if (!IsNode(NId)) {
2877  return -1;
2878  }
2879  return SAttrN.AddSAttrDat(NId, AttrName, Val);
2880 }
2881 
2882 int TDirNet::AddSAttrDatN(const TInt& NId, const TInt& AttrId, const TStr& Val) {
2883  if (!IsNode(NId)) {
2884  return -1;
2885  }
2886  return SAttrN.AddSAttrDat(NId, AttrId, Val);
2887 }
2888 
2889 int TDirNet::GetSAttrDatN(const TInt& NId, const TStr& AttrName, TInt& Val) const {
2890  if (!IsNode(NId)) {
2891  return -1;
2892  }
2893  return SAttrN.GetSAttrDat(NId, AttrName, Val);
2894 }
2895 
2896 int TDirNet::GetSAttrDatN(const TInt& NId, const TInt& AttrId, TInt& Val) const {
2897  if (!IsNode(NId)) {
2898  return -1;
2899  }
2900  return SAttrN.GetSAttrDat(NId, AttrId, Val);
2901 }
2902 
2903 int TDirNet::GetSAttrDatN(const TInt& NId, const TStr& AttrName, TFlt& Val) const {
2904  if (!IsNode(NId)) {
2905  return -1;
2906  }
2907  return SAttrN.GetSAttrDat(NId, AttrName, Val);
2908 }
2909 
2910 int TDirNet::GetSAttrDatN(const TInt& NId, const TInt& AttrId, TFlt& Val) const {
2911  if (!IsNode(NId)) {
2912  return -1;
2913  }
2914  return SAttrN.GetSAttrDat(NId, AttrId, Val);
2915 }
2916 
2917 int TDirNet::GetSAttrDatN(const TInt& NId, const TStr& AttrName, TStr& Val) const {
2918  if (!IsNode(NId)) {
2919  return -1;
2920  }
2921  return SAttrN.GetSAttrDat(NId, AttrName, Val);
2922 }
2923 
2924 int TDirNet::GetSAttrDatN(const TInt& NId, const TInt& AttrId, TStr& Val) const {
2925  if (!IsNode(NId)) {
2926  return -1;
2927  }
2928  return SAttrN.GetSAttrDat(NId, AttrId, Val);
2929 }
2930 
2931 int TDirNet::DelSAttrDatN(const TInt& NId, const TStr& AttrName) {
2932  if (!IsNode(NId)) {
2933  return -1;
2934  }
2935  return SAttrN.DelSAttrDat(NId, AttrName);
2936 }
2937 
2938 int TDirNet::DelSAttrDatN(const TInt& NId, const TInt& AttrId) {
2939  if (!IsNode(NId)) {
2940  return -1;
2941  }
2942  return SAttrN.DelSAttrDat(NId, AttrId);
2943 }
2944 
2945 int TDirNet::GetSAttrVN(const TInt& NId, const TAttrType AttrType, TAttrPrV& AttrV) const {
2946  if (!IsNode(NId)) {
2947  return -1;
2948  }
2949  SAttrN.GetSAttrV(NId, AttrType, AttrV);
2950  return 0;
2951 }
2952 
2953 int TDirNet::GetIdVSAttrN(const TStr& AttrName, TIntV& IdV) const {
2954  return SAttrN.GetIdVSAttr(AttrName, IdV);
2955 }
2956 
2957 int TDirNet::GetIdVSAttrN(const TInt& AttrId, TIntV& IdV) const {
2958  return SAttrN.GetIdVSAttr(AttrId, IdV);
2959 }
2960 
2961 int TDirNet::AddSAttrN(const TStr& Name, const TAttrType& AttrType, TInt& AttrId) {
2962  return SAttrN.AddSAttr(Name, AttrType, AttrId);
2963 }
2964 
2965 int TDirNet::GetSAttrIdN(const TStr& Name, TInt& AttrId, TAttrType& AttrType) const {
2966  return SAttrN.GetSAttrId(Name, AttrId, AttrType);
2967 }
2968 
2969 int TDirNet::GetSAttrNameN(const TInt& AttrId, TStr& Name, TAttrType& AttrType) const {
2970  return SAttrN.GetSAttrName(AttrId, Name, AttrType);
2971 }
2972 
2973 int TDirNet::AddSAttrDatE(const int& SrcNId, const int& DstNId, const TStr& AttrName, const TInt& Val) {
2974  if (!IsEdge(SrcNId, DstNId)) {
2975  return -1;
2976  }
2977  TIntPr EId(SrcNId, DstNId);
2978  return SAttrE.AddSAttrDat(EId, AttrName, Val);
2979 }
2980 
2981 int TDirNet::AddSAttrDatE(const int& SrcNId, const int& DstNId, const TInt& AttrId, const TInt& Val) {
2982  if (!IsEdge(SrcNId, DstNId)) {
2983  return -1;
2984  }
2985  TIntPr EId(SrcNId, DstNId);
2986  return SAttrE.AddSAttrDat(EId, AttrId, Val);
2987 }
2988 
2989 int TDirNet::AddSAttrDatE(const int& SrcNId, const int& DstNId, const TStr& AttrName, const TFlt& Val) {
2990  if (!IsEdge(SrcNId, DstNId)) {
2991  return -1;
2992  }
2993  TIntPr EId(SrcNId, DstNId);
2994  return SAttrE.AddSAttrDat(EId, AttrName, Val);
2995 }
2996 
2997 int TDirNet::AddSAttrDatE(const int& SrcNId, const int& DstNId, const TInt& AttrId, const TFlt& Val) {
2998  if (!IsEdge(SrcNId, DstNId)) {
2999  return -1;
3000  }
3001  TIntPr EId(SrcNId, DstNId);
3002  return SAttrE.AddSAttrDat(EId, AttrId, Val);
3003 }
3004 
3005 int TDirNet::AddSAttrDatE(const int& SrcNId, const int& DstNId, const TStr& AttrName, const TStr& Val) {
3006  if (!IsEdge(SrcNId, DstNId)) {
3007  return -1;
3008  }
3009  TIntPr EId(SrcNId, DstNId);
3010  return SAttrE.AddSAttrDat(EId, AttrName, Val);
3011 }
3012 
3013 int TDirNet::AddSAttrDatE(const int& SrcNId, const int& DstNId, const TInt& AttrId, const TStr& Val) {
3014  if (!IsEdge(SrcNId, DstNId)) {
3015  return -1;
3016  }
3017  TIntPr EId(SrcNId, DstNId);
3018  return SAttrE.AddSAttrDat(EId, AttrId, Val);
3019 }
3020 
3021 int TDirNet::GetSAttrDatE(const int& SrcNId, const int& DstNId, const TStr& AttrName, TInt& Val) const {
3022  if (!IsEdge(SrcNId, DstNId)) {
3023  return -1;
3024  }
3025  TIntPr EId(SrcNId, DstNId);
3026  return SAttrE.GetSAttrDat(EId, AttrName, Val);
3027 }
3028 
3029 int TDirNet::GetSAttrDatE(const int& SrcNId, const int& DstNId, const TInt& AttrId, TInt& Val) const {
3030  if (!IsEdge(SrcNId, DstNId)) {
3031  return -1;
3032  }
3033  TIntPr EId(SrcNId, DstNId);
3034  return SAttrE.GetSAttrDat(EId, AttrId, Val);
3035 }
3036 
3037 int TDirNet::GetSAttrDatE(const int& SrcNId, const int& DstNId, const TStr& AttrName, TFlt& Val) const {
3038  if (!IsEdge(SrcNId, DstNId)) {
3039  return -1;
3040  }
3041  TIntPr EId(SrcNId, DstNId);
3042  return SAttrE.GetSAttrDat(EId, AttrName, Val);
3043 }
3044 
3045 int TDirNet::GetSAttrDatE(const int& SrcNId, const int& DstNId, const TInt& AttrId, TFlt& Val) const {
3046  if (!IsEdge(SrcNId, DstNId)) {
3047  return -1;
3048  }
3049  TIntPr EId(SrcNId, DstNId);
3050  return SAttrE.GetSAttrDat(EId, AttrId, Val);
3051 }
3052 
3053 int TDirNet::GetSAttrDatE(const int& SrcNId, const int& DstNId, const TStr& AttrName, TStr& Val) const {
3054  if (!IsEdge(SrcNId, DstNId)) {
3055  return -1;
3056  }
3057  TIntPr EId(SrcNId, DstNId);
3058  return SAttrE.GetSAttrDat(EId, AttrName, Val);
3059 }
3060 
3061 int TDirNet::GetSAttrDatE(const int& SrcNId, const int& DstNId, const TInt& AttrId, TStr& Val) const {
3062  if (!IsEdge(SrcNId, DstNId)) {
3063  return -1;
3064  }
3065  TIntPr EId(SrcNId, DstNId);
3066  return SAttrE.GetSAttrDat(EId, AttrId, Val);
3067 }
3068 
3069 int TDirNet::DelSAttrDatE(const int& SrcNId, const int& DstNId, const TStr& AttrName) {
3070  if (!IsEdge(SrcNId, DstNId)) {
3071  return -1;
3072  }
3073  TIntPr EId(SrcNId, DstNId);
3074  return SAttrE.DelSAttrDat(EId, AttrName);
3075 }
3076 
3077 int TDirNet::DelSAttrDatE(const int& SrcNId, const int& DstNId, const TInt& AttrId) {
3078  if (!IsEdge(SrcNId, DstNId)) {
3079  return -1;
3080  }
3081  TIntPr EId(SrcNId, DstNId);
3082  return SAttrE.DelSAttrDat(EId, AttrId);
3083 }
3084 
3085 int TDirNet::GetSAttrVE(const int& SrcNId, const int& DstNId, const TAttrType AttrType, TAttrPrV& AttrV) const {
3086  if (!IsEdge(SrcNId, DstNId)) {
3087  return -1;
3088  }
3089  TIntPr EId(SrcNId, DstNId);
3090  SAttrE.GetSAttrV(EId, AttrType, AttrV);
3091  return 0;
3092 }
3093 
3094 int TDirNet::GetIdVSAttrE(const TStr& AttrName, TIntPrV& IdV) const {
3095  return SAttrE.GetIdVSAttr(AttrName, IdV);
3096 }
3097 
3098 int TDirNet::GetIdVSAttrE(const TInt& AttrId, TIntPrV& IdV) const {
3099  return SAttrE.GetIdVSAttr(AttrId, IdV);
3100 }
3101 
3102 int TDirNet::AddSAttrE(const TStr& Name, const TAttrType& AttrType, TInt& AttrId) {
3103  return SAttrE.AddSAttr(Name, AttrType, AttrId);
3104 }
3105 
3106 int TDirNet::GetSAttrIdE(const TStr& Name, TInt& AttrId, TAttrType& AttrType) const {
3107  return SAttrE.GetSAttrId(Name, AttrId, AttrType);
3108 }
3109 
3110 int TDirNet::GetSAttrNameE(const TInt& AttrId, TStr& Name, TAttrType& AttrType) const {
3111  return SAttrE.GetSAttrName(AttrId, Name, AttrType);
3112 }
bool IsOk(const bool &ThrowExcept=true) const
Checks the network data structure for internal consistency.
Definition: network.cpp:2282
#define IAssert(Cond)
Definition: bd.h:262
void GetNIdV(TIntV &NIdV) const
Gets a vector IDs of all nodes in the graph.
Definition: network.cpp:887
TVec< TIntV > VecOfIntVecsN
Definition: network.h:2063
bool IsEdge(const int &SrcNId, const int &DstNId, const bool &IsDir=true) const
Tests whether an edge from node IDs SrcNId to DstNId exists in the network.
Definition: network.cpp:2748
bool HasFlag(const TGraphFlag &Flag) const
Allows for run-time checking the type of the network (see the TGraphFlag for flags).
Definition: network.cpp:2119
static const T & Mn(const T &LVal, const T &RVal)
Definition: xmath.h:36
THash< TStr, TBool > KeyToDenseE
Definition: network.h:2058
void GetNIdV(TIntV &NIdV) const
Gets a vector IDs of all nodes in the network.
Definition: network.cpp:2761
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
void IntVAttrNameNI(const TInt &NId, TStrV &Names) const
Returns a vector of int attr names for node NId.
Definition: network.h:2550
TInt GetIntAttrDatN(const TNodeI &NodeI, const TStr &attr)
Gets the value of int attr from the node attr value vector.
Definition: network.h:2931
int GetAttrIndE(const TStr &attr)
Gets the index of the edge attr value vector specified by attr.
Definition: network.cpp:1497
bool DelIfIn(const TVal &Val)
Removes the first occurrence of element Val.
Definition: ds.h:1212
int GetEdges() const
Returns the number of edges in the network.
Definition: network.cpp:2705
bool IsInNId(const int &NId) const
Tests whether node with ID NId points to the current node.
Definition: network.cpp:54
int GetId() const
Definition: network.h:1781
int DelSAttrDat(const TInt &Id, const TStr &AttrName)
Delete attribute with name AttrName for the given id Id.
Definition: attr.cpp:103
int GetSAttrIdE(const TStr &Name, TInt &AttrIdX, TAttrType &AttrTypeX) const
Gets id and type for attribute with name Name.
Definition: network.cpp:2608
static PUndirNet New()
Static constructor that returns a pointer to the network. Call: PUndirNet Graph = TUndirNet::New()...
Definition: network.h:3547
void GetAttrENames(TStrV &IntAttrNames, TStrV &FltAttrNames, TStrV &StrAttrNames) const
Fills each of the vectors with the names of edge attributes of the given type.
Definition: network.cpp:1817
int GetNodes() const
Returns the number of nodes in the network.
Definition: network.h:2363
bool IsKeyIdEqKeyN() const
Definition: hash.h:233
int GetSAttrIdN(const TStr &Name, TInt &AttrIdX, TAttrType &AttrTypeX) const
Gets id and type for attribute with name Name.
Definition: network.cpp:2965
int GetSAttrVN(const TInt &NId, const TAttrType AttrType, TAttrPrV &AttrV) const
Gets a list of all sparse attributes of type AttrType for node with id NId.
Definition: network.cpp:2945
int GetSAttrIdE(const TStr &Name, TInt &AttrIdX, TAttrType &AttrTypeX) const
Gets id and type for attribute with name Name.
Definition: network.cpp:3106
#define IAssertR(Cond, Reason)
Definition: bd.h:265
int GetSrcNId() const
Definition: network.h:1782
TStr GetStrAttrDatN(const TNodeI &NodeI, const TStr &attr)
Gets the value of str attr from the node attr value vector.
Definition: network.h:2935
THash< TStr, TFlt > FltDefaultsE
Definition: network.h:2062
int AddIntVAttrN(const TStr &attr, TBool UseDense=true)
Adds a new IntV node attribute to the hashmap.
Definition: network.cpp:1566
int AppendIntVAttrDatN(const TNodeI &NodeI, const TInt &value, const TStr &attr)
Appends value onto the TIntV attribute for the given node.
Definition: network.h:2880
int AddFltAttrDatE(const TEdgeI &EdgeI, const TFlt &value, const TStr &attr)
Attribute based add function for attr to Flt value.
Definition: network.h:2908
TStr GetEdgeAttrValue(const int &EId, const TStrIntPrH::TIter &EdgeHI) const
Returns edge attribute value, converted to Str type.
Definition: network.cpp:521
int AddFltVAttrDatE(const TEdgeI &EdgeI, const TFltV &value, const TStr &attr)
Attribute based add function for attr to TFltV value.
Definition: network.h:2924
enum TAttrType_ TAttrType
Types for tables, sparse and dense attributes.
int GetSAttrIdN(const TStr &Name, TInt &AttrIdX, TAttrType &AttrTypeX) const
Gets id and type for attribute with name Name.
Definition: network.cpp:2460
TVec< TStrV > VecOfStrVecsE
Definition: network.h:2064
THash< TStr, TInt > IntDefaultsE
Definition: network.h:2060
int GetIntAttrIndN(const TStr &attr)
Gets the index of the node attr value vector specified by attr (same as GetAttrIndN for compatibility...
Definition: network.cpp:1448
bool NodeAttrIsStrDeleted(const int &NId, const TStrIntPrH::TIter &NodeHI) const
Returns true if NId attr deleted value for current node str attr iterator.
Definition: network.cpp:278
TFlt GetWeightOutEdges(const TNodeI &NI, const TStr &attr)
Gets the sum of the weights of all the outgoing edges of the node.
Definition: network.cpp:1831
int DelAttrN(const TStr &attr)
Removes all the values for node attr.
Definition: network.cpp:1735
void Defrag(const bool &OnlyNodeLinks=false)
Defragments the network.
Definition: network.cpp:2272
bool NodeAttrIsDeleted(const int &NId, const TStrIntPrH::TIter &NodeHI) const
Returns true if NId attr deleted for current node attr iterator.
Definition: network.cpp:246
bool IsStrAttrDeletedN(const int &NId, const TStr &attr) const
Returns true if Str attr exists for node NId and has default value.
Definition: network.cpp:238
void Del(const TSizeTy &ValN)
Removes the element at position ValN.
Definition: ds.h:1189
Edge iterator. Only forward iteration (operator++) is supported.
Definition: network.h:3486
int GetIdVSAttrN(const TStr &AttrName, TIntV &IdV) const
Gets a list of all nodes that have a sparse attribute with name AttrName.
Definition: network.cpp:2448
TInt CheckDenseOrSparseN(const TStr &attr) const
Return 1 if in Dense, 0 if in Sparse, -1 if neither.
Definition: network.h:2110
void AttrNameEI(const TInt &EId, TStrV &Names) const
Returns a vector of attr names for edge EId.
Definition: network.h:2586
static const T & Mx(const T &LVal, const T &RVal)
Definition: xmath.h:32
TVec< THash< TInt, TIntV > > VecOfIntHashVecsE
Definition: network.h:2068
int GetOutNId(const int &NodeN) const
Definition: network.h:3986
int GetSAttrNameE(const TInt &AttrId, TStr &NameX, TAttrType &AttrTypeX) const
Gets name and type for attribute with id AttrId.
Definition: network.cpp:3110
int GetSAttrVE(const TInt &EId, const TAttrType AttrType, TAttrPrV &AttrV) const
Gets a list of all sparse attributes of type AttrType for edge with id EId.
Definition: network.cpp:2089
int GetSAttrNameE(const TInt &AttrId, TStr &NameX, TAttrType &AttrTypeX) const
Gets name and type for attribute with id AttrId.
Definition: network.cpp:2113
TNode & GetNode(const int &NId)
Definition: network.h:2030
void StrAttrNameNI(const TInt &NId, TStrV &Names) const
Returns a vector of str attr names for node NId.
Definition: network.h:2569
bool IsFltVAttrDeletedN(const int &NId, const TStr &attr) const
Returns true if FltV attr exists for node NId and is an empty vector.
Definition: network.cpp:234
int AddSAttrDatE(const TInt &EId, const TStr &AttrName, const TInt &Val)
Adds Int sparse attribute with name AttrName to the given edge with id EId.
Definition: network.cpp:1991
void FltAttrNameNI(const TInt &NId, TStrV &Names) const
Returns a vector of int attr names for node NId.
Definition: network.h:2577
void DelNode(const int &NId)
Deletes node of ID NId from the network.
Definition: network.cpp:2186
void IntVAttrValueEI(const TInt &EId, TVec< TIntV > &Values) const
Returns a vector of attr values for edge EId.
Definition: network.h:2608
Directed network.
Definition: network.h:3966
TVec< THash< TInt, TFltV > > VecOfFltHashVecsN
Definition: network.h:2069
bool EdgeAttrIsFltDeleted(const int &EId, const TStrIntPrH::TIter &EdgeHI) const
Returns true if EId attr deleted for current edge flt attr iterator.
Definition: network.cpp:515
bool IsFltVAttrDeletedE(const int &EId, const TStr &attr) const
Returns true if FltV attr exists for edge EId and is an empty vector.
Definition: network.cpp:470
int AddSAttrDat(const TInt &Id, const TStr &AttrName, const TInt &Val)
Add Int attribute with name AttrName for the given id Id.
Definition: attr.cpp:2
TInt GetIntAttrDefaultE(const TStr &attribute) const
Gets Int edge attribute val. If not a proper attr, return default.
Definition: network.h:2044
int AddNodeUnchecked(int NId=-1)
Adds a node of ID NId to the network, noop if the node already exists.
Definition: network.cpp:547
TInt CheckDenseOrSparseE(const TStr &attr) const
Definition: network.h:2116
bool EdgeAttrIsDeleted(const int &EId, const TStrIntPrH::TIter &EdgeHI) const
Returns true if EId attr deleted for current edge attr iterator.
Definition: network.cpp:482
TFlt GetFltAttrDatE(const TEdgeI &EdgeI, const TStr &attr)
Gets the value of flt attr from the edge attr value vector.
Definition: network.h:2975
int AddIntVAttrDatE(const TEdgeI &EdgeI, const TIntV &value, const TStr &attr)
Attribute based add function for attr to IntV value.
Definition: network.h:2915
TIter BegI() const
Definition: hash.h:213
bool IsAttrDeletedE(const int &EId, const TStr &attr) const
Returns true if attr exists for edge EId and has default value.
Definition: network.cpp:453
bool IsFltAttrDeletedE(const int &EId, const TStr &attr) const
Returns true if Flt attr exists for edge NId and has default value.
Definition: network.cpp:478
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
const TNEANet * Graph
Definition: network.h:1796
bool EdgeAttrIsIntDeleted(const int &EId, const TStrIntPrH::TIter &EdgeHI) const
Returns true if EId attr deleted for current edge int attr iterator.
Definition: network.cpp:491
int AddSAttrDatN(const TInt &NId, const TStr &AttrName, const TInt &Val)
Adds Int sparse attribute with name AttrName to the given node with id NId.
Definition: network.cpp:2847
int GetSAttrName(const TInt &AttrId, TStr &NameX, TAttrType &AttrTypeX) const
Given the attribute id AttrId, get the attribute name.
Definition: attr.cpp:233
int AppendFltVAttrDatN(const TNodeI &NodeI, const TFlt &value, const TStr &attr)
Appends value onto the TFltV attribute for the given node.
Definition: network.h:2887
void FltVAttrNameEI(const TInt &EId, TStrV &Names) const
Returns a vector of flt attr names for edge EId.
Definition: network.h:2613
int DelAttrE(const TStr &attr)
Removes all the values for edge attr.
Definition: network.cpp:1769
void StrAttrValueNI(const TInt &NId, TStrV &Values) const
Returns a vector of attr values for node NId.
Definition: network.h:2573
int GetSAttrNameN(const TInt &AttrId, TStr &NameX, TAttrType &AttrTypeX) const
Gets name and type for attribute with id AttrId.
Definition: network.cpp:2464
TIntV GetIntVAttrDatE(const TEdgeI &EdgeI, const TStr &attr)
Gets the value of the intv attr from the edge attr value vector.
Definition: network.h:2978
void GenExt(TVal *_ValT, const TSizeTy &_Vals)
Constructs a vector of _Vals elements of memory array _ValT.
Definition: ds.h:534
TStrIntPrH KeyToIndexTypeE
Definition: network.h:2056
TStr GetStrAttrDatE(const TEdgeI &EdgeI, const TStr &attr)
Gets the value of str attr from the edge attr value vector.
Definition: network.h:2972
int GetNbrNId(const int &NodeN) const
Definition: network.h:3418
int AddNodeUnchecked(int NId=-1)
Adds a node of ID NId to the network, noop if the node already exists.
Definition: network.cpp:2633
int GetSAttrDatN(const TInt &NId, const TStr &AttrName, TInt &ValX) const
Gets Int sparse attribute with name AttrName from node with id NId.
Definition: network.cpp:2889
TAttr SAttrN
Definition: network.h:2072
Node iterator. Only forward iteration (operator++) is supported.
Definition: network.h:4003
void DelEdge(const int &EId)
Deletes an edge with edge ID EId from the graph.
Definition: network.cpp:828
THash< TStr, TFlt > FltDefaultsN
Definition: network.h:2062
bool IsEdge(const int &EId) const
Tests whether an edge with edge ID EId exists in the graph.
Definition: network.h:2804
Definition: fl.h:384
int AppendFltVAttrDatE(const TEdgeI &EdgeI, const TFlt &value, const TStr &attr)
Appends value onto the TFltV attribute for the given node.
Definition: network.h:2927
void IntAttrValueEI(const TInt &EId, TIntV &Values) const
Returns a vector of attr values for edge EId.
Definition: network.h:2598
static PUndirNet GetSmallGraph()
Returns a small network on 5 nodes and 5 edges.
Definition: network.cpp:2333
int AddIntAttrDatE(const TEdgeI &EdgeI, const TInt &value, const TStr &attr)
Attribute based add function for attr to Int value.
Definition: network.h:2898
TInt MxNId
Definition: network.h:2052
TIntV OutEIdV
Definition: network.h:1749
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
TIntV GetIntVAttrDatN(const TNodeI &NodeI, const TStr &attr) const
Gets the value of the intv attr from the node attr value vector.
Definition: network.h:2941
Node iterator. Only forward iteration (operator++) is supported.
Definition: network.h:1792
void AttrValueNI(const TInt &NId, TStrV &Values) const
Returns a vector of attr values for node NId.
Definition: network.h:2535
TVec< TIntV > VecOfIntVecsE
Definition: network.h:2063
TIter EndI() const
Definition: hash.h:218
void Load(TSIn &SIn)
Definition: ds.h:946
void IntAttrNameNI(const TInt &NId, TStrV &Names) const
Returns a vector of int attr names for node NId.
Definition: network.h:2540
int GetInDeg() const
Definition: network.h:3983
int AddNode(int NId=-1)
Adds a node of ID NId to the network.
Definition: network.cpp:535
int GetSAttrDatN(const TInt &NId, const TStr &AttrName, TInt &ValX) const
Gets Int sparse attribute with name AttrName from node with id NId.
Definition: network.cpp:1907
bool NodeAttrIsIntVDeleted(const int &NId, const TStrIntPrH::TIter &NodeHI) const
Returns true if NId attr deleted value for current node int vector attr iterator. ...
Definition: network.cpp:262
TVec< TVec< TFltV > > VecOfFltVecVecsN
Definition: network.h:2067
virtual void DelNode(const int &NId)
Deletes node of ID NId from the graph.
Definition: network.cpp:631
int GetSAttrNameN(const TInt &AttrId, TStr &NameX, TAttrType &AttrTypeX) const
Gets name and type for attribute with id AttrId.
Definition: network.cpp:2969
void DelSAttrId(const TInt &Id)
Delete all attributes for the given id Id.
Definition: attr.cpp:124
void LoadShM(TShMIn &ShMIn)
Load THash from shared memory file. Copying/Deleting Keys is illegal.
Definition: hash.h:157
const TKey & GetKey() const
Definition: hash.h:80
TSizeTy AddSorted(const TVal &Val, const bool &Asc=true, const TSizeTy &_MxVals=-1)
Adds element Val to a sorted vector.
Definition: ds.h:1117
void ErrNotify(const char *NotifyCStr)
Definition: bd.h:74
void Defrag()
Definition: hash.h:555
bool IsEdge(const int &SrcNId, const int &DstNId) const
Tests whether an edge between node IDs SrcNId and DstNId exists in the network.
Definition: network.cpp:2248
Definition: dt.h:1386
TVec< TStrV > VecOfStrVecsN
Definition: network.h:2064
bool IsIntAttrDeletedE(const int &EId, const TStr &attr) const
Returns true if Int attr exists for edge EId and has default value.
Definition: network.cpp:462
void GetEIdV(TIntV &EIdV) const
Gets a vector IDs of all edges in the graph.
Definition: network.cpp:894
void Dump(FILE *OutF=stdout) const
Print the graph in a human readable form to an output stream OutF.
Definition: network.cpp:963
bool IsFltAttrDeletedN(const int &NId, const TStr &attr) const
Returns true if Flt attr exists for node NId and has default value.
Definition: network.cpp:242
int AddFltAttrDatN(const TNodeI &NodeI, const TFlt &value, const TStr &attr)
Attribute based add function for attr to Flt value.
Definition: network.h:2868
bool NodeAttrIsFltDeleted(const int &NId, const TStrIntPrH::TIter &NodeHI) const
Returns true if NId attr deleted value for current node flt attr iterator.
Definition: network.cpp:286
void IntVAttrValueNI(const TInt &NId, TVec< TIntV > &Values) const
Returns a vector of attr values for node NId.
Definition: network.h:2554
THash< TInt, TEdge > EdgeH
Definition: network.h:2054
void GetAttrNNames(TStrV &IntAttrNames, TStrV &FltAttrNames, TStrV &StrAttrNames) const
Fills each of the vectors with the names of node attributes of the given type.
Definition: network.cpp:1803
int DelFromIntVAttrDatN(const TNodeI &NodeI, const TInt &value, const TStr &attr)
Deletes value from the TIntV attribute for the given node.
Definition: network.h:2883
int AddIntVAttrDatN(const TNodeI &NodeI, const TIntV &value, const TStr &attr)
Attribute based add function for attr to IntV value.
Definition: network.h:2871
bool IsOutNId(const int &NId) const
Tests whether the current node points to node with ID NId.
Definition: network.cpp:63
int GetAttrIndN(const TStr &attr)
Gets the index of the node attr value vector specified by attr.
Definition: network.cpp:1452
TStr GetStrAttrIndDatE(const TEdgeI &EdgeI, const int &index)
Gets the value of a string edge attr specified by edge iterator EdgeI and the attr index...
Definition: network.h:3001
void DelEdge(const int &SrcNId, const int &DstNId)
Deletes an edge between node IDs SrcNId and DstNId from the network.
Definition: network.cpp:2233
bool IsStrAttrDeletedE(const int &EId, const TStr &attr) const
Returns true if Str attr exists for edge NId and has default value.
Definition: network.cpp:474
void DelKey(const TKey &Key)
Definition: hash.h:404
TAttr SAttrE
Definition: network.h:2073
int GetId() const
Returns ID of the current node.
Definition: network.h:1807
static const int Mn
Definition: dt.h:1141
int GetSAttrVN(const TInt &NId, const TAttrType AttrType, TAttrPrV &AttrV) const
Gets a list of all sparse attributes of type AttrType for node with id NId.
Definition: network.cpp:1963
const TVal & GetVal(const TSizeTy &ValN) const
Returns a reference to the element at position ValN in the vector.
Definition: ds.h:649
int AddSAttrE(const TStr &Name, const TAttrType &AttrType, TInt &AttrId)
Adds mapping for sparse attribute with name Name and type AttrType.
Definition: network.cpp:2105
int DelSAttrDatN(const TInt &NId, const TStr &AttrName)
Deletes sparse attribute with name AttrName from node with id NId.
Definition: network.cpp:2426
TVec< TFltV > VecOfFltVecsE
Definition: network.h:2065
int GetFltKeyIdE(const int &EId)
Gets keyid for edge with id EId.
Definition: network.cpp:1407
int AddNodeUnchecked(int NId=-1)
Adds a node of ID NId to the network, noop if the node already exists.
Definition: network.cpp:2135
TStrIntPrH KeyToIndexTypeN
KeyToIndexType[N|E]: Key->(Type,Index).
Definition: network.h:2056
const TDat & GetDat() const
Definition: hash.h:81
TInt GetIntAttrDatE(const TEdgeI &EdgeI, const TStr &attr)
Gets the value of int attr from the edge attr value vector.
Definition: network.h:2969
int AddStrAttrDatN(const TNodeI &NodeI, const TStr &value, const TStr &attr)
Attribute based add function for attr to Str value.
Definition: network.h:2865
TVal * GetValVPt(const int &VId) const
Returns pointer to the first element of the vector with id VId.
Definition: ds.h:1731
int AddSAttrDatN(const TInt &NId, const TStr &AttrName, const TInt &Val)
Adds Int sparse attribute with name AttrName to the given node with id NId.
Definition: network.cpp:2342
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
void StrAttrValueEI(const TInt &EId, TStrV &Values) const
Returns a vector of attr values for node NId.
Definition: network.h:2627
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
Definition: gbase.h:41
bool IsEnd() const
Tests whether the iterator is pointing to the past-end element.
Definition: hash.h:78
void FltAttrValueEI(const TInt &EId, TFltV &Values) const
Returns a vector of attr values for node NId.
Definition: network.h:2635
int GetSAttrDatE(const TInt &EId, const TStr &AttrName, TInt &ValX) const
Gets Int sparse attribute with name AttrName from edge with id EId.
Definition: network.cpp:2033
TVec< TVec< TFltV > > VecOfFltVecVecsE
Definition: network.h:2067
int GetSAttrIdE(const TStr &Name, TInt &AttrIdX, TAttrType &AttrTypeX) const
Gets id and type for attribute with name Name.
Definition: network.cpp:2109
int GetVLen(const int &VId) const
Returns the number of elements in the vector with id VId.
Definition: ds.h:1729
int AddSAttrDatE(const int &SrcNId, const int &DstNId, const TStr &AttrName, const TInt &Val)
Adds Int sparse attribute with name AttrName to the given edge with ids SrcId and DstId...
Definition: network.cpp:2973
bool HasFlag(const TGraphFlag &Flag) const
Allows for run-time checking the type of the graph (see the TGraphFlag for flags).
Definition: network.cpp:50
int AddIntAttrN(const TStr &attr, TInt defaultValue=TInt::Mn)
Adds a new Int node attribute to the hashmap.
Definition: network.cpp:1547
TStr GetStrAttrDefaultE(const TStr &attribute) const
Gets Str edge attribute val. If not a proper attr, return default.
Definition: network.h:2046
int AddEdge(const int &SrcNId, const int &DstNId, int EId=-1)
Adds an edge with ID EId between node IDs SrcNId and DstNId to the graph.
Definition: network.cpp:753
int AddSAttrDatN(const TInt &NId, const TStr &AttrName, const TInt &Val)
Adds Int sparse attribute with name AttrName to the given node with id NId.
Definition: network.cpp:1865
THashIter NodeHI
Definition: network.h:3436
bool FNextKeyId(int &KeyId) const
Definition: hash.h:478
int GetSAttrDatE(const int &SrcNId, const int &DstNId, const TStr &AttrName, TInt &ValX) const
Gets Int sparse attribute with name AttrName from edge with ids SrcId and DstId.
Definition: network.cpp:3021
THash< TStr, TStr > StrDefaultsN
Definition: network.h:2061
void AttrNameNI(const TInt &NId, TStrV &Names) const
Returns a vector of attr names for node NId.
Definition: network.h:2531
int GetIdVSAttrE(const TStr &AttrName, TIntPrV &IdV) const
Gets a list of all edges that have a sparse attribute with name AttrName.
Definition: network.cpp:2596
int AddStrAttrE(const TStr &attr, TStr defaultValue=TStr::GetNullStr())
Adds a new Str edge attribute to the hashmap.
Definition: network.cpp:1697
TFlt GetFltAttrDatN(const TNodeI &NodeI, const TStr &attr)
Gets the value of flt attr from the node attr value vector.
Definition: network.h:2938
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the graph.
Definition: network.h:2812
bool IsOk(const bool &ThrowExcept=true) const
Checks the graph data structure for internal consistency.
Definition: network.cpp:910
TInt GetIntAttrIndDatN(const TNodeI &NodeI, const int &index)
Gets the value of an int node attr specified by node iterator NodeI and the attr index.
Definition: network.h:2954
void IntVAttrNameEI(const TInt &EId, TStrV &Names) const
Returns a vector of int attr names for edge EId.
Definition: network.h:2604
int FFirstKeyId() const
Definition: hash.h:278
void FltVAttrNameNI(const TInt &NId, TStrV &Names) const
Returns a vector of flt attr names for node NId.
Definition: network.h:2559
bool IsOk(const bool &ThrowExcept=true) const
Checks the network data structure for internal consistency.
Definition: network.cpp:2776
int GetIdVSAttr(const TInt &AttrId, const TAttrType Type, TIntV &IdV) const
Definition: attr.cpp:167
int GetSAttrNameN(const TInt &AttrId, TStr &NameX, TAttrType &AttrTypeX) const
Gets name and type for attribute with id AttrId.
Definition: network.cpp:1987
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the graph.
Definition: network.h:2814
int GetDstNId() const
Definition: network.h:1783
TFltV GetFltVAttrDatE(const TEdgeI &EdgeI, const TStr &attr)
Gets the value of the fltv attr from the edge attr value vector.
Definition: network.h:2982
void DelEdge(const int &SrcNId, const int &DstNId, const bool &IsDir=true)
Deletes an edge from node IDs SrcNId to DstNId from the network.
Definition: network.cpp:2728
bool HasFlag(const TGraphFlag &Flag) const
Allows for run-time checking the type of the network (see the TGraphFlag for flags).
Definition: network.cpp:2618
TStr GetStrAttrDefaultN(const TStr &attribute) const
Gets Str node attribute val. If not a proper attr, return default.
Definition: network.h:2040
bool EdgeAttrIsFltVDeleted(const int &EId, const TStrIntPrH::TIter &EdgeHI) const
Returns true if EId attr deleted for current edge flt vector attr iterator.
Definition: network.cpp:503
void DelNode(const int &NId)
Deletes node of ID NId from the network.
Definition: network.cpp:2684
TStr GetNodeAttrValue(const int &NId, const TStrIntPrH::TIter &NodeHI) const
Returns node attribute value, converted to Str type.
Definition: network.cpp:294
TVec< TVec< TIntV > > VecOfIntVecVecsE
Definition: network.h:2066
int GetSAttrDatE(const int &SrcNId, const int &DstNId, const TStr &AttrName, TInt &ValX) const
Gets Int sparse attribute with name AttrName from edge with ids SrcId and DstId.
Definition: network.cpp:2523
static TStr GetNullStr()
Definition: dt.cpp:1626
void AttrValueEI(const TInt &EId, TStrV &Values) const
Returns a vector of attr values for edge EId.
Definition: network.h:2590
void FltAttrValueNI(const TInt &NId, TFltV &Values) const
Returns a vector of attr values for node NId.
Definition: network.h:2581
void GetNIdV(TIntV &NIdV) const
Gets a vector IDs of all nodes in the network.
Definition: network.cpp:2265
Directed multigraph with node edge attributes.
Definition: network.h:1741
void LoadNetworkShM(TShMIn &ShMIn)
load network from shared memory for this network
Definition: network.cpp:2
TInt MxEId
Definition: network.h:2052
TSizeTy SearchBin(const TVal &Val) const
Returns the position of an element with value Val.
Definition: ds.h:1519
int AddSAttrDatE(const int &SrcNId, const int &DstNId, const TStr &AttrName, const TInt &Val)
Adds Int sparse attribute with name AttrName to the given edge with ids SrcId and DstId...
Definition: network.cpp:2475
TStr GetStr() const
Definition: dt.h:681
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge between node IDs SrcNId and DstNId to the network.
Definition: network.cpp:2213
int GetId() const
Definition: network.h:3412
void GetSAttrV(const TInt &Id, const TAttrType AttrType, TAttrPrV &AttrV) const
Get a list of all attributes of type AttrType for the given id Id.
Definition: attr.cpp:139
bool IsIntAttrE(const TStr &attr)
Checks if there is an edge attribute with name attr.
Definition: network.cpp:1855
static PNEANet GetSmallGraph()
Returns a small multigraph on 5 nodes and 6 edges.
Definition: network.cpp:1794
bool IsIntVAttrDeletedE(const int &EId, const TStr &attr) const
Returns true if IntV attr exists for edge EId and is an empty vector.
Definition: network.cpp:466
int AddSAttrN(const TStr &Name, const TAttrType &AttrType, TInt &AttrId)
Adds mapping for sparse attribute with name Name and type AttrType.
Definition: network.cpp:2961
Definition: dt.h:1137
int AddSAttrE(const TStr &Name, const TAttrType &AttrType, TInt &AttrId)
Adds mapping for sparse attribute with name Name and type AttrType.
Definition: network.cpp:2604
TEdge & GetEdge(const int &EId)
Definition: network.h:2032
int GetOutDeg() const
Definition: network.h:1759
void FltVAttrValueEI(const TInt &EId, TVec< TFltV > &Values) const
Returns a vector of attr values for edge EId.
Definition: network.h:2617
void Dump(FILE *OutF=stdout) const
Print the network in a human readable form to an output stream OutF.
Definition: network.cpp:2319
bool IsFltAttrE(const TStr &attr)
Checks if there is an edge attribute with name attr.
Definition: network.cpp:1850
int GetDeg() const
Definition: network.h:3413
int GetKeyId(const TKey &Key) const
Definition: hash.h:466
Edge iterator. Only forward iteration (operator++) is supported.
Definition: network.h:4050
void IntAttrValueNI(const TInt &NId, TIntV &Values) const
Returns a vector of attr values for node NId.
Definition: network.h:2544
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: network.h:2389
bool EdgeAttrIsIntVDeleted(const int &EId, const TStrIntPrH::TIter &EdgeHI) const
Returns true if EId attr deleted for current edge int vector attr iterator.
Definition: network.cpp:497
int AddStrAttrN(const TStr &attr, TStr defaultValue=TStr::GetNullStr())
Adds a new Str node attribute to the hashmap.
Definition: network.cpp:1602
int GetSAttrDat(const TInt &Id, const TStr &AttrName, TInt &ValX) const
Get Int attribute with name AttrName for the given id Id.
Definition: attr.cpp:54
void Ins(const TSizeTy &ValN, const TVal &Val)
Inserts new element Val before the element at position ValN.
Definition: ds.h:1180
void LoadShM(TShMIn &ShMIn)
Constructs the vector from a shared memory input.
Definition: ds.h:932
int GetId() const
Definition: network.h:3981
bool IsIntAttrDeletedN(const int &NId, const TStr &attr) const
Returns true if Int attr exists for node NId and has default value.
Definition: network.cpp:226
Edge iterator. Only forward iteration (operator++) is supported.
Definition: network.h:1867
int DelAttrDatE(const TEdgeI &EdgeI, const TStr &attr)
Deletes the edge attribute for NodeI.
Definition: network.h:3009
Definition: ds.h:32
int AddIntVAttrE(const TStr &attr, TBool UseDense=true)
Adds a new IntV edge attribute to the hashmap.
Definition: network.cpp:1661
TVec< THash< TInt, TFltV > > VecOfFltHashVecsE
Definition: network.h:2069
void FltAttrNameEI(const TInt &EId, TStrV &Names) const
Returns a vector of int attr names for node NId.
Definition: network.h:2631
int DelSAttrDatE(const int &SrcNId, const int &DstNId, const TStr &AttrName)
Deletes sparse attribute with name AttrName from edge with ids SrcId and DstId.
Definition: network.cpp:2571
TIntV InNIdV
Definition: network.h:3974
void FltVAttrValueNI(const TInt &NId, TVec< TFltV > &Values) const
Returns a vector of flt values for node NId.
Definition: network.h:2563
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge from node SrcNId to node DstNId to the network.
Definition: network.cpp:2713
bool NodeAttrIsFltVDeleted(const int &NId, const TStrIntPrH::TIter &NodeHI) const
Returns true if NId attr deleted value for current node int vector attr iterator. ...
Definition: network.cpp:270
THash< TStr, TStr > StrDefaultsE
Definition: network.h:2061
TVec< TFlt > TFltV
Definition: ds.h:1596
void StrAttrNameEI(const TInt &EId, TStrV &Names) const
Returns a vector of str attr names for node NId.
Definition: network.h:2623
TFlt GetFltAttrDefaultE(const TStr &attribute) const
Gets Flt edge attribute val. If not a proper attr, return default.
Definition: network.h:2048
int GetEdges() const
Returns the number of edges in the graph.
Definition: network.h:2785
void GetKeyV(TVec< TKey > &KeyV) const
Definition: hash.h:484
Undirected network.
Definition: network.h:3397
int GetSAttrIdN(const TStr &Name, TInt &AttrIdX, TAttrType &AttrTypeX) const
Gets id and type for attribute with name Name.
Definition: network.cpp:1983
int GetOutEId(const int &EdgeN) const
Definition: network.h:1761
bool EdgeAttrIsStrDeleted(const int &EId, const TStrIntPrH::TIter &EdgeHI) const
Returns true if EId attr deleted for current edge str attr iterator.
Definition: network.cpp:509
int GetEdges() const
Returns the number of edges in the network.
Definition: network.cpp:2203
Definition: dt.h:412
int AddStrAttrDatE(const TEdgeI &EdgeI, const TStr &value, const TStr &attr)
Attribute based add function for attr to Str value.
Definition: network.h:2903
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
bool IsSorted(const bool &Asc=true) const
Checks whether the vector is sorted in ascending (if Asc=true) or descending (if Asc=false) order...
Definition: ds.h:1323
void Pack()
Reduces vector capacity (frees memory) to match its size.
Definition: ds.h:1057
enum TGraphFlag_ TGraphFlag
Graph Flags, used for quick testing of graph types.
void Defrag(const bool &OnlyNodeLinks=false)
Defragments the network.
Definition: network.cpp:2767
void GetWeightOutEdgesV(TFltV &OutWeights, const TFltV &AttrVal)
Fills OutWeights with the outgoing weight from each node.
Definition: network.cpp:1842
int AddFltVAttrDatN(const TNodeI &NodeI, const TFltV &value, const TStr &attr)
Attribute based add function for attr to FltV value.
Definition: network.h:2874
int GetIdVSAttrN(const TStr &AttrName, TIntV &IdV) const
Gets a list of all nodes that have a sparse attribute with name AttrName.
Definition: network.cpp:1971
bool IsAttrDeletedN(const int &NId, const TStr &attr) const
Returns true if attr exists for node NId and has default value.
Definition: network.cpp:217
TVec< TFltV > VecOfFltVecsN
Definition: network.h:2065
int GetInDeg() const
Definition: network.h:1758
int GetSAttrVE(const int &SrcNId, const int &DstNId, const TAttrType AttrType, TAttrPrV &AttrV) const
Gets a list of all sparse attributes of type AttrType for edge with ids SrcId and DstId...
Definition: network.cpp:3085
#define EAssertR(Cond, MsgStr)
Definition: bd.h:283
int AddSAttrE(const TStr &Name, const TAttrType &AttrType, TInt &AttrId)
Adds mapping for sparse attribute with name Name and type AttrType.
Definition: network.cpp:3102
int GetSAttrDatN(const TInt &NId, const TStr &AttrName, TInt &ValX) const
Gets Int sparse attribute with name AttrName from node with id NId.
Definition: network.cpp:2384
int GetId() const
Definition: network.h:1756
TVec< THash< TInt, TIntV > > VecOfIntHashVecsN
Definition: network.h:2068
TVal1 Val1
Definition: ds.h:34
TVal2 Val2
Definition: ds.h:35
TVec< TInt > TIntV
Definition: ds.h:1594
int GetSAttrNameE(const TInt &AttrId, TStr &NameX, TAttrType &AttrTypeX) const
Gets name and type for attribute with id AttrId.
Definition: network.cpp:2612
TIntPr OrderEdgeNodes(const int &SrcNId, const int &DstNId) const
Definition: network.cpp:2468
TVec< TFlt > & GetFltAttrVecE(const TStr &attr)
Gets Vector for the Flt Attribute attr.
Definition: network.cpp:1403
int GetSAttrId(const TStr &Name, TInt &AttrIdX, TAttrType &AttrTypeX) const
Given the attribute name Name, get the attribute id.
Definition: attr.cpp:224
Definition: bd.h:196
THash< TStr, TInt > IntDefaultsN
Definition: network.h:2060
#define AssertR(Cond, Reason)
Definition: bd.h:258
TIntV OutNIdV
Definition: network.h:3974
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:2385
bool NodeAttrIsIntDeleted(const int &NId, const TStrIntPrH::TIter &NodeHI) const
Returns true if NId attr deleted value for current node int attr iterator.
Definition: network.cpp:254
bool IsIntVAttrDeletedN(const int &NId, const TStr &attr) const
Returns true if IntV attr exists for node NId and is an empty vector.
Definition: network.cpp:230
THash< TInt, TNode > NodeH
Definition: network.h:2053
int AppendIntVAttrDatE(const TEdgeI &EdgeI, const TInt &value, const TStr &attr)
Appends value onto the TIntV attribute for the given node.
Definition: network.h:2918
static PDirNet New()
Static constructor that returns a pointer to the network. Call: PDirNet Graph = TDirNet::New().
Definition: network.h:4107
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523
int AddNode(int NId=-1)
Adds a node of ID NId to the network.
Definition: network.cpp:2622
TIntV InEIdV
Definition: network.h:1749
TFlt GetFltAttrIndDatN(const TNodeI &NodeI, const int &index)
Gets the value of a float node attr specified by node iterator NodeI and the attr index...
Definition: network.h:2964
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: network.h:2387
TVec< TVec< TIntV > > VecOfIntVecVecsN
Definition: network.h:2066
int AddIntAttrE(const TStr &attr, TInt defaultValue=TInt::Mn)
Adds a new Int edge attribute to the hashmap.
Definition: network.cpp:1641
void Dump(FILE *OutF=stdout) const
Print the network in a human readable form to an output stream OutF.
Definition: network.cpp:2822
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: network.h:2391
int DelFromFltVAttrDatN(const TNodeI &NodeI, const TFlt &value, const TStr &attr)
Deletes value from the TFltV attribute for the given node.
Definition: network.h:2890
TFlt GetFltAttrDefaultN(const TStr &attribute) const
Gets Flt node attribute val. If not a proper attr, return default.
Definition: network.h:2042
int GetInEId(const int &EdgeN) const
Definition: network.h:1760
int GetIdVSAttrE(const TStr &AttrName, TIntPrV &IdV) const
Gets a list of all edges that have a sparse attribute with name AttrName.
Definition: network.cpp:3094
void IntAttrNameEI(const TInt &EId, TStrV &Names) const
Returns a vector of int attr names for edge EId.
Definition: network.h:2594
int GetIdVSAttrE(const TStr &AttrName, TIntV &IdV) const
Gets a list of all edges that have a sparse attribute with name AttrName.
Definition: network.cpp:2097
TInt GetIntAttrDefaultN(const TStr &attribute) const
Gets Int node attribute val. If not a proper attr, return default.
Definition: network.h:2038
void Load(TSIn &SIn)
Load attribute from input stream.
Definition: attr.h:25
int GetInNId(const int &NodeN) const
Definition: network.h:3985
int AddFltVAttrN(const TStr &attr, TBool UseDense=true)
Adds a new FltV node attribute to the hashmap.
Definition: network.cpp:1584
char * CStr()
Definition: dt.h:479
THashIter NodeHI
Definition: network.h:1795
bool IsKey(const TKey &Key) const
Definition: hash.h:258
int AddAttributes(const int NId)
Definition: network.cpp:559
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
int GetSAttrVN(const TInt &NId, const TAttrType AttrType, TAttrPrV &AttrV) const
Gets a list of all sparse attributes of type AttrType for node with id NId.
Definition: network.cpp:2440
Definition: dt.h:974
static PNEANet New()
Static cons returns pointer to graph. Ex: PNEANet Graph=TNEANet::New().
Definition: network.h:2226
int DelSAttrDatE(const TInt &EId, const TStr &AttrName)
Deletes sparse attribute with name AttrName from edge with id EId.
Definition: network.cpp:2075
int GetSAttrVE(const int &SrcNId, const int &DstNId, const TAttrType AttrType, TAttrPrV &AttrV) const
Gets a list of all sparse attributes of type AttrType for edge with ids SrcId and DstId...
Definition: network.cpp:2587
int GetIdVSAttrN(const TStr &AttrName, TIntV &IdV) const
Gets a list of all nodes that have a sparse attribute with name AttrName.
Definition: network.cpp:2953
TEdgeI GetEI(const int &EId) const
Not supported/implemented!
int DelSAttrDatE(const int &SrcNId, const int &DstNId, const TStr &AttrName)
Deletes sparse attribute with name AttrName from edge with ids SrcId and DstId.
Definition: network.cpp:3069
bool IsStrAttrE(const TStr &attr)
Checks if there is an edge attribute with name attr.
Definition: network.cpp:1860
int AddEdgeUnchecked(const int &SrcNId, const int &DstNId)
Adds an edge between node IDs SrcNId and DstNId to the network.
Definition: network.cpp:2224
int DelAttrDatN(const TNodeI &NodeI, const TStr &attr)
Deletes the node attribute for NodeI.
Definition: network.h:3006
int DelSAttrDatN(const TInt &NId, const TStr &AttrName)
Deletes sparse attribute with name AttrName from node with id NId.
Definition: network.cpp:1949
int GetOutDeg() const
Definition: network.h:3984
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
static PDirNet GetSmallGraph()
Returns a small network on 5 nodes and 6 edges.
Definition: network.cpp:2839
THashIter NodeHI
Definition: network.h:4006
TFltV GetFltVAttrDatN(const TNodeI &NodeI, const TStr &attr) const
Gets the value of the fltv attr from the node attr value vector.
Definition: network.h:2945
int AddIntAttrDatN(const TNodeI &NodeI, const TInt &value, const TStr &attr)
Attribute based add function for attr to Int value.
Definition: network.h:2862
TEdgeI GetEI(const int &EId) const
Not supported/implemented!
int GetIntAttrIndE(const TStr &attr)
Gets the index of the edge attr value vector specified by attr (same as GetAttrIndE for compatibility...
Definition: network.cpp:1493
TStr GetStrAttrIndDatN(const TNodeI &NodeI, const int &index)
Gets the value of a string node attr specified by node iterator NodeI and the attr index...
Definition: network.h:2959
int DelSAttrDatN(const TInt &NId, const TStr &AttrName)
Deletes sparse attribute with name AttrName from node with id NId.
Definition: network.cpp:2931
int AddFltVAttrE(const TStr &attr, TBool UseDense=true)
Adds a new FltV edge attribute to the hashmap.
Definition: network.cpp:1679
int AddEdgeUnchecked(const int &SrcNId, const int &DstNId)
Adds an edge from node SrcNId to node DstNId to the network.
Definition: network.cpp:2722
Node iterator. Only forward iteration (operator++) is supported.
Definition: network.h:3433
THash< TStr, TBool > KeyToDenseN
KeyToDense[N|E]: Key->(True if Vec, False if Hash)
Definition: network.h:2058
void Pack()
Definition: hash.h:289
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:252
int AddNode(int NId=-1)
Adds a node of ID NId to the network.
Definition: network.cpp:2124
void Defrag(const bool &OnlyNodeLinks=false)
Defragments the graph.
Definition: network.cpp:901
int AddSAttr(const TStr &Name, const TAttrType &AttrType, TInt &AttrIdX)
Adds a mapping for an attribute with name Name and type AttrType.
Definition: attr.cpp:210
int AddFltAttrE(const TStr &attr, TFlt defaultValue=TFlt::Mn)
Adds a new Flt edge attribute to the hashmap.
Definition: network.cpp:1716
static const double Mn
Definition: dt.h:1390
int AddSAttrN(const TStr &Name, const TAttrType &AttrType, TInt &AttrId)
Adds mapping for sparse attribute with name Name and type AttrType.
Definition: network.cpp:1979
TFlt GetFltAttrIndDatE(const TEdgeI &EdgeI, const int &index)
Gets the value of a float edge attr specified by edge iterator EdgeI and the attr index...
Definition: network.h:2996
TInt GetIntAttrIndDatE(const TEdgeI &EdgeI, const int &index)
Gets the value of an int edge attr specified by edge iterator EdgeI and the attr index.
Definition: network.h:2991
int AddSAttrN(const TStr &Name, const TAttrType &AttrType, TInt &AttrId)
Adds mapping for sparse attribute with name Name and type AttrType.
Definition: network.cpp:2456
TIter GetI(const TKey &Key) const
Definition: hash.h:220
int AddFltAttrN(const TStr &attr, TFlt defaultValue=TFlt::Mn)
Adds a new Flt node attribute to the hashmap.
Definition: network.cpp:1621