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