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