SNAP Library 2.4, User Reference  2015-05-11 19:40:56
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::StrAttrNameNI(const TInt& NId, TStrIntPrH::TIter NodeHI, TStrV& Names) const {
67  Names = TVec<TStr>();
68  while (!NodeHI.IsEnd()) {
69  if (NodeHI.GetDat().Val1 == StrType && !NodeAttrIsStrDeleted(NId, NodeHI)) {
70  Names.Add(NodeHI.GetKey());
71  }
72  NodeHI++;
73  }
74 }
75 
76 void TNEANet::StrAttrValueNI(const TInt& NId, TStrIntPrH::TIter NodeHI, TStrV& Values) const {
77  Values = TVec<TStr>();
78  while (!NodeHI.IsEnd()) {
79  if (NodeHI.GetDat().Val1 == StrType && !NodeAttrIsStrDeleted(NId, NodeHI)) {
80  TStr val = this->VecOfStrVecsN.GetVal(NodeHI.GetDat().Val2).GetVal(NodeH.GetKeyId(NId));
81  Values.Add(val);
82  }
83  NodeHI++;
84  }
85 }
86 
87 void TNEANet::FltAttrNameNI(const TInt& NId, TStrIntPrH::TIter NodeHI, TStrV& Names) const {
88  Names = TVec<TStr>();
89  while (!NodeHI.IsEnd()) {
90  if (NodeHI.GetDat().Val1 == FltType && !NodeAttrIsFltDeleted(NId, NodeHI)) {
91  Names.Add(NodeHI.GetKey());
92  }
93  NodeHI++;
94  }
95 }
96 
97 void TNEANet::FltAttrValueNI(const TInt& NId, TStrIntPrH::TIter NodeHI, TFltV& Values) const {
98  Values = TVec<TFlt>();
99  while (!NodeHI.IsEnd()) {
100  if (NodeHI.GetDat().Val1 == FltType && !NodeAttrIsFltDeleted(NId, NodeHI)) {
101  TFlt val = (this->VecOfFltVecsN.GetVal(NodeHI.GetDat().Val2).GetVal(NodeH.GetKeyId(NId)));
102  Values.Add(val);
103  }
104  NodeHI++;
105  }
106 }
107 
108 bool TNEANet::IsAttrDeletedN(const int& NId, const TStr& attr) const {
109  bool IntDel = IsIntAttrDeletedN(NId, attr);
110  bool StrDel = IsStrAttrDeletedN(NId, attr);
111  bool FltDel = IsFltAttrDeletedN(NId, attr);
112  return IntDel || StrDel || FltDel;
113 }
114 
115 bool TNEANet::IsIntAttrDeletedN(const int& NId, const TStr& attr) const {
116  return NodeAttrIsIntDeleted(NId, KeyToIndexTypeN.GetI(attr));
117 }
118 
119 bool TNEANet::IsStrAttrDeletedN(const int& NId, const TStr& attr) const {
120  return NodeAttrIsStrDeleted(NId, KeyToIndexTypeN.GetI(attr));
121 }
122 
123 bool TNEANet::IsFltAttrDeletedN(const int& NId, const TStr& attr) const {
124  return NodeAttrIsFltDeleted(NId, KeyToIndexTypeN.GetI(attr));
125 }
126 
127 bool TNEANet::NodeAttrIsDeleted(const int& NId, const TStrIntPrH::TIter& NodeHI) const {
128  bool IntDel = NodeAttrIsIntDeleted(NId, NodeHI);
129  bool StrDel = NodeAttrIsStrDeleted(NId, NodeHI);
130  bool FltDel = NodeAttrIsFltDeleted(NId, NodeHI);
131  return IntDel || StrDel || FltDel;
132 }
133 
134 bool TNEANet::NodeAttrIsIntDeleted(const int& NId, const TStrIntPrH::TIter& NodeHI) const {
135  if (NodeHI.GetDat().Val1 != IntType) {
136  return false;
137  }
138  return (GetIntAttrDefaultN(NodeHI.GetKey()) == this->VecOfIntVecsN.GetVal(
139  this->KeyToIndexTypeN.GetDat(NodeHI.GetKey()).Val2).GetVal(NodeH.GetKeyId(NId)));
140 }
141 
142 bool TNEANet::NodeAttrIsStrDeleted(const int& NId, const TStrIntPrH::TIter& NodeHI) const {
143  if (NodeHI.GetDat().Val1 != StrType) {
144  return false;
145  }
146  return (GetStrAttrDefaultN(NodeHI.GetKey()) == this->VecOfStrVecsN.GetVal(
147  this->KeyToIndexTypeN.GetDat(NodeHI.GetKey()).Val2).GetVal(NodeH.GetKeyId(NId)));
148 }
149 
150 bool TNEANet::NodeAttrIsFltDeleted(const int& NId, const TStrIntPrH::TIter& NodeHI) const {
151  if (NodeHI.GetDat().Val1 != FltType) {
152  return false;
153  }
154  return (GetFltAttrDefaultN(NodeHI.GetKey()) == this->VecOfFltVecsN.GetVal(
155  this->KeyToIndexTypeN.GetDat(NodeHI.GetKey()).Val2).GetVal(NodeH.GetKeyId(NId)));
156 }
157 
158 TStr TNEANet::GetNodeAttrValue(const int& NId, const TStrIntPrH::TIter& NodeHI) const {
159  if (NodeHI.GetDat().Val1 == IntType) {
160  return (this->VecOfIntVecsN.GetVal(
161  this->KeyToIndexTypeN.GetDat(NodeHI.GetKey()).Val2).GetVal(NodeH.GetKeyId(NId))).GetStr();
162  } else if(NodeHI.GetDat().Val1 == StrType) {
163  return this->VecOfStrVecsN.GetVal(
164  this->KeyToIndexTypeN.GetDat(NodeHI.GetKey()).Val2).GetVal(NodeH.GetKeyId(NId));
165  } else if (NodeHI.GetDat().Val1 == FltType) {
166  return (this->VecOfFltVecsN.GetVal(
167  this->KeyToIndexTypeN.GetDat(NodeHI.GetKey()).Val2).GetVal(NodeH.GetKeyId(NId))).GetStr();
168  }
169  return TStr::GetNullStr();
170 }
171 
172 void TNEANet::AttrNameEI(const TInt& EId, TStrIntPrH::TIter EdgeHI, TStrV& Names) const {
173  Names = TVec<TStr>();
174  while (!EdgeHI.IsEnd()) {
175  if (!EdgeAttrIsDeleted(EId, EdgeHI)) {
176  Names.Add(EdgeHI.GetKey());
177  }
178  EdgeHI++;
179  }
180 }
181 
182 void TNEANet::AttrValueEI(const TInt& EId, TStrIntPrH::TIter EdgeHI, TStrV& Values) const {
183  Values = TVec<TStr>();
184  while (!EdgeHI.IsEnd()) {
185  if (!EdgeAttrIsDeleted(EId, EdgeHI)) {
186  Values.Add(GetEdgeAttrValue(EId, EdgeHI));
187  }
188  EdgeHI++;
189  }
190 }
191 
192 void TNEANet::IntAttrNameEI(const TInt& EId, TStrIntPrH::TIter EdgeHI, TStrV& Names) const {
193  Names = TVec<TStr>();
194  while (!EdgeHI.IsEnd()) {
195  if (EdgeHI.GetDat().Val1 == IntType && !EdgeAttrIsIntDeleted(EId, EdgeHI)) {
196  Names.Add(EdgeHI.GetKey());
197  }
198  EdgeHI++;
199  }
200 }
201 
202 void TNEANet::IntAttrValueEI(const TInt& EId, TStrIntPrH::TIter EdgeHI, TIntV& Values) const {
203  Values = TVec<TInt>();
204  while (!EdgeHI.IsEnd()) {
205  if (EdgeHI.GetDat().Val1 == IntType && !EdgeAttrIsIntDeleted(EId, EdgeHI)) {
206  TInt val = (this->VecOfIntVecsE.GetVal(EdgeHI.GetDat().Val2).GetVal(EId));
207  Values.Add(val);
208  }
209  EdgeHI++;
210  }
211 }
212 
213 void TNEANet::StrAttrNameEI(const TInt& EId, TStrIntPrH::TIter EdgeHI, TStrV& Names) const {
214  Names = TVec<TStr>();
215  while (!EdgeHI.IsEnd()) {
216  if (EdgeHI.GetDat().Val1 == StrType && !EdgeAttrIsStrDeleted(EId, EdgeHI)) {
217  Names.Add(EdgeHI.GetKey());
218  }
219  EdgeHI++;
220  }
221 }
222 
223 void TNEANet::StrAttrValueEI(const TInt& EId, TStrIntPrH::TIter EdgeHI, TStrV& Values) const {
224  Values = TVec<TStr>();
225  while (!EdgeHI.IsEnd()) {
226  if (EdgeHI.GetDat().Val1 == StrType && !EdgeAttrIsStrDeleted(EId, EdgeHI)) {
227  TStr val = this->VecOfStrVecsE.GetVal(EdgeHI.GetDat().Val2).GetVal(EId);
228  Values.Add(val);
229  }
230  EdgeHI++;
231  }
232 }
233 
234 void TNEANet::FltAttrNameEI(const TInt& EId, TStrIntPrH::TIter EdgeHI, TStrV& Names) const {
235  Names = TVec<TStr>();
236  while (!EdgeHI.IsEnd()) {
237  if (EdgeHI.GetDat().Val1 == FltType && !EdgeAttrIsFltDeleted(EId, EdgeHI)) {
238  Names.Add(EdgeHI.GetKey());
239  }
240  EdgeHI++;
241  }
242 }
243 
244 void TNEANet::FltAttrValueEI(const TInt& EId, TStrIntPrH::TIter EdgeHI, TFltV& Values) const {
245  Values = TVec<TFlt>();
246  while (!EdgeHI.IsEnd()) {
247  if (EdgeHI.GetDat().Val1 == FltType && !EdgeAttrIsFltDeleted(EId, EdgeHI)) {
248  TFlt val = (this->VecOfFltVecsE.GetVal(EdgeHI.GetDat().Val2).GetVal(EId));
249  Values.Add(val);
250  }
251  EdgeHI++;
252  }
253 }
254 
255 bool TNEANet::IsAttrDeletedE(const int& EId, const TStr& attr) const {
256  bool IntDel = IsIntAttrDeletedE(EId, attr);
257  bool StrDel = IsStrAttrDeletedE(EId, attr);
258  bool FltDel = IsFltAttrDeletedE(EId, attr);
259  return IntDel || StrDel || FltDel;
260 }
261 
262 bool TNEANet::IsIntAttrDeletedE(const int& EId, const TStr& attr) const {
263  return EdgeAttrIsIntDeleted(EId, KeyToIndexTypeE.GetI(attr));
264 }
265 
266 bool TNEANet::IsStrAttrDeletedE(const int& EId, const TStr& attr) const {
267  return EdgeAttrIsStrDeleted(EId, KeyToIndexTypeE.GetI(attr));
268 }
269 
270 bool TNEANet::IsFltAttrDeletedE(const int& EId, const TStr& attr) const {
271  return EdgeAttrIsFltDeleted(EId, KeyToIndexTypeE.GetI(attr));
272 }
273 
274 bool TNEANet::EdgeAttrIsDeleted(const int& EId, const TStrIntPrH::TIter& EdgeHI) const {
275  bool IntDel = EdgeAttrIsIntDeleted(EId, EdgeHI);
276  bool StrDel = EdgeAttrIsStrDeleted(EId, EdgeHI);
277  bool FltDel = EdgeAttrIsFltDeleted(EId, EdgeHI);
278  return IntDel || StrDel || FltDel;
279 }
280 
281 bool TNEANet::EdgeAttrIsIntDeleted(const int& EId, const TStrIntPrH::TIter& EdgeHI) const {
282  return (EdgeHI.GetDat().Val1 == IntType &&
283  GetIntAttrDefaultE(EdgeHI.GetKey()) == this->VecOfIntVecsE.GetVal(
284  this->KeyToIndexTypeE.GetDat(EdgeHI.GetKey()).Val2).GetVal(EdgeH.GetKeyId(EId)));
285 }
286 
287 bool TNEANet::EdgeAttrIsStrDeleted(const int& EId, const TStrIntPrH::TIter& EdgeHI) const {
288  return (EdgeHI.GetDat().Val1 == StrType &&
289  GetStrAttrDefaultE(EdgeHI.GetKey()) == this->VecOfStrVecsE.GetVal(
290  this->KeyToIndexTypeE.GetDat(EdgeHI.GetKey()).Val2).GetVal(EdgeH.GetKeyId(EId)));
291 }
292 
293 bool TNEANet::EdgeAttrIsFltDeleted(const int& EId, const TStrIntPrH::TIter& EdgeHI) const {
294  return (EdgeHI.GetDat().Val1 == FltType &&
295  GetFltAttrDefaultE(EdgeHI.GetKey()) == this->VecOfFltVecsE.GetVal(
296  this->KeyToIndexTypeE.GetDat(EdgeHI.GetKey()).Val2).GetVal(EdgeH.GetKeyId(EId)));
297 }
298 
299 TStr TNEANet::GetEdgeAttrValue(const int& EId, const TStrIntPrH::TIter& EdgeHI) const {
300  if (EdgeHI.GetDat().Val1 == IntType) {
301  return (this->VecOfIntVecsE.GetVal(
302  this->KeyToIndexTypeE.GetDat(EdgeHI.GetKey()).Val2).GetVal(EdgeH.GetKeyId(EId))).GetStr();
303  } else if(EdgeHI.GetDat().Val1 == StrType) {
304  return this->VecOfStrVecsE.GetVal(
305  this->KeyToIndexTypeE.GetDat(EdgeHI.GetKey()).Val2).GetVal(EdgeH.GetKeyId(EId));
306  } else if (EdgeHI.GetDat().Val1 == FltType) {
307  return (this->VecOfFltVecsE.GetVal(
308  this->KeyToIndexTypeE.GetDat(EdgeHI.GetKey()).Val2).GetVal(EdgeH.GetKeyId(EId))).GetStr();
309  }
310  return TStr::GetNullStr();
311 }
312 
313 int TNEANet::AddNode(int NId) {
314  int i;
315  if (NId == -1) {
316  NId = MxNId; MxNId++;
317  } else {
318  IAssertR(!IsNode(NId), TStr::Fmt("NodeId %d already exists", NId));
319  MxNId = TMath::Mx(NId+1, MxNId());
320  }
321  // update attribute columns
322  NodeH.AddDat(NId, TNode(NId));
323  for (i = 0; i < VecOfIntVecsN.Len(); i++) {
324  TVec<TInt>& IntVec = VecOfIntVecsN[i];
325  IntVec.Ins(NodeH.GetKeyId(NId), TInt::Mn);
326  }
327  TVec<TStr> DefIntVec = TVec<TStr>();
328  IntDefaultsN.GetKeyV(DefIntVec);
329  for (i = 0; i < DefIntVec.Len(); i++) {
330  TStr attr = DefIntVec[i];
331  TVec<TInt>& IntVec = VecOfIntVecsN[KeyToIndexTypeN.GetDat(DefIntVec[i]).Val2];
332  IntVec[NodeH.GetKeyId(NId)] = GetIntAttrDefaultN(attr);
333  }
334  for (i = 0; i < VecOfStrVecsN.Len(); i++) {
335  TVec<TStr>& StrVec = VecOfStrVecsN[i];
336  StrVec.Ins(NodeH.GetKeyId(NId), TStr::GetNullStr());
337  }
338  TVec<TStr> DefStrVec = TVec<TStr>();
339  IntDefaultsN.GetKeyV(DefStrVec);
340  for (i = 0; i < DefStrVec.Len(); i++) {
341  TStr attr = DefStrVec[i];
342  TVec<TStr>& StrVec = VecOfStrVecsN[KeyToIndexTypeN.GetDat(DefStrVec[i]).Val2];
343  StrVec[NodeH.GetKeyId(NId)] = GetStrAttrDefaultN(attr);
344  }
345  for (i = 0; i < VecOfFltVecsN.Len(); i++) {
346  TVec<TFlt>& FltVec = VecOfFltVecsN[i];
347  FltVec.Ins(NodeH.GetKeyId(NId), TFlt::Mn);
348  }
349  TVec<TStr> DefFltVec = TVec<TStr>();
350  FltDefaultsN.GetKeyV(DefFltVec);
351  for (i = 0; i < DefFltVec.Len(); i++) {
352  TStr attr = DefFltVec[i];
353  TVec<TFlt>& FltVec = VecOfFltVecsN[KeyToIndexTypeN.GetDat(DefFltVec[i]).Val2];
354  FltVec[NodeH.GetKeyId(NId)] = GetFltAttrDefaultN(attr);
355  }
356  return NId;
357 }
358 
359 void TNEANet::DelNode(const int& NId) {
360  int i;
361 
362  const TNode& Node = GetNode(NId);
363  for (int out = 0; out < Node.GetOutDeg(); out++) {
364  const int EId = Node.GetOutEId(out);
365  const TEdge& Edge = GetEdge(EId);
366  IAssert(Edge.GetSrcNId() == NId);
367  GetNode(Edge.GetDstNId()).InEIdV.DelIfIn(EId);
368  // Delete from Edge Attributes
369  for (i = 0; i < VecOfIntVecsE.Len(); i++) {
370  TVec<TInt>& IntVec = VecOfIntVecsE[i];
371  IntVec[EdgeH.GetKeyId(EId)] = TInt::Mn;
372  }
373  for (i = 0; i < VecOfStrVecsE.Len(); i++) {
374  TVec<TStr>& StrVec = VecOfStrVecsE[i];
375  StrVec[EdgeH.GetKeyId(EId)] = TStr::GetNullStr();
376  }
377  for (i = 0; i < VecOfFltVecsE.Len(); i++) {
378  TVec<TFlt>& FltVec = VecOfFltVecsE[i];
379  FltVec[EdgeH.GetKeyId(EId)] = TFlt::Mn;
380  }
381  EdgeH.DelKey(EId);
382  }
383  for (int in = 0; in < Node.GetInDeg(); in++) {
384  const int EId = Node.GetInEId(in);
385  const TEdge& Edge = GetEdge(EId);
386  IAssert(Edge.GetDstNId() == NId);
387  GetNode(Edge.GetSrcNId()).OutEIdV.DelIfIn(EId);
388  // Delete from Edge Attributes
389  for (i = 0; i < VecOfIntVecsE.Len(); i++) {
390  TVec<TInt>& IntVec = VecOfIntVecsE[i];
391  IntVec[EdgeH.GetKeyId(EId)] = TInt::Mn;
392  }
393  for (i = 0; i < VecOfStrVecsE.Len(); i++) {
394  TVec<TStr>& StrVec = VecOfStrVecsE[i];
395  StrVec[EdgeH.GetKeyId(EId)] = TStr::GetNullStr();
396  }
397  for (i = 0; i < VecOfFltVecsE.Len(); i++) {
398  TVec<TFlt>& FltVec = VecOfFltVecsE[i];
399  FltVec[EdgeH.GetKeyId(EId)] = TFlt::Mn;
400  }
401  EdgeH.DelKey(EId);
402  }
403 
404  for (i = 0; i < VecOfIntVecsN.Len(); i++) {
405  TVec<TInt>& IntVec = VecOfIntVecsN[i];
406  IntVec[NodeH.GetKeyId(NId)] = TInt::Mn;
407  }
408  for (i = 0; i < VecOfStrVecsN.Len(); i++) {
409  TVec<TStr>& StrVec = VecOfStrVecsN[i];
410  StrVec[NodeH.GetKeyId(NId)] = TStr::GetNullStr();
411  }
412  for (i = 0; i < VecOfFltVecsN.Len(); i++) {
413  TVec<TFlt>& FltVec = VecOfFltVecsN[i];
414  FltVec[NodeH.GetKeyId(NId)] = TFlt::Mn;
415  }
416  NodeH.DelKey(NId);
417 }
418 
419 int TNEANet::AddEdge(const int& SrcNId, const int& DstNId, int EId) {
420  int i;
421 
422  if (EId == -1) { EId = MxEId; MxEId++; }
423  else { MxEId = TMath::Mx(EId+1, MxEId()); }
424  IAssertR(!IsEdge(EId), TStr::Fmt("EdgeId %d already exists", EId));
425  IAssertR(IsNode(SrcNId) && IsNode(DstNId), TStr::Fmt("%d or %d not a node.", SrcNId, DstNId).CStr());
426  EdgeH.AddDat(EId, TEdge(EId, SrcNId, DstNId));
427  GetNode(SrcNId).OutEIdV.AddSorted(EId);
428  GetNode(DstNId).InEIdV.AddSorted(EId);
429 
430  // update attribute columns
431  for (i = 0; i < VecOfIntVecsE.Len(); i++) {
432  TVec<TInt>& IntVec = VecOfIntVecsE[i];
433  IntVec.Ins(EdgeH.GetKeyId(EId), TInt::Mn);
434  }
435  TVec<TStr> DefIntVec = TVec<TStr>();
436  IntDefaultsE.GetKeyV(DefIntVec);
437  for (i = 0; i < DefIntVec.Len(); i++) {
438  TStr attr = DefIntVec[i];
439  TVec<TInt>& IntVec = VecOfIntVecsE[KeyToIndexTypeE.GetDat(DefIntVec[i]).Val2];
440  IntVec[EdgeH.GetKeyId(EId)] = GetIntAttrDefaultE(attr);
441  }
442 
443  for (i = 0; i < VecOfStrVecsE.Len(); i++) {
444  TVec<TStr>& StrVec = VecOfStrVecsE[i];
445  StrVec.Ins(EdgeH.GetKeyId(EId), TStr::GetNullStr());
446  }
447  TVec<TStr> DefStrVec = TVec<TStr>();
448  IntDefaultsE.GetKeyV(DefStrVec);
449  for (i = 0; i < DefStrVec.Len(); i++) {
450  TStr attr = DefStrVec[i];
451  TVec<TStr>& StrVec = VecOfStrVecsE[KeyToIndexTypeE.GetDat(DefStrVec[i]).Val2];
452  StrVec[EdgeH.GetKeyId(EId)] = GetStrAttrDefaultE(attr);
453  }
454 
455  for (i = 0; i < VecOfFltVecsE.Len(); i++) {
456  TVec<TFlt>& FltVec = VecOfFltVecsE[i];
457  FltVec.Ins(EdgeH.GetKeyId(EId), TFlt::Mn);
458  }
459  TVec<TStr> DefFltVec = TVec<TStr>();
460  FltDefaultsE.GetKeyV(DefFltVec);
461  for (i = 0; i < DefFltVec.Len(); i++) {
462  TStr attr = DefFltVec[i];
463  TVec<TFlt>& FltVec = VecOfFltVecsE[KeyToIndexTypeE.GetDat(DefFltVec[i]).Val2];
464  FltVec[NodeH.GetKeyId(EId)] = GetFltAttrDefaultE(attr);
465  }
466  return EId;
467 }
468 
469 void TNEANet::DelEdge(const int& EId) {
470  int i;
471 
472  IAssert(IsEdge(EId));
473  const int SrcNId = GetEdge(EId).GetSrcNId();
474  const int DstNId = GetEdge(EId).GetDstNId();
475  GetNode(SrcNId).OutEIdV.DelIfIn(EId);
476  GetNode(DstNId).InEIdV.DelIfIn(EId);
477  EdgeH.DelKey(EId);
478 
479  for (i = 0; i < VecOfIntVecsE.Len(); i++) {
480  TVec<TInt>& IntVec = VecOfIntVecsE[i];
481  IntVec.Ins(EdgeH.GetKeyId(EId), TInt::Mn);
482  }
483  for (i = 0; i < VecOfStrVecsE.Len(); i++) {
484  TVec<TStr>& StrVec = VecOfStrVecsE[i];
485  StrVec.Ins(EdgeH.GetKeyId(EId), TStr::GetNullStr());
486  }
487  for (i = 0; i < VecOfFltVecsE.Len(); i++) {
488  TVec<TFlt>& FltVec = VecOfFltVecsE[i];
489  FltVec.Ins(EdgeH.GetKeyId(EId), TFlt::Mn);
490  }
491 }
492 
493 // delete all edges between the two nodes
494 void TNEANet::DelEdge(const int& SrcNId, const int& DstNId, const bool& IsDir) {
495  int EId = 0;
496  bool Edge = IsEdge(SrcNId, DstNId, EId, IsDir);
497  IAssert(Edge); // there is at least one edge
498  while (Edge) {
499  DelEdge(EId);
500  Edge = IsEdge(SrcNId, DstNId, EId, IsDir);
501  }
502 }
503 
504 bool TNEANet::IsEdge(const int& SrcNId, const int& DstNId, int& EId, const bool& IsDir) const {
505  const TNode& SrcNode = GetNode(SrcNId);
506  for (int edge = 0; edge < SrcNode.GetOutDeg(); edge++) {
507  const TEdge& Edge = GetEdge(SrcNode.GetOutEId(edge));
508  if (DstNId == Edge.GetDstNId()) {
509  EId = Edge.GetId();
510  return true;
511  }
512  }
513  if (! IsDir) {
514  for (int edge = 0; edge < SrcNode.GetInDeg(); edge++) {
515  const TEdge& Edge = GetEdge(SrcNode.GetInEId(edge));
516  if (DstNId == Edge.GetSrcNId()) {
517  EId = Edge.GetId();
518  return true;
519  }
520  }
521  }
522  return false;
523 }
524 
525 void TNEANet::GetNIdV(TIntV& NIdV) const {
526  NIdV.Gen(GetNodes(), 0);
527  for (int N=NodeH.FFirstKeyId(); NodeH.FNextKeyId(N); ) {
528  NIdV.Add(NodeH.GetKey(N));
529  }
530 }
531 
532 void TNEANet::GetEIdV(TIntV& EIdV) const {
533  EIdV.Gen(GetEdges(), 0);
534  for (int E=EdgeH.FFirstKeyId(); EdgeH.FNextKeyId(E); ) {
535  EIdV.Add(EdgeH.GetKey(E));
536  }
537 }
538 
539 void TNEANet::Defrag(const bool& OnlyNodeLinks) {
540  for (int kid = NodeH.FFirstKeyId(); NodeH.FNextKeyId(kid); ) {
541  TNode& Node = NodeH[kid];
542  Node.InEIdV.Pack(); Node.OutEIdV.Pack();
543  }
544  if (! OnlyNodeLinks && ! NodeH.IsKeyIdEqKeyN()) { NodeH.Defrag(); }
545  if (! OnlyNodeLinks && ! EdgeH.IsKeyIdEqKeyN()) { EdgeH.Defrag(); }
546 }
547 
548 bool TNEANet::IsOk(const bool& ThrowExcept) const {
549  bool RetVal = true;
550  for (int N = NodeH.FFirstKeyId(); NodeH.FNextKeyId(N); ) {
551  const TNode& Node = NodeH[N];
552  if (! Node.OutEIdV.IsSorted()) {
553  const TStr Msg = TStr::Fmt("Out-edge list of node %d is not sorted.", Node.GetId());
554  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
555  }
556  if (! Node.InEIdV.IsSorted()) {
557  const TStr Msg = TStr::Fmt("In-edge list of node %d is not sorted.", Node.GetId());
558  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
559  }
560  // check out-edge ids
561  int prevEId = -1;
562  for (int e = 0; e < Node.GetOutDeg(); e++) {
563  if (! IsEdge(Node.GetOutEId(e))) {
564  const TStr Msg = TStr::Fmt("Out-edge id %d of node %d does not exist.", Node.GetOutEId(e), Node.GetId());
565  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
566  }
567  if (e > 0 && prevEId == Node.GetOutEId(e)) {
568  const TStr Msg = TStr::Fmt("Node %d has duplidate out-edge id %d.", Node.GetId(), Node.GetOutEId(e));
569  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
570  }
571  prevEId = Node.GetOutEId(e);
572  }
573  // check in-edge ids
574  prevEId = -1;
575  for (int e = 0; e < Node.GetInDeg(); e++) {
576  if (! IsEdge(Node.GetInEId(e))) {
577  const TStr Msg = TStr::Fmt("Out-edge id %d of node %d does not exist.", Node.GetInEId(e), Node.GetId());
578  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
579  }
580  if (e > 0 && prevEId == Node.GetInEId(e)) {
581  const TStr Msg = TStr::Fmt("Node %d has duplidate out-edge id %d.", Node.GetId(), Node.GetInEId(e));
582  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
583  }
584  prevEId = Node.GetInEId(e);
585  }
586  }
587  for (int E = EdgeH.FFirstKeyId(); EdgeH.FNextKeyId(E); ) {
588  const TEdge& Edge = EdgeH[E];
589  if (! IsNode(Edge.GetSrcNId())) {
590  const TStr Msg = TStr::Fmt("Edge %d source node %d does not exist.", Edge.GetId(), Edge.GetSrcNId());
591  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
592  }
593  if (! IsNode(Edge.GetDstNId())) {
594  const TStr Msg = TStr::Fmt("Edge %d destination node %d does not exist.", Edge.GetId(), Edge.GetDstNId());
595  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
596  }
597  }
598  return RetVal;
599 }
600 
601 void TNEANet::Dump(FILE *OutF) const {
602  const int NodePlaces = (int) ceil(log10((double) GetNodes()));
603  const int EdgePlaces = (int) ceil(log10((double) GetEdges()));
604  fprintf(OutF, "-------------------------------------------------\nDirected Node-Edge Network: nodes: %d, edges: %d\n", GetNodes(), GetEdges());
605  for (TNodeI NodeI = BegNI(); NodeI < EndNI(); NodeI++) {
606  fprintf(OutF, " %*d]\n", NodePlaces, NodeI.GetId());
607  // load node attributes
608  TIntV IntAttrN;
609  IntAttrValueNI(NodeI.GetId(), IntAttrN);
610  fprintf(OutF, " nai[%d]", IntAttrN.Len());
611  for (int i = 0; i < IntAttrN.Len(); i++) {
612  fprintf(OutF, " %*i", NodePlaces, IntAttrN[i]()); }
613  TStrV StrAttrN;
614  StrAttrValueNI(NodeI.GetId(), StrAttrN);
615  fprintf(OutF, " nas[%d]", StrAttrN.Len());
616  for (int i = 0; i < StrAttrN.Len(); i++) {
617  fprintf(OutF, " %*s", NodePlaces, StrAttrN[i]()); }
618  TFltV FltAttrN;
619  FltAttrValueNI(NodeI.GetId(), FltAttrN);
620  fprintf(OutF, " naf[%d]", FltAttrN.Len());
621  for (int i = 0; i < FltAttrN.Len(); i++) {
622  fprintf(OutF, " %*f", NodePlaces, FltAttrN[i]()); }
623 
624  fprintf(OutF, " in[%d]", NodeI.GetInDeg());
625  for (int edge = 0; edge < NodeI.GetInDeg(); edge++) {
626  fprintf(OutF, " %*d", EdgePlaces, NodeI.GetInEId(edge)); }
627  fprintf(OutF, "\n");
628  fprintf(OutF, " out[%d]", NodeI.GetOutDeg());
629  for (int edge = 0; edge < NodeI.GetOutDeg(); edge++) {
630  fprintf(OutF, " %*d", EdgePlaces, NodeI.GetOutEId(edge)); }
631  fprintf(OutF, "\n");
632  }
633  for (TEdgeI EdgeI = BegEI(); EdgeI < EndEI(); EdgeI++) {
634  fprintf(OutF, " %*d] %*d -> %*d\n", EdgePlaces, EdgeI.GetId(), NodePlaces, EdgeI.GetSrcNId(), NodePlaces, EdgeI.GetDstNId());
635 
636  // load edge attributes
637  TIntV IntAttrE;
638  IntAttrValueEI(EdgeI.GetId(), IntAttrE);
639  fprintf(OutF, " eai[%d]", IntAttrE.Len());
640  for (int i = 0; i < IntAttrE.Len(); i++) {
641  fprintf(OutF, " %*i", EdgePlaces, IntAttrE[i]()); }
642  TStrV StrAttrE;
643  StrAttrValueEI(EdgeI.GetId(), StrAttrE);
644  fprintf(OutF, " eas[%d]", StrAttrE.Len());
645  for (int i = 0; i < StrAttrE.Len(); i++) {
646  fprintf(OutF, " %*s", EdgePlaces, StrAttrE[i]()); }
647  TFltV FltAttrE;
648  FltAttrValueEI(EdgeI.GetId(), FltAttrE);
649  fprintf(OutF, " eaf[%d]", FltAttrE.Len());
650  for (int i = 0; i < FltAttrE.Len(); i++) {
651  fprintf(OutF, " %*f", EdgePlaces, FltAttrE[i]()); }
652  }
653  fprintf(OutF, "\n");
654 }
655 
656 // Attribute related function
657 
658 int TNEANet::AddIntAttrDatN(const int& NId, const TInt& value, const TStr& attr) {
659  int i;
660  TInt CurrLen;
661  if (!IsNode(NId)) {
662  // AddNode(NId);
663  return -1;
664  }
665  if (KeyToIndexTypeN.IsKey(attr)) {
667  NewVec[NodeH.GetKeyId(NId)] = value;
668  } else {
669  CurrLen = VecOfIntVecsN.Len();
670  KeyToIndexTypeN.AddDat(attr, TIntPr(IntType, CurrLen));
671  TVec<TInt> NewVec = TVec<TInt>();
672  for (i = 0; i < MxNId; i++) {
673  NewVec.Ins(i, GetIntAttrDefaultN(attr));
674  }
675  NewVec[NodeH.GetKeyId(NId)] = value;
676  VecOfIntVecsN.Add(NewVec);
677  }
678  return 0;
679 }
680 
681 int TNEANet::AddStrAttrDatN(const int& NId, const TStr& value, const TStr& attr) {
682  int i;
683  TInt CurrLen;
684  if (!IsNode(NId)) {
685  // AddNode(NId);
686  return -1;
687  }
688  if (KeyToIndexTypeN.IsKey(attr)) {
690  NewVec[NodeH.GetKeyId(NId)] = value;
691  } else {
692  CurrLen = VecOfStrVecsN.Len();
693  KeyToIndexTypeN.AddDat(attr, TIntPr(StrType, CurrLen));
694  TVec<TStr> NewVec = TVec<TStr>();
695  for (i = 0; i < MxNId; i++) {
696  NewVec.Ins(i, GetStrAttrDefaultN(attr));
697  }
698  NewVec[NodeH.GetKeyId(NId)] = value;
699  VecOfStrVecsN.Add(NewVec);
700  }
701  return 0;
702 }
703 
704 int TNEANet::AddFltAttrDatN(const int& NId, const TFlt& value, const TStr& attr) {
705  int i;
706  TInt CurrLen;
707 
708  if (!IsNode(NId)) {
709  // AddNode(NId);
710  return -1;
711  }
712  if (KeyToIndexTypeN.IsKey(attr)) {
714  NewVec[NodeH.GetKeyId(NId)] = value;
715  } else {
716  CurrLen = VecOfFltVecsN.Len();
717  KeyToIndexTypeN.AddDat(attr, TIntPr(FltType, CurrLen));
718  TVec<TFlt> NewVec = TVec<TFlt>();
719  for (i = 0; i < MxNId; i++) {
720  NewVec.Ins(i, GetFltAttrDefaultN(attr));
721  }
722  NewVec[NodeH.GetKeyId(NId)] = value;
723  VecOfFltVecsN.Add(NewVec);
724  }
725  return 0;
726 }
727 
728 int TNEANet::AddIntAttrDatE(const int& EId, const TInt& value, const TStr& attr) {
729  int i;
730  TInt CurrLen;
731  if (!IsEdge(EId)) {
732  //AddEdge(EId);
733  return -1;
734  }
735  if (KeyToIndexTypeE.IsKey(attr)) {
737  NewVec[EdgeH.GetKeyId(EId)] = value;
738  } else {
739  CurrLen = VecOfIntVecsE.Len();
740  KeyToIndexTypeE.AddDat(attr, TIntPr(IntType, CurrLen));
741  TVec<TInt> NewVec = TVec<TInt>();
742  for (i = 0; i < MxEId; i++) {
743  NewVec.Ins(i, GetIntAttrDefaultE(attr));
744  }
745  NewVec[EdgeH.GetKeyId(EId)] = value;
746  VecOfIntVecsE.Add(NewVec);
747  }
748  return 0;
749 }
750 
751 int TNEANet::AddStrAttrDatE(const int& EId, const TStr& value, const TStr& attr) {
752  int i;
753  TInt CurrLen;
754  if (!IsEdge(EId)) {
755  //AddEdge(EId);
756  return -1;
757  }
758  if (KeyToIndexTypeE.IsKey(attr)) {
760  NewVec[EdgeH.GetKeyId(EId)] = value;
761  } else {
762  CurrLen = VecOfStrVecsE.Len();
763  KeyToIndexTypeE.AddDat(attr, TIntPr(StrType, CurrLen));
764  TVec<TStr> NewVec = TVec<TStr>();
765  for (i = 0; i < MxEId; i++) {
766  NewVec.Ins(i, GetStrAttrDefaultE(attr));
767  }
768  NewVec[EdgeH.GetKeyId(EId)] = value;
769  VecOfStrVecsE.Add(NewVec);
770  }
771  return 0;
772 }
773 
774 int TNEANet::AddFltAttrDatE(const int& EId, const TFlt& value, const TStr& attr) {
775  int i;
776  TInt CurrLen;
777 
778  if (!IsEdge(EId)) {
779  //AddEdge(EId);
780  return -1;
781  }
782  if (KeyToIndexTypeE.IsKey(attr)) {
784  NewVec[EdgeH.GetKeyId(EId)] = value;
785  } else {
786  CurrLen = VecOfFltVecsE.Len();
787  KeyToIndexTypeE.AddDat(attr, TIntPr(FltType, CurrLen));
788  TVec<TFlt> NewVec = TVec<TFlt>();
789  for (i = 0; i < MxEId; i++) {
790  NewVec.Ins(i, GetFltAttrDefaultE(attr));
791  }
792  NewVec[EdgeH.GetKeyId(EId)] = value;
793  VecOfFltVecsE.Add(NewVec);
794  }
795  return 0;
796 }
797 
798 TInt TNEANet::GetIntAttrDatN(const int& NId, const TStr& attr) {
800 }
801 
802 TStr TNEANet::GetStrAttrDatN(const int& NId, const TStr& attr) {
804 }
805 
806 TFlt TNEANet::GetFltAttrDatN(const int& NId, const TStr& attr) {
808 }
809 
810 TInt TNEANet::GetIntAttrIndDatN(const int& NId, const int& index) {
811  return VecOfIntVecsN[index][NodeH.GetKeyId(NId)];
812 }
813 
814 TStr TNEANet::GetStrAttrIndDatN(const int& NId, const int& index) {
815  return VecOfStrVecsN[index][NodeH.GetKeyId(NId)];
816 }
817 
818 TFlt TNEANet::GetFltAttrIndDatN(const int& NId, const int& index) {
819  return VecOfFltVecsN[index][NodeH.GetKeyId(NId)];
820 }
821 
822 int TNEANet::GetIntAttrIndN(const TStr& attr) {
823  return KeyToIndexTypeN.GetDat(attr).Val2.Val;
824 }
825 
826 int TNEANet::GetAttrIndN(const TStr& attr) {
827  return KeyToIndexTypeN.GetDat(attr).Val2.Val;
828 }
829 
830 TInt TNEANet::GetIntAttrDatE(const int& EId, const TStr& attr) {
832 }
833 
834 TStr TNEANet::GetStrAttrDatE(const int& EId, const TStr& attr) {
836 }
837 
838 TFlt TNEANet::GetFltAttrDatE(const int& EId, const TStr& attr) {
840 }
841 
842 TInt TNEANet::GetIntAttrIndDatE(const int& EId, const int& index) {
843  return VecOfIntVecsE[index][EdgeH.GetKeyId(EId)];
844 }
845 
846 TStr TNEANet::GetStrAttrIndDatE(const int& EId, const int& index) {
847  return VecOfStrVecsE[index][EdgeH.GetKeyId(EId)];
848 }
849 
850 TFlt TNEANet::GetFltAttrIndDatE(const int& EId, const int& index) {
851  return VecOfFltVecsE[index][EdgeH.GetKeyId(EId)];
852 }
853 
854 int TNEANet::GetIntAttrIndE(const TStr& attr) {
855  return KeyToIndexTypeE.GetDat(attr).Val2.Val;
856 }
857 
858 int TNEANet::GetAttrIndE(const TStr& attr) {
859  return KeyToIndexTypeE.GetDat(attr).Val2.Val;
860 }
861 
862 int TNEANet::DelAttrDatN(const int& NId, const TStr& attr) {
863  TInt vecType = KeyToIndexTypeN(attr).Val1;
864  if (vecType == IntType) {
866  } else if (vecType == StrType) {
868  } else if (vecType == FltType) {
870  } else {
871  return -1;
872  }
873  return 0;
874 }
875 
876 int TNEANet::DelAttrDatE(const int& EId, const TStr& attr) {
877  // TODO(nkhadke): add error checking
878  TInt vecType = KeyToIndexTypeE(attr).Val1;
879  if (vecType == IntType) {
881  } else if (vecType == StrType) {
883  } else if (vecType == FltType) {
885  } else {
886  return -1;
887  }
888  return 0;
889 }
890 
891 int TNEANet::AddIntAttrN(const TStr& attr, TInt defaultValue){
892  int i;
893  TInt CurrLen;
894  TVec<TInt> NewVec;
895  CurrLen = VecOfIntVecsN.Len();
896  KeyToIndexTypeN.AddDat(attr, TIntPr(IntType, CurrLen));
897  NewVec = TVec<TInt>();
898  for (i = 0; i < MxNId; i++) {
899  NewVec.Ins(i, defaultValue);
900  }
901  VecOfIntVecsN.Add(NewVec);
902  if (!IntDefaultsN.IsKey(attr)) {
903  IntDefaultsN.AddDat(attr, defaultValue);
904  } else {
905  return -1;
906  }
907  return 0;
908 }
909 
910 int TNEANet::AddStrAttrN(const TStr& attr, TStr defaultValue) {
911  int i;
912  TInt CurrLen;
913  TVec<TStr> NewVec;
914  CurrLen = VecOfStrVecsN.Len();
915  KeyToIndexTypeN.AddDat(attr, TIntPr(StrType, CurrLen));
916  NewVec = TVec<TStr>();
917  for (i = 0; i < MxNId; i++) {
918  NewVec.Ins(i, defaultValue);
919  }
920  VecOfStrVecsN.Add(NewVec);
921  if (!StrDefaultsN.IsKey(attr)) {
922  StrDefaultsN.AddDat(attr, defaultValue);
923  } else {
924  return -1;
925  }
926  return 0;
927 }
928 
929 int TNEANet::AddFltAttrN(const TStr& attr, TFlt defaultValue) {
930  // TODO(nkhadke): add error checking
931  int i;
932  TInt CurrLen;
933  TVec<TFlt> NewVec;
934  CurrLen = VecOfStrVecsN.Len();
935  KeyToIndexTypeN.AddDat(attr, TIntPr(FltType, CurrLen));
936  NewVec = TVec<TFlt>();
937  for (i = 0; i < MxNId; i++) {
938  NewVec.Ins(i, defaultValue);
939  }
940  VecOfFltVecsN.Add(NewVec);
941  if (!FltDefaultsN.IsKey(attr)) {
942  FltDefaultsN.AddDat(attr, defaultValue);
943  } else {
944  return -1;
945  }
946  return 0;
947 }
948 
949 int TNEANet::AddIntAttrE(const TStr& attr, TInt defaultValue){
950  // TODO(nkhadke): add error checking
951  int i;
952  TInt CurrLen;
953  TVec<TInt> NewVec;
954  CurrLen = VecOfIntVecsE.Len();
955  KeyToIndexTypeE.AddDat(attr, TIntPr(IntType, CurrLen));
956  NewVec = TVec<TInt>();
957  for (i = 0; i < MxEId; i++) {
958  NewVec.Ins(i, defaultValue);
959  }
960  VecOfIntVecsE.Add(NewVec);
961  if (!IntDefaultsE.IsKey(attr)) {
962  IntDefaultsE.AddDat(attr, defaultValue);
963  } else {
964  return -1;
965  }
966  return 0;
967 }
968 
969 int TNEANet::AddStrAttrE(const TStr& attr, TStr defaultValue) {
970  int i;
971  TInt CurrLen;
972  TVec<TStr> NewVec;
973  CurrLen = VecOfStrVecsE.Len();
974  KeyToIndexTypeE.AddDat(attr, TIntPr(StrType, CurrLen));
975  NewVec = TVec<TStr>();
976  for (i = 0; i < MxEId; i++) {
977  NewVec.Ins(i, defaultValue);
978  }
979  VecOfStrVecsE.Add(NewVec);
980  if (!StrDefaultsE.IsKey(attr)) {
981  StrDefaultsE.AddDat(attr, defaultValue);
982  } else {
983  return -1;
984  }
985  return 0;
986 }
987 
988 int TNEANet::AddFltAttrE(const TStr& attr, TFlt defaultValue) {
989  int i;
990  TInt CurrLen;
991  TVec<TFlt> NewVec;
992  CurrLen = VecOfStrVecsE.Len();
993  KeyToIndexTypeE.AddDat(attr, TIntPr(FltType, CurrLen));
994  NewVec = TVec<TFlt>();
995  for (i = 0; i < MxEId; i++) {
996  NewVec.Ins(i, defaultValue);
997  }
998  VecOfFltVecsE.Add(NewVec);
999  if (!FltDefaultsE.IsKey(attr)) {
1000  FltDefaultsE.AddDat(attr, defaultValue);
1001  } else {
1002  return -1;
1003  }
1004  return 0;
1005 }
1006 
1007 int TNEANet::DelAttrN(const TStr& attr) {
1008  TInt vecType = KeyToIndexTypeN(attr).Val1;
1009  if (vecType == IntType) {
1011  if (IntDefaultsN.IsKey(attr)) {
1012  IntDefaultsN.DelKey(attr);
1013  }
1014  } else if (vecType == StrType) {
1016  if (StrDefaultsN.IsKey(attr)) {
1017  StrDefaultsN.DelKey(attr);
1018  }
1019  } else if (vecType == FltType) {
1021  if (FltDefaultsN.IsKey(attr)) {
1022  FltDefaultsN.DelKey(attr);
1023  }
1024  } else {
1025  return -1;
1026  }
1027  KeyToIndexTypeN.DelKey(attr);
1028  return 0;
1029 }
1030 
1031 int TNEANet::DelAttrE(const TStr& attr) {
1032  TInt vecType = KeyToIndexTypeE(attr).Val1;
1033  if (vecType == IntType) {
1035  if (IntDefaultsE.IsKey(attr)) {
1036  IntDefaultsE.DelKey(attr);
1037  }
1038  } else if (vecType == StrType) {
1040  if (StrDefaultsE.IsKey(attr)) {
1041  StrDefaultsE.DelKey(attr);
1042  }
1043  } else if (vecType == FltType) {
1045  if (FltDefaultsE.IsKey(attr)) {
1046  FltDefaultsE.DelKey(attr);
1047  }
1048  } else {
1049  return -1;
1050  }
1051  KeyToIndexTypeE.DelKey(attr);
1052  return 0;
1053 }
1054 
1055 // Return a small graph on 5 nodes and 6 edges.
1057  PNEANet Net = TNEANet::New();
1058  for (int i = 0; i < 5; i++) { Net->AddNode(i); }
1059  Net->AddEdge(0,1); Net->AddEdge(0,2);
1060  Net->AddEdge(0,3); Net->AddEdge(0,4);
1061  Net->AddEdge(1,2); Net->AddEdge(1,2);
1062  return Net;
1063 }
1064 
1065 void TNEANet::GetAttrNNames(TStrV& IntAttrNames, TStrV& FltAttrNames, TStrV& StrAttrNames) const {
1066  for (TStrIntPrH::TIter it = KeyToIndexTypeN.BegI(); it < KeyToIndexTypeN.EndI(); it++) {
1067  if (it.GetDat().GetVal1() == IntType) {
1068  IntAttrNames.Add(it.GetKey());
1069  }
1070  if (it.GetDat().GetVal1() == FltType) {
1071  FltAttrNames.Add(it.GetKey());
1072  }
1073  if (it.GetDat().GetVal1() == StrType) {
1074  StrAttrNames.Add(it.GetKey());
1075  }
1076  }
1077 }
1078 
1079 void TNEANet::GetAttrENames(TStrV& IntAttrNames, TStrV& FltAttrNames, TStrV& StrAttrNames) const {
1080  for (TStrIntPrH::TIter it = KeyToIndexTypeE.BegI(); it < KeyToIndexTypeE.EndI(); it++) {
1081  if (it.GetDat().GetVal1() == IntType) {
1082  IntAttrNames.Add(it.GetKey());
1083  }
1084  if (it.GetDat().GetVal1() == FltType) {
1085  FltAttrNames.Add(it.GetKey());
1086  }
1087  if (it.GetDat().GetVal1() == StrType) {
1088  StrAttrNames.Add(it.GetKey());
1089  }
1090  }
1091 }
1092 
#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:525
TVec< TIntV > VecOfIntVecsN
Definition: network.h:1837
TPair< TInt, TInt > TIntPr
Definition: ds.h:83
TInt GetIntAttrDatN(const TNodeI &NodeI, const TStr &attr)
Gets the value of int attr from the node attr value vector.
Definition: network.h:2156
int GetAttrIndE(const TStr &attr)
Gets the index of the edge attr value vector specified by attr.
Definition: network.cpp:858
bool DelIfIn(const TVal &Val)
Removes the first occurrence of element Val.
Definition: ds.h:1115
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:1622
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:1079
int GetNodes() const
Returns the number of nodes in the graph.
Definition: network.h:1890
bool IsKeyIdEqKeyN() const
Definition: hash.h:191
#define IAssertR(Cond, Reason)
Definition: bd.h:265
int GetSrcNId() const
Definition: network.h:1623
TStr GetStrAttrDatN(const TNodeI &NodeI, const TStr &attr)
Gets the value of str attr from the node attr value vector.
Definition: network.h:2160
THash< TStr, TFlt > FltDefaultsE
Definition: network.h:1836
int AddFltAttrDatE(const TEdgeI &EdgeI, const TFlt &value, const TStr &attr)
Attribute based add function for attr to Flt value.
Definition: network.h:2152
TStr GetEdgeAttrValue(const int &EId, const TStrIntPrH::TIter &EdgeHI) const
Definition: network.cpp:299
TVec< TStrV > VecOfStrVecsE
Definition: network.h:1838
THash< TStr, TInt > IntDefaultsE
Definition: network.h:1834
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:822
bool NodeAttrIsStrDeleted(const int &NId, const TStrIntPrH::TIter &NodeHI) const
Definition: network.cpp:142
int DelAttrN(const TStr &attr)
Removes all the values for node attr.
Definition: network.cpp:1007
bool NodeAttrIsDeleted(const int &NId, const TStrIntPrH::TIter &NodeHI) const
Definition: network.cpp:127
bool IsStrAttrDeletedN(const int &NId, const TStr &attr) const
Definition: network.cpp:119
void AttrNameEI(const TInt &EId, TStrV &Names) const
Returns a vector of attr names for edge EId.
Definition: network.h:1976
static const T & Mx(const T &LVal, const T &RVal)
Definition: xmath.h:32
TNode & GetNode(const int &NId)
Definition: network.h:1808
void StrAttrNameNI(const TInt &NId, TStrV &Names) const
Returns a vector of str attr names for node NId.
Definition: network.h:1959
void FltAttrNameNI(const TInt &NId, TStrV &Names) const
Returns a vector of int attr names for node NId.
Definition: network.h:1967
bool EdgeAttrIsFltDeleted(const int &EId, const TStrIntPrH::TIter &EdgeHI) const
Definition: network.cpp:293
TInt GetIntAttrDefaultE(const TStr &attribute) const
Get Int edge attribute val. If not a proper attr, return default.
Definition: network.h:1820
bool EdgeAttrIsDeleted(const int &EId, const TStrIntPrH::TIter &EdgeHI) const
Definition: network.cpp:274
TFlt GetFltAttrDatE(const TEdgeI &EdgeI, const TStr &attr)
Gets the value of flt attr from the edge attr value vector.
Definition: network.h:2193
TIter BegI() const
Definition: hash.h:171
bool IsAttrDeletedE(const int &EId, const TStr &attr) const
Definition: network.cpp:255
bool IsFltAttrDeletedE(const int &EId, const TStr &attr) const
Definition: network.cpp:270
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:535
const TNEANet * Graph
Definition: network.h:1632
bool EdgeAttrIsIntDeleted(const int &EId, const TStrIntPrH::TIter &EdgeHI) const
Definition: network.cpp:281
int DelAttrE(const TStr &attr)
Removes all the values for edge attr.
Definition: network.cpp:1031
void StrAttrValueNI(const TInt &NId, TStrV &Values) const
Returns a vector of attr values for node NId.
Definition: network.h:1963
TStrIntPrH KeyToIndexTypeE
Definition: network.h:1832
TStr GetStrAttrDatE(const TEdgeI &EdgeI, const TStr &attr)
Gets the value of str attr from the edge attr value vector.
Definition: network.h:2190
void DelEdge(const int &EId)
Deletes an edge with edge ID EId from the graph.
Definition: network.cpp:469
THash< TStr, TFlt > FltDefaultsN
Definition: network.h:1836
bool IsEdge(const int &EId) const
Tests whether an edge with edge ID EId exists in the graph.
Definition: network.h:2068
void IntAttrValueEI(const TInt &EId, TIntV &Values) const
Returns a vector of attr values for edge EId.
Definition: network.h:1988
int AddIntAttrDatE(const TEdgeI &EdgeI, const TInt &value, const TStr &attr)
Attribute based add function for attr to Int value.
Definition: network.h:2142
TInt MxNId
Definition: network.h:1828
TIntV OutEIdV
Definition: network.h:1595
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
Node iterator. Only forward iteration (operator++) is supported.
Definition: network.h:1628
void AttrValueNI(const TInt &NId, TStrV &Values) const
Returns a vector of attr values for node NId.
Definition: network.h:1947
TVec< TIntV > VecOfIntVecsE
Definition: network.h:1837
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:1951
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
Definition: network.cpp:313
void DelNode(const int &NId)
Deletes node of ID NId from the graph.
Definition: network.cpp:359
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:1027
void ErrNotify(const char *NotifyCStr)
Definition: bd.h:74
void Defrag()
Definition: hash.h:509
Definition: dt.h:1291
TVec< TStrV > VecOfStrVecsN
Definition: network.h:1838
bool IsIntAttrDeletedE(const int &EId, const TStr &attr) const
Definition: network.cpp:262
void GetEIdV(TIntV &EIdV) const
Gets a vector IDs of all edges in the graph.
Definition: network.cpp:532
void Dump(FILE *OutF=stdout) const
Print the graph in a human readable form to an output stream OutF.
Definition: network.cpp:601
bool IsFltAttrDeletedN(const int &NId, const TStr &attr) const
Definition: network.cpp:123
int AddFltAttrDatN(const TNodeI &NodeI, const TFlt &value, const TStr &attr)
Attribute based add function for attr to Flt value.
Definition: network.h:2136
bool NodeAttrIsFltDeleted(const int &NId, const TStrIntPrH::TIter &NodeHI) const
Definition: network.cpp:150
THash< TInt, TEdge > EdgeH
Definition: network.h:1830
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:1065
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:826
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:2212
bool IsStrAttrDeletedE(const int &EId, const TStr &attr) const
Definition: network.cpp:266
void DelKey(const TKey &Key)
Definition: hash.h:358
static const int Mn
Definition: dt.h:1046
TVec< TFltV > VecOfFltVecsE
Definition: network.h:1839
TStrIntPrH KeyToIndexTypeN
KeyToIndexType[N|E]: Key->(Type,Index).
Definition: network.h:1832
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:2187
int AddStrAttrDatN(const TNodeI &NodeI, const TStr &value, const TStr &attr)
Attribute based add function for attr to Str value.
Definition: network.h:2131
void StrAttrValueEI(const TInt &EId, TStrV &Values) const
Returns a vector of attr values for node NId.
Definition: network.h:1996
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
Definition: gbase.h:38
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:2004
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:891
TStr GetStrAttrDefaultE(const TStr &attribute) const
Get Str edge attribute val. If not a proper attr, return default.
Definition: network.h:1822
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:419
bool FNextKeyId(int &KeyId) const
Definition: hash.h:432
THash< TStr, TStr > StrDefaultsN
Definition: network.h:1835
void AttrNameNI(const TInt &NId, TStrV &Names) const
Returns a vector of attr names for node NId.
Definition: network.h:1943
int AddStrAttrE(const TStr &attr, TStr defaultValue=TStr::GetNullStr())
Adds a new Str edge attribute to the hashmap.
Definition: network.cpp:969
TFlt GetFltAttrDatN(const TNodeI &NodeI, const TStr &attr)
Gets the value of flt attr from the node attr value vector.
Definition: network.h:2163
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the graph.
Definition: network.h:2076
bool IsOk(const bool &ThrowExcept=true) const
Checks the graph data structure for internal consistency.
Definition: network.cpp:548
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:2172
int FFirstKeyId() const
Definition: hash.h:232
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the graph.
Definition: network.h:2078
int GetDstNId() const
Definition: network.h:1624
TStr GetStrAttrDefaultN(const TStr &attribute) const
Get Str node attribute val. If not a proper attr, return default.
Definition: network.h:1816
TStr GetNodeAttrValue(const int &NId, const TStrIntPrH::TIter &NodeHI) const
Definition: network.cpp:158
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:1980
void FltAttrValueNI(const TInt &NId, TFltV &Values) const
Returns a vector of attr values for node NId.
Definition: network.h:1971
Directed multigraph with node edge attributes.
Definition: network.h:1587
TInt MxEId
Definition: network.h:1828
TStr GetStr() const
Definition: dt.h:679
static PNEANet GetSmallGraph()
Returns a small multigraph on 5 nodes and 6 edges.
Definition: network.cpp:1056
Definition: dt.h:1042
TEdge & GetEdge(const int &EId)
Definition: network.h:1810
int GetOutDeg() const
Definition: network.h:1605
int GetKeyId(const TKey &Key) const
Definition: hash.h:420
void IntAttrValueNI(const TInt &NId, TIntV &Values) const
Returns a vector of attr values for node NId.
Definition: network.h:1955
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
Definition: network.h:1910
int AddStrAttrN(const TStr &attr, TStr defaultValue=TStr::GetNullStr())
Adds a new Str node attribute to the hashmap.
Definition: network.cpp:910
void Ins(const TSizeTy &ValN, const TVal &Val)
Inserts new element Val before the element at position ValN.
Definition: ds.h:1086
bool IsIntAttrDeletedN(const int &NId, const TStr &attr) const
Definition: network.cpp:115
Edge iterator. Only forward iteration (operator++) is supported.
Definition: network.h:1699
int DelAttrDatE(const TEdgeI &EdgeI, const TStr &attr)
Deletes the edge attribute for NodeI.
Definition: network.h:2220
void FltAttrNameEI(const TInt &EId, TStrV &Names) const
Returns a vector of int attr names for node NId.
Definition: network.h:2000
THash< TStr, TStr > StrDefaultsE
Definition: network.h:1835
void StrAttrNameEI(const TInt &EId, TStrV &Names) const
Returns a vector of str attr names for node NId.
Definition: network.h:1992
TFlt GetFltAttrDefaultE(const TStr &attribute) const
Get Flt edge attribute val. If not a proper attr, return default.
Definition: network.h:1824
int GetEdges() const
Returns the number of edges in the graph.
Definition: network.h:2049
void GetKeyV(TVec< TKey > &KeyV) const
Definition: hash.h:438
int GetOutEId(const int &EdgeN) const
Definition: network.h:1607
bool EdgeAttrIsStrDeleted(const int &EId, const TStrIntPrH::TIter &EdgeHI) const
Definition: network.cpp:287
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:2147
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:1223
void Pack()
The vector reduces its capacity (frees memory) to match its size.
Definition: ds.h:987
enum TGraphFlag_ TGraphFlag
Graph Flags, used for quick testing of graph types.
bool IsAttrDeletedN(const int &NId, const TStr &attr) const
Definition: network.cpp:108
TVec< TFltV > VecOfFltVecsN
Definition: network.h:1839
int GetInDeg() const
Definition: network.h:1604
#define EAssertR(Cond, MsgStr)
Definition: bd.h:283
int GetId() const
Definition: network.h:1602
TVal2 Val2
Definition: ds.h:35
Definition: bd.h:196
THash< TStr, TInt > IntDefaultsN
Definition: network.h:1834
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:1906
bool NodeAttrIsIntDeleted(const int &NId, const TStrIntPrH::TIter &NodeHI) const
Definition: network.cpp:134
THash< TInt, TNode > NodeH
Definition: network.h:1829
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:486
TIntV InEIdV
Definition: network.h:1595
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:2182
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
Definition: network.h:1908
int AddIntAttrE(const TStr &attr, TInt defaultValue=TInt::Mn)
Adds a new Int edge attribute to the hashmap.
Definition: network.cpp:949
TFlt GetFltAttrDefaultN(const TStr &attribute) const
Get Flt node attribute val. If not a proper attr, return default.
Definition: network.h:1818
int GetInEId(const int &EdgeN) const
Definition: network.h:1606
void IntAttrNameEI(const TInt &EId, TStrV &Names) const
Returns a vector of int attr names for edge EId.
Definition: network.h:1984
TInt GetIntAttrDefaultN(const TStr &attribute) const
Get Int node attribute val. If not a proper attr, return default.
Definition: network.h:1814
char * CStr()
Definition: dt.h:476
THashIter NodeHI
Definition: network.h:1631
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:559
static PNEANet New()
Static cons returns pointer to graph. Ex: PNEANet Graph=TNEANet::New().
Definition: network.h:1876
int DelAttrDatN(const TNodeI &NodeI, const TStr &attr)
Deletes the node attribute for NodeI.
Definition: network.h:2217
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
int AddIntAttrDatN(const TNodeI &NodeI, const TInt &value, const TStr &attr)
Attribute based add function for attr to Int value.
Definition: network.h:2126
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:854
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:2177
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:210
void Defrag(const bool &OnlyNodeLinks=false)
Defragments the graph.
Definition: network.cpp:539
int AddFltAttrE(const TStr &attr, TFlt defaultValue=TFlt::Mn)
Adds a new Flt edge attribute to the hashmap.
Definition: network.cpp:988
static const double Mn
Definition: dt.h:1295
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:420
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:2207
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:2202
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:929