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
TNodeEDatNet< TNodeData, TEdgeData > Class Template Reference

Node Edge Network (directed graph, TNGraph with data on nodes and edges). More...

#include <network.h>

Classes

class  TEdgeI
 Edge iterator. Only forward iteration (operator++) is supported. More...
 
class  TNode
 
class  TNodeI
 Node iterator. Only forward iteration (operator++) is supported. More...
 

Public Types

typedef TNodeData TNodeDat
 
typedef TEdgeData TEdgeDat
 
typedef TNodeEDatNet
< TNodeData, TEdgeData > 
TNet
 
typedef TPt< TNetPNet
 
typedef TVec< TPair< TInt,
TEdgeData > > 
TNIdDatPrV
 

Public Member Functions

 TNodeEDatNet ()
 
 TNodeEDatNet (const int &Nodes, const int &Edges)
 Constructor that reserves enough memory for a network of Nodes nodes and Edges edges. More...
 
 TNodeEDatNet (const TNodeEDatNet &NodeNet)
 
 TNodeEDatNet (TSIn &SIn)
 Constructor that loads the network from a (binary) stream SIn. More...
 
virtual ~TNodeEDatNet ()
 
virtual void Save (TSOut &SOut) const
 Saves the network to a (binary) stream SOut. More...
 
bool HasFlag (const TGraphFlag &Flag) const
 Allows for run-time checking the type of the network (see the TGraphFlag for flags). More...
 
TNodeEDatNetoperator= (const TNodeEDatNet &NodeNet)
 
int GetNodes () const
 Returns the number of nodes in the network. More...
 
int AddNode (int NId=-1)
 Adds a node of ID NId to the network. More...
 
int AddNode (int NId, const TNodeData &NodeDat)
 Adds a node of ID NId and node data NodeDat to the network. More...
 
int AddNode (const TNodeI &NodeI)
 Adds a node NodeI and its node data to the network. More...
 
void DelNode (const int &NId)
 Deletes node of ID NId from the network. More...
 
void DelNode (const TNode &NodeI)
 Deletes node of ID NodeI.GetId() from the network. More...
 
bool IsNode (const int &NId) const
 Tests whether ID NId is a node. More...
 
TNodeI BegNI () const
 Returns an iterator referring to the first node in the network. More...
 
TNodeI EndNI () const
 Returns an iterator referring to the past-the-end node in the network. More...
 
TNodeI GetNI (const int &NId) const
 Returns an iterator referring to the node of ID NId in the network. More...
 
const TNodeGetNode (const int &NId) const
 Returns node element for the node of ID NId in the network. More...
 
void SetNDat (const int &NId, const TNodeData &NodeDat)
 Sets node data for the node of ID NId in the network. More...
 
TNodeData & GetNDat (const int &NId)
 Returns node data for the node of ID NId in the network. More...
 
const TNodeData & GetNDat (const int &NId) const
 Returns node data for the node of ID NId in the network. More...
 
int GetMxNId () const
 Returns an ID that is larger than any node ID in the network. More...
 
int GetEdges () const
 Returns the number of edges in the network. More...
 
int AddEdge (const int &SrcNId, const int &DstNId)
 Adds an edge from node IDs SrcNId to node DstNId to the network. More...
 
int AddEdge (const int &SrcNId, const int &DstNId, const TEdgeData &EdgeDat)
 Adds an edge and edge data from node IDs SrcNId to node DstNId. More...
 
int AddEdge (const TEdgeI &EdgeI)
 Adds an edge from EdgeI.GetSrcNId() to EdgeI.GetDstNId() and its edge data to the network. More...
 
void DelEdge (const int &SrcNId, const int &DstNId, const bool &IsDir=true)
 Deletes an edge from node IDs SrcNId to DstNId from the network. More...
 
bool IsEdge (const int &SrcNId, const int &DstNId, const bool &IsDir=true) const
 Tests whether an edge from node IDs SrcNId to DstNId exists in the network. More...
 
TEdgeI BegEI () const
 Returns an iterator referring to the first edge in the network. More...
 
TEdgeI EndEI () const
 Returns an iterator referring to the past-the-end edge in the network. More...
 
TEdgeI GetEI (const int &EId) const
 Not supported/implemented! More...
 
TEdgeI GetEI (const int &SrcNId, const int &DstNId) const
 Returns an iterator referring to edge (SrcNId, DstNId) in the network. More...
 
void SetEDat (const int &SrcNId, const int &DstNId, const TEdgeData &EdgeDat)
 Sets edge data for the edge between nodes SrcNId and DstNId in the network. More...
 
bool GetEDat (const int &SrcNId, const int &DstNId, TEdgeData &EdgeDat) const
 Returns edge data in Data for the edge from node IDs SrcNId to DstNId. More...
 
TEdgeData & GetEDat (const int &SrcNId, const int &DstNId)
 Returns edge data for the edge from node IDs SrcNId to DstNId. More...
 
const TEdgeData & GetEDat (const int &SrcNId, const int &DstNId) const
 Returns edge data for the edge from node IDs SrcNId to DstNId. More...
 
void SetAllEDat (const TEdgeData &EdgeDat)
 Sets edge data for all the edges in the network to EDat. More...
 
int GetRndNId (TRnd &Rnd=TInt::Rnd)
 Returns an ID of a random node in the network. More...
 
TNodeI GetRndNI (TRnd &Rnd=TInt::Rnd)
 Returns an interator referring to a random node in the network. More...
 
void GetNIdV (TIntV &NIdV) const
 Gets a vector IDs of all nodes in the network. More...
 
bool Empty () const
 Tests whether the network is empty (has zero nodes). More...
 
void Clr (const bool &DoDel=true, const bool &ResetDat=true)
 Deletes all nodes and edges from the network. More...
 
void Reserve (const int &Nodes, const int &Edges)
 Reserves memory for a network of Nodes nodes and Edges edges. More...
 
void SortNIdById (const bool &Asc=true)
 Sorts nodes by node IDs. More...
 
void SortNIdByDat (const bool &Asc=true)
 Sorts nodes by node data. More...
 
void Defrag (const bool &OnlyNodeLinks=false)
 Defragments the network. More...
 
bool IsOk (const bool &ThrowExcept=true) const
 Checks the network data structure for internal consistency. More...
 

Static Public Member Functions

static PNet New ()
 Static constructor that returns a pointer to the network. Call: TPt <TNodeEDatNet<TNodeData, TEdgeData> > Net = TNodeEDatNet<TNodeData, TEdgeData>::New(). More...
 
static PNet Load (TSIn &SIn)
 Static constructor that loads the network from a stream SIn and returns a pointer to it. More...
 

Protected Member Functions

TNodeGetNode (const int &NId)
 

Static Protected Member Functions

static int GetNIdPos (const TVec< TPair< TInt, TEdgeData > > &NIdV, const int &NId)
 

Protected Attributes

TCRef CRef
 
TInt MxNId
 
THash< TInt, TNodeNodeH
 

Friends

class TPt< TNodeEDatNet< TNodeData, TEdgeData > >
 

Detailed Description

template<class TNodeData, class TEdgeData>
class TNodeEDatNet< TNodeData, TEdgeData >

Node Edge Network (directed graph, TNGraph with data on nodes and edges).

Definition at line 441 of file network.h.

Member Typedef Documentation

template<class TNodeData, class TEdgeData>
typedef TPt<TNet> TNodeEDatNet< TNodeData, TEdgeData >::PNet

Definition at line 446 of file network.h.

template<class TNodeData, class TEdgeData>
typedef TEdgeData TNodeEDatNet< TNodeData, TEdgeData >::TEdgeDat

Definition at line 444 of file network.h.

template<class TNodeData, class TEdgeData>
typedef TNodeEDatNet<TNodeData, TEdgeData> TNodeEDatNet< TNodeData, TEdgeData >::TNet

Definition at line 445 of file network.h.

template<class TNodeData, class TEdgeData>
typedef TVec<TPair<TInt, TEdgeData> > TNodeEDatNet< TNodeData, TEdgeData >::TNIdDatPrV

Definition at line 447 of file network.h.

template<class TNodeData, class TEdgeData>
typedef TNodeData TNodeEDatNet< TNodeData, TEdgeData >::TNodeDat

Definition at line 443 of file network.h.

Constructor & Destructor Documentation

template<class TNodeData, class TEdgeData>
TNodeEDatNet< TNodeData, TEdgeData >::TNodeEDatNet ( )
inline

Definition at line 583 of file network.h.

583 : CRef(), MxNId(0), NodeH() { }
TCRef CRef
Definition: network.h:579
THash< TInt, TNode > NodeH
Definition: network.h:581
TInt MxNId
Definition: network.h:580
template<class TNodeData, class TEdgeData>
TNodeEDatNet< TNodeData, TEdgeData >::TNodeEDatNet ( const int &  Nodes,
const int &  Edges 
)
inlineexplicit

Constructor that reserves enough memory for a network of Nodes nodes and Edges edges.

Definition at line 585 of file network.h.

585 : MxNId(0) { Reserve(Nodes, Edges); }
TInt MxNId
Definition: network.h:580
void Reserve(const int &Nodes, const int &Edges)
Reserves memory for a network of Nodes nodes and Edges edges.
Definition: network.h:705
template<class TNodeData, class TEdgeData>
TNodeEDatNet< TNodeData, TEdgeData >::TNodeEDatNet ( const TNodeEDatNet< TNodeData, TEdgeData > &  NodeNet)
inline

Definition at line 586 of file network.h.

586 : MxNId(NodeNet.MxNId), NodeH(NodeNet.NodeH) { }
THash< TInt, TNode > NodeH
Definition: network.h:581
TInt MxNId
Definition: network.h:580
template<class TNodeData, class TEdgeData>
TNodeEDatNet< TNodeData, TEdgeData >::TNodeEDatNet ( TSIn SIn)
inline

Constructor that loads the network from a (binary) stream SIn.

Definition at line 588 of file network.h.

588 : MxNId(SIn), NodeH(SIn) { }
THash< TInt, TNode > NodeH
Definition: network.h:581
TInt MxNId
Definition: network.h:580
template<class TNodeData, class TEdgeData>
virtual TNodeEDatNet< TNodeData, TEdgeData >::~TNodeEDatNet ( )
inlinevirtual

Definition at line 589 of file network.h.

589 { }

Member Function Documentation

template<class TNodeData , class TEdgeData >
int TNodeEDatNet< TNodeData, TEdgeData >::AddEdge ( const int &  SrcNId,
const int &  DstNId 
)

Adds an edge from node IDs SrcNId to node DstNId to the network.

If the edge already exists return -2. If the edge does not exist and was successfully added return -1. Normally the function should return an ID of the edge added but since edges in TNodeEDatNet have no IDs we return -1. Function aborts if SrcNId or DstNId are not nodes in the network.

Definition at line 810 of file network.h.

810  {
811  return AddEdge(SrcNId, DstNId, TEdgeData());
812 }
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge from node IDs SrcNId to node DstNId to the network.
Definition: network.h:810
template<class TNodeData , class TEdgeData>
int TNodeEDatNet< TNodeData, TEdgeData >::AddEdge ( const int &  SrcNId,
const int &  DstNId,
const TEdgeData &  EdgeDat 
)

Adds an edge and edge data from node IDs SrcNId to node DstNId.

If the edge already exists, set edges data and returns -2. If the edge does not exist and was successfully added returns -1. Normally the function should return an ID of the edge added but since edges in TNodeEDatNet have no IDs it returns -1. Function aborts if SrcNId or DstNId are not nodes in the network.

Definition at line 815 of file network.h.

815  {
816  IAssertR(IsNode(SrcNId) && IsNode(DstNId), TStr::Fmt("%d or %d not a node.", SrcNId, DstNId).CStr());
817  //IAssert(! IsEdge(SrcNId, DstNId));
818  if (IsEdge(SrcNId, DstNId)) {
819  GetEDat(SrcNId, DstNId) = EdgeDat;
820  return -2;
821  }
822  GetNode(SrcNId).OutNIdV.AddSorted(TPair<TInt, TEdgeData>(DstNId, EdgeDat));
823  GetNode(DstNId).InNIdV.AddSorted(SrcNId);
824  return -1; // edge id
825 }
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:624
#define IAssertR(Cond, Reason)
Definition: bd.h:265
bool GetEDat(const int &SrcNId, const int &DstNId, TEdgeData &EdgeDat) const
Returns edge data in Data for the edge from node IDs SrcNId to DstNId.
Definition: network.h:857
TSizeTy AddSorted(const TVal &Val, const bool &Asc=true, const TSizeTy &_MxVals=-1)
Adds element Val to a sorted vector.
Definition: ds.h:1027
TNode & GetNode(const int &NId)
Definition: network.h:576
bool IsEdge(const int &SrcNId, const int &DstNId, const bool &IsDir=true) const
Tests whether an edge from node IDs SrcNId to DstNId exists in the network.
Definition: network.h:843
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
TNIdDatPrV OutNIdV
Definition: network.h:454
template<class TNodeData, class TEdgeData>
int TNodeEDatNet< TNodeData, TEdgeData >::AddEdge ( const TEdgeI EdgeI)
inline

Adds an edge from EdgeI.GetSrcNId() to EdgeI.GetDstNId() and its edge data to the network.

Definition at line 662 of file network.h.

662 { return AddEdge(EdgeI.GetSrcNId(), EdgeI.GetDstNId(), EdgeI()); }
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge from node IDs SrcNId to node DstNId to the network.
Definition: network.h:810
template<class TNodeData , class TEdgeData >
int TNodeEDatNet< TNodeData, TEdgeData >::AddNode ( int  NId = -1)

Adds a node of ID NId to the network.

Returns the ID of the node being added. If NId is -1, node ID is automatically assigned. Aborts, if a node with ID NId already exists.

Definition at line 752 of file network.h.

752  {
753  if (NId == -1) {
754  NId = MxNId; MxNId++;
755  } else {
756  IAssertR(!IsNode(NId), TStr::Fmt("NodeId %d already exists", NId));
757  MxNId = TMath::Mx(NId+1, MxNId());
758  }
759  NodeH.AddDat(NId, TNode(NId));
760  return NId;
761 }
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:624
#define IAssertR(Cond, Reason)
Definition: bd.h:265
static const T & Mx(const T &LVal, const T &RVal)
Definition: xmath.h:32
THash< TInt, TNode > NodeH
Definition: network.h:581
TInt MxNId
Definition: network.h:580
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
template<class TNodeData, class TEdgeData >
int TNodeEDatNet< TNodeData, TEdgeData >::AddNode ( int  NId,
const TNodeData &  NodeDat 
)

Adds a node of ID NId and node data NodeDat to the network.

Returns the ID of the node being added. If NId is -1, node ID is automatically assigned. Aborts, if a node with ID NId already exists.

Definition at line 764 of file network.h.

764  {
765  if (NId == -1) {
766  NId = MxNId; MxNId++;
767  } else {
768  IAssertR(!IsNode(NId), TStr::Fmt("NodeId %d already exists", NId));
769  MxNId = TMath::Mx(NId+1, MxNId());
770  }
771  NodeH.AddDat(NId, TNode(NId, NodeDat));
772  return NId;
773 }
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:624
#define IAssertR(Cond, Reason)
Definition: bd.h:265
static const T & Mx(const T &LVal, const T &RVal)
Definition: xmath.h:32
THash< TInt, TNode > NodeH
Definition: network.h:581
TInt MxNId
Definition: network.h:580
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
template<class TNodeData, class TEdgeData>
int TNodeEDatNet< TNodeData, TEdgeData >::AddNode ( const TNodeI NodeI)
inline

Adds a node NodeI and its node data to the network.

Definition at line 616 of file network.h.

616 { return AddNode(NodeI.GetId(), NodeI.GetDat()); }
int AddNode(int NId=-1)
Adds a node of ID NId to the network.
Definition: network.h:752
template<class TNodeData, class TEdgeData>
TEdgeI TNodeEDatNet< TNodeData, TEdgeData >::BegEI ( ) const
inline

Returns an iterator referring to the first edge in the network.

Definition at line 672 of file network.h.

672 { TNodeI NI=BegNI(); while(NI<EndNI() && NI.GetOutDeg()==0) NI++; return TEdgeI(NI, EndNI()); }
int GetOutDeg() const
Returns out-degree of the current node.
Definition: network.h:503
TNodeI BegNI() const
Returns an iterator referring to the first node in the network.
Definition: network.h:626
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the network.
Definition: network.h:628
template<class TNodeData, class TEdgeData>
TNodeI TNodeEDatNet< TNodeData, TEdgeData >::BegNI ( ) const
inline

Returns an iterator referring to the first node in the network.

Definition at line 626 of file network.h.

626 { return TNodeI(NodeH.BegI(), this); }
TIter BegI() const
Definition: hash.h:171
THash< TInt, TNode > NodeH
Definition: network.h:581
template<class TNodeData, class TEdgeData>
void TNodeEDatNet< TNodeData, TEdgeData >::Clr ( const bool &  DoDel = true,
const bool &  ResetDat = true 
)
inline

Deletes all nodes and edges from the network.

Definition at line 702 of file network.h.

702  {
703  MxNId = 0; NodeH.Clr(DoDel, -1, ResetDat); }
THash< TInt, TNode > NodeH
Definition: network.h:581
TInt MxNId
Definition: network.h:580
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
Definition: hash.h:315
template<class TNodeData , class TEdgeData >
void TNodeEDatNet< TNodeData, TEdgeData >::Defrag ( const bool &  OnlyNodeLinks = false)

Defragments the network.

After performing many node and edge insertions and deletions to a network, the network data structure will be fragmented in memory. This function compacts down the network data structure and frees unneeded memory.

Definition at line 909 of file network.h.

909  {
910  for (int n = NodeH.FFirstKeyId(); NodeH.FNextKeyId(n);) {
911  TNode& Node = NodeH[n];
912  Node.InNIdV.Pack(); Node.OutNIdV.Pack();
913  }
914  if (! OnlyNodeLinks && ! NodeH.IsKeyIdEqKeyN()) {
915  NodeH.Defrag();
916  }
917 }
bool IsKeyIdEqKeyN() const
Definition: hash.h:191
void Defrag()
Definition: hash.h:509
THash< TInt, TNode > NodeH
Definition: network.h:581
bool FNextKeyId(int &KeyId) const
Definition: hash.h:432
int FFirstKeyId() const
Definition: hash.h:232
void Pack()
Definition: hash.h:243
template<class TNodeData , class TEdgeData >
void TNodeEDatNet< TNodeData, TEdgeData >::DelEdge ( const int &  SrcNId,
const int &  DstNId,
const bool &  IsDir = true 
)

Deletes an edge from node IDs SrcNId to DstNId from the network.

If the edge (SrcNId, DstNId) does not exist in the network function still completes. But the function aborts if SrcNId or DstNId are not nodes in the network.

Definition at line 828 of file network.h.

828  {
829  IAssertR(IsNode(SrcNId) && IsNode(DstNId), TStr::Fmt("%d or %d not a node.", SrcNId, DstNId).CStr());
830  int pos = GetNIdPos(GetNode(SrcNId).OutNIdV, DstNId);
831  if (pos != -1) { GetNode(SrcNId).OutNIdV.Del(pos); }
832  pos = GetNode(DstNId).InNIdV.SearchBin(SrcNId);
833  if (pos != -1) { GetNode(DstNId).InNIdV.Del(pos); }
834  if (! IsDir) {
835  pos = GetNIdPos(GetNode(DstNId).OutNIdV, SrcNId);
836  if (pos != -1) { GetNode(DstNId).OutNIdV.Del(pos); }
837  pos = GetNode(SrcNId).InNIdV.SearchBin(DstNId);
838  if (pos != -1) { GetNode(SrcNId).InNIdV.Del(pos); }
839  }
840 }
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:624
static int GetNIdPos(const TVec< TPair< TInt, TEdgeData > > &NIdV, const int &NId)
Definition: network.h:739
#define IAssertR(Cond, Reason)
Definition: bd.h:265
void Del(const TSizeTy &ValN)
Removes the element at position ValN.
Definition: ds.h:1094
TNode & GetNode(const int &NId)
Definition: network.h:576
TSizeTy SearchBin(const TVal &Val) const
Returns the position of an element with value Val.
Definition: ds.h:1418
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
TNIdDatPrV OutNIdV
Definition: network.h:454
template<class TNodeData , class TEdgeData >
void TNodeEDatNet< TNodeData, TEdgeData >::DelNode ( const int &  NId)

Deletes node of ID NId from the network.

If the node of ID NId does not exist the function aborts.

Definition at line 782 of file network.h.

782  {
783  const TNode& Node = GetNode(NId);
784  for (int out = 0; out < Node.GetOutDeg(); out++) {
785  const int nbr = Node.GetOutNId(out);
786  if (nbr == NId) { continue; }
787  TIntV& NIdV = GetNode(nbr).InNIdV;
788  const int pos = NIdV.SearchBin(NId);
789  if (pos != -1) { NIdV.Del(pos); }
790  }
791  for (int in = 0; in < Node.GetInDeg(); in++) {
792  const int nbr = Node.GetInNId(in);
793  if (nbr == NId) { continue; }
794  TNIdDatPrV& NIdDatV = GetNode(nbr).OutNIdV;
795  const int pos = GetNIdPos(NIdDatV, NId);
796  if (pos != -1) { NIdDatV.Del(pos); }
797  }
798  NodeH.DelKey(NId);
799 }
static int GetNIdPos(const TVec< TPair< TInt, TEdgeData > > &NIdV, const int &NId)
Definition: network.h:739
void Del(const TSizeTy &ValN)
Removes the element at position ValN.
Definition: ds.h:1094
THash< TInt, TNode > NodeH
Definition: network.h:581
void DelKey(const TKey &Key)
Definition: hash.h:358
TNode & GetNode(const int &NId)
Definition: network.h:576
TVec< TPair< TInt, TEdgeData > > TNIdDatPrV
Definition: network.h:447
TSizeTy SearchBin(const TVal &Val) const
Returns the position of an element with value Val.
Definition: ds.h:1418
TNIdDatPrV OutNIdV
Definition: network.h:454
template<class TNodeData, class TEdgeData>
void TNodeEDatNet< TNodeData, TEdgeData >::DelNode ( const TNode NodeI)
inline

Deletes node of ID NodeI.GetId() from the network.

Definition at line 622 of file network.h.

622 { DelNode(NodeI.GetId()); }
void DelNode(const int &NId)
Deletes node of ID NId from the network.
Definition: network.h:782
template<class TNodeData, class TEdgeData>
bool TNodeEDatNet< TNodeData, TEdgeData >::Empty ( ) const
inline

Tests whether the network is empty (has zero nodes).

Definition at line 700 of file network.h.

700 { return GetNodes()==0; }
int GetNodes() const
Returns the number of nodes in the network.
Definition: network.h:602
template<class TNodeData, class TEdgeData>
TEdgeI TNodeEDatNet< TNodeData, TEdgeData >::EndEI ( ) const
inline

Returns an iterator referring to the past-the-end edge in the network.

Definition at line 674 of file network.h.

674 { return TEdgeI(EndNI(), EndNI()); }
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the network.
Definition: network.h:628
template<class TNodeData, class TEdgeData>
TNodeI TNodeEDatNet< TNodeData, TEdgeData >::EndNI ( ) const
inline

Returns an iterator referring to the past-the-end node in the network.

Definition at line 628 of file network.h.

628 { return TNodeI(NodeH.EndI(), this); }
TIter EndI() const
Definition: hash.h:176
THash< TInt, TNode > NodeH
Definition: network.h:581
template<class TNodeData , class TEdgeData>
bool TNodeEDatNet< TNodeData, TEdgeData >::GetEDat ( const int &  SrcNId,
const int &  DstNId,
TEdgeData &  EdgeDat 
) const

Returns edge data in Data for the edge from node IDs SrcNId to DstNId.

Returns true, if the edge was found. Otherwise, returns false.

Definition at line 857 of file network.h.

857  {
858  if (! IsEdge(SrcNId, DstNId)) { return false; }
859  const TNode& N = GetNode(SrcNId);
860  EdgeDat = N.GetOutEDat(GetNIdPos(N.OutNIdV, DstNId));
861  return true;
862 }
static int GetNIdPos(const TVec< TPair< TInt, TEdgeData > > &NIdV, const int &NId)
Definition: network.h:739
TNode & GetNode(const int &NId)
Definition: network.h:576
bool IsEdge(const int &SrcNId, const int &DstNId, const bool &IsDir=true) const
Tests whether an edge from node IDs SrcNId to DstNId exists in the network.
Definition: network.h:843
template<class TNodeData , class TEdgeData>
TEdgeData & TNodeEDatNet< TNodeData, TEdgeData >::GetEDat ( const int &  SrcNId,
const int &  DstNId 
)

Returns edge data for the edge from node IDs SrcNId to DstNId.

Definition at line 865 of file network.h.

865  {
866  Assert(IsEdge(SrcNId, DstNId));
867  TNode& N = GetNode(SrcNId);
868  return N.GetOutEDat(GetNIdPos(N.OutNIdV, DstNId));
869 }
static int GetNIdPos(const TVec< TPair< TInt, TEdgeData > > &NIdV, const int &NId)
Definition: network.h:739
TNode & GetNode(const int &NId)
Definition: network.h:576
bool IsEdge(const int &SrcNId, const int &DstNId, const bool &IsDir=true) const
Tests whether an edge from node IDs SrcNId to DstNId exists in the network.
Definition: network.h:843
#define Assert(Cond)
Definition: bd.h:251
template<class TNodeData , class TEdgeData>
const TEdgeData & TNodeEDatNet< TNodeData, TEdgeData >::GetEDat ( const int &  SrcNId,
const int &  DstNId 
) const

Returns edge data for the edge from node IDs SrcNId to DstNId.

Definition at line 872 of file network.h.

872  {
873  Assert(IsEdge(SrcNId, DstNId));
874  const TNode& N = GetNode(SrcNId);
875  return N.GetOutEDat(GetNIdPos(N.OutNIdV, DstNId));
876 }
static int GetNIdPos(const TVec< TPair< TInt, TEdgeData > > &NIdV, const int &NId)
Definition: network.h:739
TNode & GetNode(const int &NId)
Definition: network.h:576
bool IsEdge(const int &SrcNId, const int &DstNId, const bool &IsDir=true) const
Tests whether an edge from node IDs SrcNId to DstNId exists in the network.
Definition: network.h:843
#define Assert(Cond)
Definition: bd.h:251
template<class TNodeData , class TEdgeData >
int TNodeEDatNet< TNodeData, TEdgeData >::GetEdges ( ) const

Returns the number of edges in the network.

Definition at line 802 of file network.h.

802  {
803  int edges=0;
804  for (int N=NodeH.FFirstKeyId(); NodeH.FNextKeyId(N); ) {
805  edges+=NodeH[N].GetOutDeg(); }
806  return edges;
807 }
THash< TInt, TNode > NodeH
Definition: network.h:581
bool FNextKeyId(int &KeyId) const
Definition: hash.h:432
int FFirstKeyId() const
Definition: hash.h:232
template<class TNodeData, class TEdgeData>
TEdgeI TNodeEDatNet< TNodeData, TEdgeData >::GetEI ( const int &  EId) const

Not supported/implemented!

template<class TNodeData , class TEdgeData >
TNodeEDatNet< TNodeData, TEdgeData >::TEdgeI TNodeEDatNet< TNodeData, TEdgeData >::GetEI ( const int &  SrcNId,
const int &  DstNId 
) const

Returns an iterator referring to edge (SrcNId, DstNId) in the network.

Definition at line 886 of file network.h.

886  {
887  const TNodeI SrcNI = GetNI(SrcNId);
888  int NodeN = -1;
889  //SrcNI.NodeHI.GetDat().OutNIdV.SearchBin(DstNId);
890  const TNIdDatPrV& NIdDatV = SrcNI.NodeHI.GetDat().OutNIdV;
891  int LValN=0, RValN=NIdDatV.Len()-1;
892  while (RValN>=LValN){
893  int ValN=(LValN+RValN)/2;
894  if (DstNId==NIdDatV[ValN].Val1){ NodeN=ValN; break; }
895  if (DstNId<NIdDatV[ValN].Val1){RValN=ValN-1;} else {LValN=ValN+1;}
896  }
897  if (NodeN == -1) { return EndEI(); }
898  else { return TEdgeI(SrcNI, EndNI(), NodeN); }
899 }
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the network.
Definition: network.h:674
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the network.
Definition: network.h:630
const TVal & GetDat(const TVal &Val) const
Returns reference to the first occurrence of element Val.
Definition: ds.h:792
TVec< TPair< TInt, TEdgeData > > TNIdDatPrV
Definition: network.h:447
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the network.
Definition: network.h:628
template<class TNodeData, class TEdgeData>
int TNodeEDatNet< TNodeData, TEdgeData >::GetMxNId ( ) const
inline

Returns an ID that is larger than any node ID in the network.

Definition at line 640 of file network.h.

640 { return MxNId; }
TInt MxNId
Definition: network.h:580
template<class TNodeData, class TEdgeData>
TNodeData& TNodeEDatNet< TNodeData, TEdgeData >::GetNDat ( const int &  NId)
inline

Returns node data for the node of ID NId in the network.

Definition at line 636 of file network.h.

636 { return NodeH.GetDat(NId).NodeDat; }
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
THash< TInt, TNode > NodeH
Definition: network.h:581
TNodeData NodeDat
Definition: network.h:452
template<class TNodeData, class TEdgeData>
const TNodeData& TNodeEDatNet< TNodeData, TEdgeData >::GetNDat ( const int &  NId) const
inline

Returns node data for the node of ID NId in the network.

Definition at line 638 of file network.h.

638 { return NodeH.GetDat(NId).NodeDat; }
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
THash< TInt, TNode > NodeH
Definition: network.h:581
TNodeData NodeDat
Definition: network.h:452
template<class TNodeData, class TEdgeData>
TNodeI TNodeEDatNet< TNodeData, TEdgeData >::GetNI ( const int &  NId) const
inline

Returns an iterator referring to the node of ID NId in the network.

Definition at line 630 of file network.h.

630 { return TNodeI(NodeH.GetI(NId), this); }
THash< TInt, TNode > NodeH
Definition: network.h:581
TIter GetI(const TKey &Key) const
Definition: hash.h:178
template<class TNodeData , class TEdgeData>
int TNodeEDatNet< TNodeData, TEdgeData >::GetNIdPos ( const TVec< TPair< TInt, TEdgeData > > &  NIdV,
const int &  NId 
)
staticprotected

Definition at line 739 of file network.h.

739  {
740  int LValN=0, RValN = NIdV.Len()-1;
741  while (RValN >= LValN) {
742  const int ValN = (LValN+RValN)/2;
743  const int CurNId = NIdV[ValN].Val1;
744  if (NId == CurNId) { return ValN; }
745  if (NId < CurNId) { RValN=ValN-1; }
746  else { LValN=ValN+1; }
747  }
748  return -1;
749 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:535
template<class TNodeData , class TEdgeData >
void TNodeEDatNet< TNodeData, TEdgeData >::GetNIdV ( TIntV NIdV) const

Gets a vector IDs of all nodes in the network.

Definition at line 902 of file network.h.

902  {
903  NIdV.Reserve(GetNodes(), 0);
904  for (int N=NodeH.FFirstKeyId(); NodeH.FNextKeyId(N); ) {
905  NIdV.Add(NodeH.GetKey(N)); }
906 }
THash< TInt, TNode > NodeH
Definition: network.h:581
bool FNextKeyId(int &KeyId) const
Definition: hash.h:432
int FFirstKeyId() const
Definition: hash.h:232
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:506
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:559
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:210
int GetNodes() const
Returns the number of nodes in the network.
Definition: network.h:602
template<class TNodeData, class TEdgeData>
TNode& TNodeEDatNet< TNodeData, TEdgeData >::GetNode ( const int &  NId)
inlineprotected

Definition at line 576 of file network.h.

576 { return NodeH.GetDat(NId); }
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
THash< TInt, TNode > NodeH
Definition: network.h:581
template<class TNodeData, class TEdgeData>
const TNode& TNodeEDatNet< TNodeData, TEdgeData >::GetNode ( const int &  NId) const
inline

Returns node element for the node of ID NId in the network.

Definition at line 632 of file network.h.

632 { return NodeH.GetDat(NId); }
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
THash< TInt, TNode > NodeH
Definition: network.h:581
template<class TNodeData, class TEdgeData>
int TNodeEDatNet< TNodeData, TEdgeData >::GetNodes ( ) const
inline

Returns the number of nodes in the network.

Definition at line 602 of file network.h.

602 { return NodeH.Len(); }
THash< TInt, TNode > NodeH
Definition: network.h:581
int Len() const
Definition: hash.h:186
template<class TNodeData, class TEdgeData>
TNodeI TNodeEDatNet< TNodeData, TEdgeData >::GetRndNI ( TRnd Rnd = TInt::Rnd)
inline

Returns an interator referring to a random node in the network.

Definition at line 695 of file network.h.

695 { return GetNI(GetRndNId(Rnd)); }
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the network.
Definition: network.h:630
int GetRndNId(TRnd &Rnd=TInt::Rnd)
Returns an ID of a random node in the network.
Definition: network.h:693
template<class TNodeData, class TEdgeData>
int TNodeEDatNet< TNodeData, TEdgeData >::GetRndNId ( TRnd Rnd = TInt::Rnd)
inline

Returns an ID of a random node in the network.

Definition at line 693 of file network.h.

693 { return NodeH.GetKey(NodeH.GetRndKeyId(Rnd, 0.8)); }
THash< TInt, TNode > NodeH
Definition: network.h:581
int GetRndKeyId(TRnd &Rnd) const
Get an index of a random element. If the hash table has many deleted keys, this may take a long time...
Definition: hash.h:398
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:210
template<class TNodeData , class TEdgeData >
bool TNodeEDatNet< TNodeData, TEdgeData >::HasFlag ( const TGraphFlag Flag) const

Allows for run-time checking the type of the network (see the TGraphFlag for flags).

Definition at line 734 of file network.h.

734  {
735  return HasGraphFlag(typename TNet, Flag);
736 }
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
Definition: gbase.h:38
TNodeEDatNet< TNodeData, TEdgeData > TNet
Definition: network.h:445
template<class TNodeData , class TEdgeData >
bool TNodeEDatNet< TNodeData, TEdgeData >::IsEdge ( const int &  SrcNId,
const int &  DstNId,
const bool &  IsDir = true 
) const

Tests whether an edge from node IDs SrcNId to DstNId exists in the network.

Definition at line 843 of file network.h.

843  {
844  if (! IsNode(SrcNId) || ! IsNode(DstNId)) { return false; }
845  if (IsDir) { return GetNode(SrcNId).IsOutNId(DstNId); }
846  else { return GetNode(SrcNId).IsOutNId(DstNId) || GetNode(DstNId).IsOutNId(SrcNId); }
847 }
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:624
bool IsOutNId(const int &NId) const
Definition: network.h:474
TNode & GetNode(const int &NId)
Definition: network.h:576
template<class TNodeData, class TEdgeData>
bool TNodeEDatNet< TNodeData, TEdgeData >::IsNode ( const int &  NId) const
inline

Tests whether ID NId is a node.

Definition at line 624 of file network.h.

624 { return NodeH.IsKey(NId); }
THash< TInt, TNode > NodeH
Definition: network.h:581
bool IsKey(const TKey &Key) const
Definition: hash.h:216
template<class TNodeData , class TEdgeData >
bool TNodeEDatNet< TNodeData, TEdgeData >::IsOk ( const bool &  ThrowExcept = true) const

Checks the network data structure for internal consistency.

For each node in the network check that its neighbors are also nodes in the network.

Definition at line 920 of file network.h.

920  {
921  bool RetVal = true;
922  for (int N = NodeH.FFirstKeyId(); NodeH.FNextKeyId(N); ) {
923  const TNode& Node = NodeH[N];
924  if (! Node.OutNIdV.IsSorted()) {
925  const TStr Msg = TStr::Fmt("Out-neighbor list of node %d is not sorted.", Node.GetId());
926  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
927  }
928  if (! Node.InNIdV.IsSorted()) {
929  const TStr Msg = TStr::Fmt("In-neighbor list of node %d is not sorted.", Node.GetId());
930  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
931  }
932  // check out-edges
933  int prevNId = -1;
934  for (int e = 0; e < Node.GetOutDeg(); e++) {
935  if (! IsNode(Node.GetOutNId(e))) {
936  const TStr Msg = TStr::Fmt("Out-edge %d --> %d: node %d does not exist.",
937  Node.GetId(), Node.GetOutNId(e), Node.GetOutNId(e));
938  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
939  }
940  if (e > 0 && prevNId == Node.GetOutNId(e)) {
941  const TStr Msg = TStr::Fmt("Node %d has duplidate out-edge %d --> %d.",
942  Node.GetId(), Node.GetId(), Node.GetOutNId(e));
943  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
944  }
945  prevNId = Node.GetOutNId(e);
946  }
947  // check in-edges
948  prevNId = -1;
949  for (int e = 0; e < Node.GetInDeg(); e++) {
950  if (! IsNode(Node.GetInNId(e))) {
951  const TStr Msg = TStr::Fmt("In-edge %d <-- %d: node %d does not exist.",
952  Node.GetId(), Node.GetInNId(e), Node.GetInNId(e));
953  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
954  }
955  if (e > 0 && prevNId == Node.GetInNId(e)) {
956  const TStr Msg = TStr::Fmt("Node %d has duplidate in-edge %d <-- %d.",
957  Node.GetId(), Node.GetId(), Node.GetInNId(e));
958  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
959  }
960  prevNId = Node.GetInNId(e);
961  }
962  }
963  return RetVal;
964 }
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:624
void ErrNotify(const char *NotifyCStr)
Definition: bd.h:74
THash< TInt, TNode > NodeH
Definition: network.h:581
bool FNextKeyId(int &KeyId) const
Definition: hash.h:432
int FFirstKeyId() const
Definition: hash.h:232
Definition: dt.h:412
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
#define EAssertR(Cond, MsgStr)
Definition: bd.h:283
char * CStr()
Definition: dt.h:476
template<class TNodeData, class TEdgeData>
static PNet TNodeEDatNet< TNodeData, TEdgeData >::Load ( TSIn SIn)
inlinestatic

Static constructor that loads the network from a stream SIn and returns a pointer to it.

Definition at line 595 of file network.h.

595 { return PNet(new TNet(SIn)); }
TPt< TNet > PNet
Definition: network.h:446
TNodeEDatNet< TNodeData, TEdgeData > TNet
Definition: network.h:445
template<class TNodeData, class TEdgeData>
static PNet TNodeEDatNet< TNodeData, TEdgeData >::New ( )
inlinestatic

Static constructor that returns a pointer to the network. Call: TPt <TNodeEDatNet<TNodeData, TEdgeData> > Net = TNodeEDatNet<TNodeData, TEdgeData>::New().

Definition at line 593 of file network.h.

593 { return PNet(new TNet()); }
TPt< TNet > PNet
Definition: network.h:446
TNodeEDatNet< TNodeData, TEdgeData > TNet
Definition: network.h:445
template<class TNodeData, class TEdgeData>
TNodeEDatNet& TNodeEDatNet< TNodeData, TEdgeData >::operator= ( const TNodeEDatNet< TNodeData, TEdgeData > &  NodeNet)
inline

Definition at line 598 of file network.h.

598  { if (this!=&NodeNet) {
599  NodeH=NodeNet.NodeH; MxNId=NodeNet.MxNId; } return *this; }
THash< TInt, TNode > NodeH
Definition: network.h:581
TInt MxNId
Definition: network.h:580
template<class TNodeData, class TEdgeData>
void TNodeEDatNet< TNodeData, TEdgeData >::Reserve ( const int &  Nodes,
const int &  Edges 
)
inline

Reserves memory for a network of Nodes nodes and Edges edges.

Definition at line 705 of file network.h.

705 { if (Nodes>0) { NodeH.Gen(Nodes/2); } }
THash< TInt, TNode > NodeH
Definition: network.h:581
void Gen(const int &ExpectVals)
Definition: hash.h:180
template<class TNodeData, class TEdgeData>
virtual void TNodeEDatNet< TNodeData, TEdgeData >::Save ( TSOut SOut) const
inlinevirtual

Saves the network to a (binary) stream SOut.

Definition at line 591 of file network.h.

591 { MxNId.Save(SOut); NodeH.Save(SOut); }
void Save(TSOut &SOut) const
Definition: dt.h:1058
void Save(TSOut &SOut) const
Definition: hash.h:141
THash< TInt, TNode > NodeH
Definition: network.h:581
TInt MxNId
Definition: network.h:580
template<class TNodeData , class TEdgeData>
void TNodeEDatNet< TNodeData, TEdgeData >::SetAllEDat ( const TEdgeData &  EdgeDat)

Sets edge data for all the edges in the network to EDat.

Definition at line 879 of file network.h.

879  {
880  for (TEdgeI EI = BegEI(); EI < EndEI(); EI++) {
881  EI() = EdgeDat;
882  }
883 }
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the network.
Definition: network.h:674
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the network.
Definition: network.h:672
template<class TNodeData , class TEdgeData>
void TNodeEDatNet< TNodeData, TEdgeData >::SetEDat ( const int &  SrcNId,
const int &  DstNId,
const TEdgeData &  EdgeDat 
)

Sets edge data for the edge between nodes SrcNId and DstNId in the network.

Definition at line 850 of file network.h.

850  {
851  IAssertR(IsNode(SrcNId) && IsNode(DstNId), TStr::Fmt("%d or %d not a node.", SrcNId, DstNId).CStr());
852  IAssertR(IsEdge(SrcNId, DstNId), TStr::Fmt("Edge between %d and %d does not exist.", SrcNId, DstNId).CStr());
853  GetEDat(SrcNId, DstNId) = EdgeDat;
854 }
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:624
#define IAssertR(Cond, Reason)
Definition: bd.h:265
bool GetEDat(const int &SrcNId, const int &DstNId, TEdgeData &EdgeDat) const
Returns edge data in Data for the edge from node IDs SrcNId to DstNId.
Definition: network.h:857
bool IsEdge(const int &SrcNId, const int &DstNId, const bool &IsDir=true) const
Tests whether an edge from node IDs SrcNId to DstNId exists in the network.
Definition: network.h:843
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
template<class TNodeData, class TEdgeData >
void TNodeEDatNet< TNodeData, TEdgeData >::SetNDat ( const int &  NId,
const TNodeData &  NodeDat 
)

Sets node data for the node of ID NId in the network.

Definition at line 776 of file network.h.

776  {
777  IAssertR(IsNode(NId), TStr::Fmt("NodeId %d does not exist.", NId).CStr());
778  NodeH.GetDat(NId).NodeDat = NodeDat;
779 }
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:624
#define IAssertR(Cond, Reason)
Definition: bd.h:265
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
THash< TInt, TNode > NodeH
Definition: network.h:581
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
TNodeData NodeDat
Definition: network.h:452
template<class TNodeData, class TEdgeData>
void TNodeEDatNet< TNodeData, TEdgeData >::SortNIdByDat ( const bool &  Asc = true)
inline

Sorts nodes by node data.

Definition at line 709 of file network.h.

709 { NodeH.SortByDat(Asc); }
THash< TInt, TNode > NodeH
Definition: network.h:581
void SortByDat(const bool &Asc=true)
Definition: hash.h:246
template<class TNodeData, class TEdgeData>
void TNodeEDatNet< TNodeData, TEdgeData >::SortNIdById ( const bool &  Asc = true)
inline

Sorts nodes by node IDs.

Definition at line 707 of file network.h.

707 { NodeH.SortByKey(Asc); }
THash< TInt, TNode > NodeH
Definition: network.h:581
void SortByKey(const bool &Asc=true)
Definition: hash.h:245

Friends And Related Function Documentation

template<class TNodeData, class TEdgeData>
friend class TPt< TNodeEDatNet< TNodeData, TEdgeData > >
friend

Definition at line 723 of file network.h.

Member Data Documentation

template<class TNodeData, class TEdgeData>
TCRef TNodeEDatNet< TNodeData, TEdgeData >::CRef
protected

Definition at line 579 of file network.h.

template<class TNodeData, class TEdgeData>
TInt TNodeEDatNet< TNodeData, TEdgeData >::MxNId
protected

Definition at line 580 of file network.h.

template<class TNodeData, class TEdgeData>
THash<TInt, TNode> TNodeEDatNet< TNodeData, TEdgeData >::NodeH
protected

Definition at line 581 of file network.h.


The documentation for this class was generated from the following file: