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
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 444 of file network.h.

Member Typedef Documentation

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

Definition at line 449 of file network.h.

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

Definition at line 447 of file network.h.

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

Definition at line 448 of file network.h.

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

Definition at line 450 of file network.h.

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

Definition at line 446 of file network.h.

Constructor & Destructor Documentation

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

Definition at line 586 of file network.h.

586 : CRef(), MxNId(0), NodeH() { }
TCRef CRef
Definition: network.h:582
THash< TInt, TNode > NodeH
Definition: network.h:584
TInt MxNId
Definition: network.h:583
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 588 of file network.h.

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

Definition at line 589 of file network.h.

589 : MxNId(NodeNet.MxNId), NodeH(NodeNet.NodeH) { }
THash< TInt, TNode > NodeH
Definition: network.h:584
TInt MxNId
Definition: network.h:583
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 591 of file network.h.

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

Definition at line 592 of file network.h.

592 { }

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 813 of file network.h.

813  {
814  return AddEdge(SrcNId, DstNId, TEdgeData());
815 }
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge from node IDs SrcNId to node DstNId to the network.
Definition: network.h:813
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 818 of file network.h.

818  {
819  IAssertR(IsNode(SrcNId) && IsNode(DstNId), TStr::Fmt("%d or %d not a node.", SrcNId, DstNId).CStr());
820  //IAssert(! IsEdge(SrcNId, DstNId));
821  if (IsEdge(SrcNId, DstNId)) {
822  GetEDat(SrcNId, DstNId) = EdgeDat;
823  return -2;
824  }
825  GetNode(SrcNId).OutNIdV.AddSorted(TPair<TInt, TEdgeData>(DstNId, EdgeDat));
826  GetNode(DstNId).InNIdV.AddSorted(SrcNId);
827  return -1; // edge id
828 }
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:627
#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:860
TSizeTy AddSorted(const TVal &Val, const bool &Asc=true, const TSizeTy &_MxVals=-1)
Adds element Val to a sorted vector.
Definition: ds.h:1063
TNode & GetNode(const int &NId)
Definition: network.h:579
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:846
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
TNIdDatPrV OutNIdV
Definition: network.h:457
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 665 of file network.h.

665 { 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:813
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 755 of file network.h.

755  {
756  if (NId == -1) {
757  NId = MxNId; MxNId++;
758  } else {
759  IAssertR(!IsNode(NId), TStr::Fmt("NodeId %d already exists", NId));
760  MxNId = TMath::Mx(NId+1, MxNId());
761  }
762  NodeH.AddDat(NId, TNode(NId));
763  return NId;
764 }
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:627
#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:584
TInt MxNId
Definition: network.h:583
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 767 of file network.h.

767  {
768  if (NId == -1) {
769  NId = MxNId; MxNId++;
770  } else {
771  IAssertR(!IsNode(NId), TStr::Fmt("NodeId %d already exists", NId));
772  MxNId = TMath::Mx(NId+1, MxNId());
773  }
774  NodeH.AddDat(NId, TNode(NId, NodeDat));
775  return NId;
776 }
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:627
#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:584
TInt MxNId
Definition: network.h:583
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 619 of file network.h.

619 { return AddNode(NodeI.GetId(), NodeI.GetDat()); }
int AddNode(int NId=-1)
Adds a node of ID NId to the network.
Definition: network.h:755
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 675 of file network.h.

675 { 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:506
TNodeI BegNI() const
Returns an iterator referring to the first node in the network.
Definition: network.h:629
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the network.
Definition: network.h:631
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 629 of file network.h.

629 { return TNodeI(NodeH.BegI(), this); }
TIter BegI() const
Definition: hash.h:171
THash< TInt, TNode > NodeH
Definition: network.h:584
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 705 of file network.h.

705  {
706  MxNId = 0; NodeH.Clr(DoDel, -1, ResetDat); }
THash< TInt, TNode > NodeH
Definition: network.h:584
TInt MxNId
Definition: network.h:583
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
Definition: hash.h:319
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 912 of file network.h.

912  {
913  for (int n = NodeH.FFirstKeyId(); NodeH.FNextKeyId(n);) {
914  TNode& Node = NodeH[n];
915  Node.InNIdV.Pack(); Node.OutNIdV.Pack();
916  }
917  if (! OnlyNodeLinks && ! NodeH.IsKeyIdEqKeyN()) {
918  NodeH.Defrag();
919  }
920 }
bool IsKeyIdEqKeyN() const
Definition: hash.h:191
void Defrag()
Definition: hash.h:513
THash< TInt, TNode > NodeH
Definition: network.h:584
bool FNextKeyId(int &KeyId) const
Definition: hash.h:436
int FFirstKeyId() const
Definition: hash.h:236
void Pack()
Definition: hash.h:247
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 831 of file network.h.

831  {
832  IAssertR(IsNode(SrcNId) && IsNode(DstNId), TStr::Fmt("%d or %d not a node.", SrcNId, DstNId).CStr());
833  int pos = GetNIdPos(GetNode(SrcNId).OutNIdV, DstNId);
834  if (pos != -1) { GetNode(SrcNId).OutNIdV.Del(pos); }
835  pos = GetNode(DstNId).InNIdV.SearchBin(SrcNId);
836  if (pos != -1) { GetNode(DstNId).InNIdV.Del(pos); }
837  if (! IsDir) {
838  pos = GetNIdPos(GetNode(DstNId).OutNIdV, SrcNId);
839  if (pos != -1) { GetNode(DstNId).OutNIdV.Del(pos); }
840  pos = GetNode(SrcNId).InNIdV.SearchBin(DstNId);
841  if (pos != -1) { GetNode(SrcNId).InNIdV.Del(pos); }
842  }
843 }
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:627
static int GetNIdPos(const TVec< TPair< TInt, TEdgeData > > &NIdV, const int &NId)
Definition: network.h:742
#define IAssertR(Cond, Reason)
Definition: bd.h:265
void Del(const TSizeTy &ValN)
Removes the element at position ValN.
Definition: ds.h:1130
TNode & GetNode(const int &NId)
Definition: network.h:579
TSizeTy SearchBin(const TVal &Val) const
Returns the position of an element with value Val.
Definition: ds.h:1454
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
TNIdDatPrV OutNIdV
Definition: network.h:457
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 785 of file network.h.

785  {
786  const TNode& Node = GetNode(NId);
787  for (int out = 0; out < Node.GetOutDeg(); out++) {
788  const int nbr = Node.GetOutNId(out);
789  if (nbr == NId) { continue; }
790  TIntV& NIdV = GetNode(nbr).InNIdV;
791  const int pos = NIdV.SearchBin(NId);
792  if (pos != -1) { NIdV.Del(pos); }
793  }
794  for (int in = 0; in < Node.GetInDeg(); in++) {
795  const int nbr = Node.GetInNId(in);
796  if (nbr == NId) { continue; }
797  TNIdDatPrV& NIdDatV = GetNode(nbr).OutNIdV;
798  const int pos = GetNIdPos(NIdDatV, NId);
799  if (pos != -1) { NIdDatV.Del(pos); }
800  }
801  NodeH.DelKey(NId);
802 }
static int GetNIdPos(const TVec< TPair< TInt, TEdgeData > > &NIdV, const int &NId)
Definition: network.h:742
void Del(const TSizeTy &ValN)
Removes the element at position ValN.
Definition: ds.h:1130
THash< TInt, TNode > NodeH
Definition: network.h:584
void DelKey(const TKey &Key)
Definition: hash.h:362
TNode & GetNode(const int &NId)
Definition: network.h:579
TVec< TPair< TInt, TEdgeData > > TNIdDatPrV
Definition: network.h:450
TSizeTy SearchBin(const TVal &Val) const
Returns the position of an element with value Val.
Definition: ds.h:1454
TNIdDatPrV OutNIdV
Definition: network.h:457
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 625 of file network.h.

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

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

Definition at line 703 of file network.h.

703 { return GetNodes()==0; }
int GetNodes() const
Returns the number of nodes in the network.
Definition: network.h:605
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 677 of file network.h.

677 { return TEdgeI(EndNI(), EndNI()); }
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the network.
Definition: network.h:631
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 631 of file network.h.

631 { return TNodeI(NodeH.EndI(), this); }
TIter EndI() const
Definition: hash.h:176
THash< TInt, TNode > NodeH
Definition: network.h:584
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 860 of file network.h.

860  {
861  if (! IsEdge(SrcNId, DstNId)) { return false; }
862  const TNode& N = GetNode(SrcNId);
863  EdgeDat = N.GetOutEDat(GetNIdPos(N.OutNIdV, DstNId));
864  return true;
865 }
static int GetNIdPos(const TVec< TPair< TInt, TEdgeData > > &NIdV, const int &NId)
Definition: network.h:742
TNode & GetNode(const int &NId)
Definition: network.h:579
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:846
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 868 of file network.h.

868  {
869  Assert(IsEdge(SrcNId, DstNId));
870  TNode& N = GetNode(SrcNId);
871  return N.GetOutEDat(GetNIdPos(N.OutNIdV, DstNId));
872 }
static int GetNIdPos(const TVec< TPair< TInt, TEdgeData > > &NIdV, const int &NId)
Definition: network.h:742
TNode & GetNode(const int &NId)
Definition: network.h:579
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:846
#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 875 of file network.h.

875  {
876  Assert(IsEdge(SrcNId, DstNId));
877  const TNode& N = GetNode(SrcNId);
878  return N.GetOutEDat(GetNIdPos(N.OutNIdV, DstNId));
879 }
static int GetNIdPos(const TVec< TPair< TInt, TEdgeData > > &NIdV, const int &NId)
Definition: network.h:742
TNode & GetNode(const int &NId)
Definition: network.h:579
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:846
#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 805 of file network.h.

805  {
806  int edges=0;
807  for (int N=NodeH.FFirstKeyId(); NodeH.FNextKeyId(N); ) {
808  edges+=NodeH[N].GetOutDeg(); }
809  return edges;
810 }
THash< TInt, TNode > NodeH
Definition: network.h:584
bool FNextKeyId(int &KeyId) const
Definition: hash.h:436
int FFirstKeyId() const
Definition: hash.h:236
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 889 of file network.h.

889  {
890  const TNodeI SrcNI = GetNI(SrcNId);
891  int NodeN = -1;
892  //SrcNI.NodeHI.GetDat().OutNIdV.SearchBin(DstNId);
893  const TNIdDatPrV& NIdDatV = SrcNI.NodeHI.GetDat().OutNIdV;
894  int LValN=0, RValN=NIdDatV.Len()-1;
895  while (RValN>=LValN){
896  int ValN=(LValN+RValN)/2;
897  if (DstNId==NIdDatV[ValN].Val1){ NodeN=ValN; break; }
898  if (DstNId<NIdDatV[ValN].Val1){RValN=ValN-1;} else {LValN=ValN+1;}
899  }
900  if (NodeN == -1) { return EndEI(); }
901  else { return TEdgeI(SrcNI, EndNI(), NodeN); }
902 }
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the network.
Definition: network.h:677
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the network.
Definition: network.h:633
const TVal & GetDat(const TVal &Val) const
Returns reference to the first occurrence of element Val.
Definition: ds.h:807
TVec< TPair< TInt, TEdgeData > > TNIdDatPrV
Definition: network.h:450
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the network.
Definition: network.h:631
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 643 of file network.h.

643 { return MxNId; }
TInt MxNId
Definition: network.h:583
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 639 of file network.h.

639 { return NodeH.GetDat(NId).NodeDat; }
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
THash< TInt, TNode > NodeH
Definition: network.h:584
TNodeData NodeDat
Definition: network.h:455
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 641 of file network.h.

641 { return NodeH.GetDat(NId).NodeDat; }
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
THash< TInt, TNode > NodeH
Definition: network.h:584
TNodeData NodeDat
Definition: network.h:455
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 633 of file network.h.

633 { return TNodeI(NodeH.GetI(NId), this); }
THash< TInt, TNode > NodeH
Definition: network.h:584
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 742 of file network.h.

742  {
743  int LValN=0, RValN = NIdV.Len()-1;
744  while (RValN >= LValN) {
745  const int ValN = (LValN+RValN)/2;
746  const int CurNId = NIdV[ValN].Val1;
747  if (NId == CurNId) { return ValN; }
748  if (NId < CurNId) { RValN=ValN-1; }
749  else { LValN=ValN+1; }
750  }
751  return -1;
752 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
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 905 of file network.h.

905  {
906  NIdV.Reserve(GetNodes(), 0);
907  for (int N=NodeH.FFirstKeyId(); NodeH.FNextKeyId(N); ) {
908  NIdV.Add(NodeH.GetKey(N)); }
909 }
THash< TInt, TNode > NodeH
Definition: network.h:584
bool FNextKeyId(int &KeyId) const
Definition: hash.h:436
int FFirstKeyId() const
Definition: hash.h:236
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
Definition: ds.h:515
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:574
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:605
template<class TNodeData, class TEdgeData>
TNode& TNodeEDatNet< TNodeData, TEdgeData >::GetNode ( const int &  NId)
inlineprotected

Definition at line 579 of file network.h.

579 { return NodeH.GetDat(NId); }
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
THash< TInt, TNode > NodeH
Definition: network.h:584
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 635 of file network.h.

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

Returns the number of nodes in the network.

Definition at line 605 of file network.h.

605 { return NodeH.Len(); }
THash< TInt, TNode > NodeH
Definition: network.h:584
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 698 of file network.h.

698 { 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:633
int GetRndNId(TRnd &Rnd=TInt::Rnd)
Returns an ID of a random node in the network.
Definition: network.h:696
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 696 of file network.h.

696 { return NodeH.GetKey(NodeH.GetRndKeyId(Rnd, 0.8)); }
THash< TInt, TNode > NodeH
Definition: network.h:584
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:402
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 737 of file network.h.

737  {
738  return HasGraphFlag(typename TNet, Flag);
739 }
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
Definition: gbase.h:41
TNodeEDatNet< TNodeData, TEdgeData > TNet
Definition: network.h:448
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 846 of file network.h.

846  {
847  if (! IsNode(SrcNId) || ! IsNode(DstNId)) { return false; }
848  if (IsDir) { return GetNode(SrcNId).IsOutNId(DstNId); }
849  else { return GetNode(SrcNId).IsOutNId(DstNId) || GetNode(DstNId).IsOutNId(SrcNId); }
850 }
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:627
bool IsOutNId(const int &NId) const
Definition: network.h:477
TNode & GetNode(const int &NId)
Definition: network.h:579
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 627 of file network.h.

627 { return NodeH.IsKey(NId); }
THash< TInt, TNode > NodeH
Definition: network.h:584
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 923 of file network.h.

923  {
924  bool RetVal = true;
925  for (int N = NodeH.FFirstKeyId(); NodeH.FNextKeyId(N); ) {
926  const TNode& Node = NodeH[N];
927  if (! Node.OutNIdV.IsSorted()) {
928  const TStr Msg = TStr::Fmt("Out-neighbor list of node %d is not sorted.", Node.GetId());
929  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
930  }
931  if (! Node.InNIdV.IsSorted()) {
932  const TStr Msg = TStr::Fmt("In-neighbor list of node %d is not sorted.", Node.GetId());
933  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
934  }
935  // check out-edges
936  int prevNId = -1;
937  for (int e = 0; e < Node.GetOutDeg(); e++) {
938  if (! IsNode(Node.GetOutNId(e))) {
939  const TStr Msg = TStr::Fmt("Out-edge %d --> %d: node %d does not exist.",
940  Node.GetId(), Node.GetOutNId(e), Node.GetOutNId(e));
941  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
942  }
943  if (e > 0 && prevNId == Node.GetOutNId(e)) {
944  const TStr Msg = TStr::Fmt("Node %d has duplidate out-edge %d --> %d.",
945  Node.GetId(), Node.GetId(), Node.GetOutNId(e));
946  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
947  }
948  prevNId = Node.GetOutNId(e);
949  }
950  // check in-edges
951  prevNId = -1;
952  for (int e = 0; e < Node.GetInDeg(); e++) {
953  if (! IsNode(Node.GetInNId(e))) {
954  const TStr Msg = TStr::Fmt("In-edge %d <-- %d: node %d does not exist.",
955  Node.GetId(), Node.GetInNId(e), Node.GetInNId(e));
956  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
957  }
958  if (e > 0 && prevNId == Node.GetInNId(e)) {
959  const TStr Msg = TStr::Fmt("Node %d has duplidate in-edge %d <-- %d.",
960  Node.GetId(), Node.GetId(), Node.GetInNId(e));
961  if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false;
962  }
963  prevNId = Node.GetInNId(e);
964  }
965  }
966  return RetVal;
967 }
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:627
void ErrNotify(const char *NotifyCStr)
Definition: bd.h:74
THash< TInt, TNode > NodeH
Definition: network.h:584
bool FNextKeyId(int &KeyId) const
Definition: hash.h:436
int FFirstKeyId() const
Definition: hash.h:236
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 598 of file network.h.

598 { return PNet(new TNet(SIn)); }
TPt< TNet > PNet
Definition: network.h:449
TNodeEDatNet< TNodeData, TEdgeData > TNet
Definition: network.h:448
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 596 of file network.h.

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

Definition at line 601 of file network.h.

601  { if (this!=&NodeNet) {
602  NodeH=NodeNet.NodeH; MxNId=NodeNet.MxNId; } return *this; }
THash< TInt, TNode > NodeH
Definition: network.h:584
TInt MxNId
Definition: network.h:583
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 708 of file network.h.

708 { if (Nodes>0) { NodeH.Gen(Nodes/2); } }
THash< TInt, TNode > NodeH
Definition: network.h:584
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 594 of file network.h.

594 { MxNId.Save(SOut); NodeH.Save(SOut); }
void Save(TSOut &SOut) const
Definition: dt.h:1060
void Save(TSOut &SOut) const
Definition: hash.h:141
THash< TInt, TNode > NodeH
Definition: network.h:584
TInt MxNId
Definition: network.h:583
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 882 of file network.h.

882  {
883  for (TEdgeI EI = BegEI(); EI < EndEI(); EI++) {
884  EI() = EdgeDat;
885  }
886 }
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the network.
Definition: network.h:677
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the network.
Definition: network.h:675
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 853 of file network.h.

853  {
854  IAssertR(IsNode(SrcNId) && IsNode(DstNId), TStr::Fmt("%d or %d not a node.", SrcNId, DstNId).CStr());
855  IAssertR(IsEdge(SrcNId, DstNId), TStr::Fmt("Edge between %d and %d does not exist.", SrcNId, DstNId).CStr());
856  GetEDat(SrcNId, DstNId) = EdgeDat;
857 }
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:627
#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:860
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:846
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 779 of file network.h.

779  {
780  IAssertR(IsNode(NId), TStr::Fmt("NodeId %d does not exist.", NId).CStr());
781  NodeH.GetDat(NId).NodeDat = NodeDat;
782 }
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:627
#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:584
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
TNodeData NodeDat
Definition: network.h:455
template<class TNodeData, class TEdgeData>
void TNodeEDatNet< TNodeData, TEdgeData >::SortNIdByDat ( const bool &  Asc = true)
inline

Sorts nodes by node data.

Definition at line 712 of file network.h.

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

Sorts nodes by node IDs.

Definition at line 710 of file network.h.

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

Friends And Related Function Documentation

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

Definition at line 726 of file network.h.

Member Data Documentation

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

Definition at line 582 of file network.h.

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

Definition at line 583 of file network.h.

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

Definition at line 584 of file network.h.


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