[Rsiena-commits] r234 - in pkg/RSienaTest/src/network: . iterators

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Aug 2 09:58:47 CEST 2013


Author: ortmann
Date: 2013-08-02 09:58:46 +0200 (Fri, 02 Aug 2013)
New Revision: 234

Added:
   pkg/RSienaTest/src/network/iterators/CombinedTieIterator.cpp
   pkg/RSienaTest/src/network/iterators/IntersectionTieIterator.cpp
   pkg/RSienaTest/src/network/iterators/SymDiffTieIterator.cpp
   pkg/RSienaTest/src/network/iterators/UnionTieIterator.cpp
Modified:
   pkg/RSienaTest/src/network/CommonNeighborIterator.cpp
   pkg/RSienaTest/src/network/CommonNeighborIterator.h
   pkg/RSienaTest/src/network/IncidentTieIterator.cpp
   pkg/RSienaTest/src/network/IncidentTieIterator.h
   pkg/RSienaTest/src/network/Network.cpp
   pkg/RSienaTest/src/network/Network.h
   pkg/RSienaTest/src/network/OneModeNetwork.cpp
   pkg/RSienaTest/src/network/OneModeNetwork.h
   pkg/RSienaTest/src/network/iterators/CombinedTieIterator.h
   pkg/RSienaTest/src/network/iterators/ITieIterator.h
   pkg/RSienaTest/src/network/iterators/IntersectionTieIterator.h
   pkg/RSienaTest/src/network/iterators/SymDiffTieIterator.h
   pkg/RSienaTest/src/network/iterators/UnionTieIterator.h
Log:
changes:
	changed iterators to use virtual constructor (slower than templates, but less code bloat and better design)
	improved the time needed to insert a new edge
	added functionality to decide whether or not the given network is a OneModeNetwork
	added functionality to check whether or not the edge (i,j) exists

Modified: pkg/RSienaTest/src/network/CommonNeighborIterator.cpp
===================================================================
--- pkg/RSienaTest/src/network/CommonNeighborIterator.cpp	2013-07-26 04:32:48 UTC (rev 233)
+++ pkg/RSienaTest/src/network/CommonNeighborIterator.cpp	2013-08-02 07:58:46 UTC (rev 234)
@@ -1,8 +1,7 @@
 #include "CommonNeighborIterator.h"
 #include "../utils/Utils.h"
 
-namespace siena
-{
+namespace siena {
 
 // ----------------------------------------------------------------------------
 // Section: Construction area
@@ -12,19 +11,15 @@
  * Creates an iterator over actors common to both of the given incident
  * tie iterators.
  */
-CommonNeighborIterator::CommonNeighborIterator(IncidentTieIterator iter1,
-	IncidentTieIterator iter2)
-{
-	// Store the iterators.
-	
-	this->liter1 = iter1;
-	this->liter2 = iter2;
-	
+CommonNeighborIterator::CommonNeighborIterator(const IncidentTieIterator& iter1,
+		const IncidentTieIterator& iter2) :
+		ITieIterator(), //
+		liter1(iter1), //
+		liter2(iter2) {
 	// Make sure they point to the first common actor.
 	this->skipMismatches();
 }
 
-
 // ----------------------------------------------------------------------------
 // Section: Public interface
 // ----------------------------------------------------------------------------
@@ -32,19 +27,15 @@
 /**
  * Indicates if there are still some common actors to be reported.
  */
-bool CommonNeighborIterator::valid() const
-{
+bool CommonNeighborIterator::valid() const {
 	return this->liter1.valid() && this->liter2.valid();
 }
 
-
 /**
  * Returns the current common actor.
  */
-int CommonNeighborIterator::actor() const
-{
-	if (!this->valid())
-	{
+int CommonNeighborIterator::actor() const {
+	if (!this->valid()) {
 		throw InvalidIteratorException();
 	}
 
@@ -52,25 +43,25 @@
 	return this->liter1.actor();
 }
 
-
 /**
  * Moves on to the next common actor if any.
  */
-void CommonNeighborIterator::next()
-{
-	if (!this->valid())
-	{
+void CommonNeighborIterator::next() {
+	if (!this->valid()) {
 		throw InvalidIteratorException();
 	}
 
 	// Advance both iterators until they point to the next common actor
 	// or we run out of actors.
-	
+
 	this->liter1.next();
 	this->liter2.next();
 	this->skipMismatches();
 }
 
+CommonNeighborIterator* CommonNeighborIterator::clone() const {
+	return new CommonNeighborIterator(*this);
+}
 
 // ----------------------------------------------------------------------------
 // Section: Private methods
@@ -80,21 +71,27 @@
  * Advances both iterators until they point to the same actor. If there is
  * no such an actor, one of the iterators becomes invalid and we stop.
  */
-void CommonNeighborIterator::skipMismatches()
-{
-	while (this->liter1.valid() &&
-		this->liter2.valid() &&
-		this->liter1.actor() != this->liter2.actor())
-	{
-		if (this->liter1.actor() < this->liter2.actor())
-		{
-			this->liter1.next();
+void CommonNeighborIterator::skipMismatches() {
+	while (this->liter1.valid() && this->liter2.valid()
+			&& this->liter1.actor() != this->liter2.actor()) {
+		while (liter1.valid() && liter1.actor() < liter2.actor()) {
+			liter1.next();
 		}
-		else
-		{
-			this->liter2.next();
+		if (!liter1.valid()) {
+			return;
 		}
+		while (liter2.valid() && liter2.actor() < liter1.actor()) {
+			liter2.next();
+		}
 	}
 }
 
+CommonNeighborIterator::CommonNeighborIterator(
+		const CommonNeighborIterator& rhs) :
+		ITieIterator(rhs), //
+		liter1(rhs.liter1), //
+		liter2(rhs.liter2) {
+	// note there is no need to skip matches this has been down by rhs
 }
+
+}

Modified: pkg/RSienaTest/src/network/CommonNeighborIterator.h
===================================================================
--- pkg/RSienaTest/src/network/CommonNeighborIterator.h	2013-07-26 04:32:48 UTC (rev 233)
+++ pkg/RSienaTest/src/network/CommonNeighborIterator.h	2013-08-02 07:58:46 UTC (rev 234)
@@ -2,29 +2,31 @@
 #define COMMONNEIGHBORITERATOR_H_
 
 #include "IncidentTieIterator.h"
+#include "iterators/ITieIterator.h"
 
-namespace siena
-{
+namespace siena {
 
 /**
  * This class defines an iterator over actors that are common to a pair
  * of incident tie iterators.
  */
-class CommonNeighborIterator
-{
+class CommonNeighborIterator: ITieIterator {
 public:
-	CommonNeighborIterator(IncidentTieIterator iter1,
-		IncidentTieIterator iter2);
-	
+	CommonNeighborIterator(const IncidentTieIterator& iter1,
+			const IncidentTieIterator& iter2);
+	CommonNeighborIterator(const CommonNeighborIterator& rhs);
 	bool valid() const;
 	int actor() const;
 	void next();
-	
+	CommonNeighborIterator* clone() const;
+
 private:
 	void skipMismatches();
-	
+
 	IncidentTieIterator liter1;
 	IncidentTieIterator liter2;
+
+	CommonNeighborIterator& operator=(const CommonNeighborIterator&);
 };
 
 }

Modified: pkg/RSienaTest/src/network/IncidentTieIterator.cpp
===================================================================
--- pkg/RSienaTest/src/network/IncidentTieIterator.cpp	2013-07-26 04:32:48 UTC (rev 233)
+++ pkg/RSienaTest/src/network/IncidentTieIterator.cpp	2013-08-02 07:58:46 UTC (rev 234)
@@ -27,19 +27,29 @@
 // given map. The values of the pairs in the map represent the values
 // of ties, and the keys represent the corresponding neighbors.
 //
-IncidentTieIterator::IncidentTieIterator(std::map<int, int> & ties) :
+IncidentTieIterator::IncidentTieIterator(const std::map<int, int> & ties) :
 		ITieIterator(), //
 		lcurrent(ties.begin()), //
 		lend(ties.end()) {
 }
 
+IncidentTieIterator::IncidentTieIterator(const IncidentTieIterator& rhs) :
+		ITieIterator(rhs), //
+		lcurrent(rhs.lcurrent), //
+		lend(rhs.lend) {
+}
+
+IncidentTieIterator* IncidentTieIterator::clone() const {
+	return new IncidentTieIterator(*this);
+}
+
 //
 // Creates an iterator over a collection of ties represented by the
 // given map. The values of the pairs in the map represent the values
 // of ties, and the keys represent the corresponding neighbors. Only
 // neighbors that are greater or equal with the given bound are returned.
 //
-IncidentTieIterator::IncidentTieIterator(std::map<int, int> & ties,
+IncidentTieIterator::IncidentTieIterator(const std::map<int, int> & ties,
 		int lowerBound) :
 		ITieIterator(), //
 		lcurrent(ties.lower_bound(lowerBound)), //

Modified: pkg/RSienaTest/src/network/IncidentTieIterator.h
===================================================================
--- pkg/RSienaTest/src/network/IncidentTieIterator.h	2013-07-26 04:32:48 UTC (rev 233)
+++ pkg/RSienaTest/src/network/IncidentTieIterator.h	2013-08-02 07:58:46 UTC (rev 234)
@@ -26,9 +26,11 @@
 class IncidentTieIterator: public ITieIterator {
 	// The class Network needs access to the private constructor.
 	friend class Network;
+	friend class DistanceTwoView;
 
 public:
 	IncidentTieIterator();
+	IncidentTieIterator(const IncidentTieIterator& rhs);
 
 	/**
 	 * Returns the neighbor incident to the current tie.
@@ -64,9 +66,11 @@
 		++lcurrent;
 	}
 
+	IncidentTieIterator* clone() const;
+
 private:
-	IncidentTieIterator(std::map<int, int> & ties);
-	IncidentTieIterator(std::map<int, int> & ties, int lowerBound);
+	IncidentTieIterator(const std::map<int, int> & ties);
+	IncidentTieIterator(const std::map<int, int> & ties, int lowerBound);
 
 	// Points to the current element in the underlying map
 	std::map<int, int>::const_iterator lcurrent;

Modified: pkg/RSienaTest/src/network/Network.cpp
===================================================================
--- pkg/RSienaTest/src/network/Network.cpp	2013-07-26 04:32:48 UTC (rev 233)
+++ pkg/RSienaTest/src/network/Network.cpp	2013-08-02 07:58:46 UTC (rev 234)
@@ -17,9 +17,9 @@
 #include <map>
 #include <stdexcept>
 #include <limits>
+#include <algorithm>
 
-namespace siena
-{
+namespace siena {
 
 // ----------------------------------------------------------------------------
 // Section: Construction and destruction
@@ -28,15 +28,12 @@
 /**
  * Creates an empty network with <i>n</i> senders and <i>m</i> receivers.
  */
-Network::Network(int n, int m)
-{
-	if (n < 0)
-	{
+Network::Network(int n, int m) {
+	if (n < 0) {
 		throw std::invalid_argument("Negative number of senders specified");
 	}
 
-	if (m < 0)
-	{
+	if (m < 0) {
 		throw std::invalid_argument("Negative number of receivers specified");
 	}
 
@@ -50,16 +47,13 @@
 
 	// No ties for now
 	this->ltieCount = 0;
-
 	this->lmodificationCount = 0;
 }
 
-
 /**
  * Creates a copy of the given network.
  */
-Network::Network(const Network & rNetwork)
-{
+Network::Network(const Network & rNetwork) {
 	this->ln = rNetwork.ln;
 	this->lm = rNetwork.lm;
 
@@ -68,39 +62,32 @@
 
 	// Copy everything from rNetwork
 
-	for (int i = 0; i < this->ln; i++)
-	{
+	for (int i = 0; i < this->ln; ++i) {
 		this->lpOutTies[i].insert(rNetwork.lpOutTies[i].begin(),
-			rNetwork.lpOutTies[i].end());
+				rNetwork.lpOutTies[i].end());
 	}
 
-	for (int i = 0; i < this->lm; i++)
-	{
+	for (int i = 0; i < this->lm; ++i) {
 		this->lpInTies[i].insert(rNetwork.lpInTies[i].begin(),
-			rNetwork.lpInTies[i].end());
+				rNetwork.lpInTies[i].end());
 	}
 
 	this->ltieCount = rNetwork.ltieCount;
 	this->lmodificationCount = 0;
 }
 
-
 /**
  * Assigns the contents of the given network to this network.
  */
-Network & Network::operator=(const Network & rNetwork)
-{
-	if (this != &rNetwork)
-	{
+Network & Network::operator=(const Network & rNetwork) {
+	if (this != &rNetwork) {
 		// Empty the current network structure.
 
-		for (int i = 0; i < this->ln; i++)
-		{
+		for (int i = 0; i < this->ln; ++i) {
 			this->lpOutTies[i].clear();
 		}
 
-		for (int i = 0; i < this->lm; i++)
-		{
+		for (int i = 0; i < this->lm; ++i) {
 			this->lpInTies[i].clear();
 		}
 
@@ -116,20 +103,18 @@
 
 		// Copy everything from rNetwork
 
-		for (int i = 0; i < this->ln; i++)
-		{
+		for (int i = 0; i < this->ln; ++i) {
 			this->lpOutTies[i].insert(rNetwork.lpOutTies[i].begin(),
-				rNetwork.lpOutTies[i].end());
+					rNetwork.lpOutTies[i].end());
 		}
 
-		for (int i = 0; i < this->lm; i++)
-		{
+		for (int i = 0; i < this->lm; ++i) {
 			this->lpInTies[i].insert(rNetwork.lpInTies[i].begin(),
-				rNetwork.lpInTies[i].end());
+					rNetwork.lpInTies[i].end());
 		}
 
 		this->ltieCount = rNetwork.ltieCount;
-		this->lmodificationCount++;
+		++this->lmodificationCount;
 	}
 
 	return *this;
@@ -138,30 +123,25 @@
 /**
  * This method creates a copy of this network.
  */
-Network * Network::clone() const
-{
+Network * Network::clone() const {
 	return new Network(*this);
 }
 
-
 /**
  * This method allocates the memory for maps of incident ties and
  * various arrays of counters.
  */
-void Network::allocateArrays()
-{
+void Network::allocateArrays() {
 	// Allocate data structures
 
 	this->lpOutTies = new std::map<int, int>[this->ln];
 	this->lpInTies = new std::map<int, int>[this->lm];
 }
 
-
 /**
  * Deallocates various arrays used by this network.
  */
-void Network::deleteArrays()
-{
+void Network::deleteArrays() {
 	delete[] this->lpOutTies;
 	delete[] this->lpInTies;
 
@@ -169,16 +149,13 @@
 	this->lpInTies = 0;
 }
 
-
 /**
  * Destructs this network.
  */
-Network::~Network()
-{
+Network::~Network() {
 	this->deleteArrays();
 }
 
-
 // ----------------------------------------------------------------------------
 // Section: Accessors
 // ----------------------------------------------------------------------------
@@ -186,30 +163,24 @@
 /**
  * Returns the number of actors acting as tie senders in this network.
  */
-int Network::n() const
-{
+int Network::n() const {
 	return this->ln;
 }
 
-
 /**
  * Returns the number of actors acting as tie receivers in this network.
  */
-int Network::m() const
-{
+int Network::m() const {
 	return this->lm;
 }
 
-
 /**
  * Returns the total number of ties of this network
  */
-int Network::tieCount() const
-{
+int Network::tieCount() const {
 	return this->ltieCount;
 }
 
-
 // ----------------------------------------------------------------------------
 // Section: Basic structural operations
 // ----------------------------------------------------------------------------
@@ -217,133 +188,99 @@
 /**
  * This method sets the value of the tie from <i>i</i> to <i>j</i>.
  */
-void Network::setTieValue(int i, int j, int v)
-{
+void Network::setTieValue(int i, int j, int v) {
 	this->changeTieValue(i, j, v, REPLACE);
 }
 
-
 /**
  * This method increases the value of the tie from <i>i</i> to <i>j</i> by
  * <i>v</i> and returns the new value.
  */
-int Network::increaseTieValue(int i, int j, int v)
-{
+int Network::increaseTieValue(int i, int j, int v) {
 	return this->changeTieValue(i, j, v, INCREASE);
 }
 
-
 /**
  * This method changes the values of the tie from <i>i</i> to <i>j</i>
  * according to the specified type of change. The new value is returned as
  * the result.
  */
-int Network::changeTieValue(int i, int j, int v, ChangeType type)
-{
+int Network::changeTieValue(int i, int j, int v, ChangeType type) {
 	this->checkSenderRange(i);
 	this->checkReceiverRange(j);
 
 	// Retrieve the old value
-
-	std::map<int, int>::iterator iter = this->lpOutTies[i].find(j);
 	int oldValue = 0;
-
-	if (iter != this->lpOutTies[i].end())
-	{
+	std::map<int, int>& egoMap = lpOutTies[i];
+	std::map<int, int>::iterator iter = egoMap.lower_bound(j);
+	// we found the element
+	if (iter != egoMap.end() && !egoMap.key_comp()(j, iter->first)) {
 		oldValue = iter->second;
 	}
-
 	// Should we increase the value or replace?
 
-	if (type == INCREASE)
-	{
+	if (type == INCREASE) {
 		v += oldValue;
 	}
 
-	// Act on tie withdrawal or introduction.
-
-	if (oldValue && !v)
-	{
-		// The tie is withdrawn.
-		this->onTieWithdrawal(i, j);
-	}
-	else if (!oldValue && v)
-	{
-		// A new tie is introduced.
-		this->onTieIntroduction(i, j);
-	}
-
 	// Update the maps of incoming and outgoing ties
 
-	if (v == 0)
-	{
-		// The tie (i,j) should be removed from the maps, if it was
-		// explicit before
-
-		if (oldValue)
-		{
+	// if oldValue != 0 the (i,j) exists and we have to update the value
+	// or remove the tie. Otherwise, we have to insert the tie (i,j) if
+	// v!=0
+	if (oldValue) {
+		if (v == 0) {
 			// A non-zero tie becomes 0. Just remove the corresponding
 			// entries from the maps. Erasing an element pointed to by
 			// an iterator is potentially faster than removing by key,
 			// since we don't have to find the element.
-
-			this->lpOutTies[i].erase(iter);
+			egoMap.erase(iter);
 			this->lpInTies[j].erase(i);
-		}
-	}
-	else
-	{
-		// The new value of the tie is non-zero, so make sure it is stored
-		// in the maps.
-
-		if (oldValue)
-		{
-			// The tie was explicit before, and we have an iterator pointing
-			// to it in lpOutTies[i]. Simply replace the value, which is the
-			// second element of the pair pointed to by the iterator.
-
+		} else {
+			// the value of the edge has been changed
 			iter->second = v;
+			this->lpInTies[j][i] = v;
 		}
-		else
-		{
-			this->lpOutTies[i][j] = v;
-		}
-
-		this->lpInTies[j][i] = v;
+	} else if (v) {
+		// iter points to the element right after j. Using this position
+		// as a hint speeds things up.
+		egoMap.insert(iter, std::map<int, int>::value_type(j, v));
+		lpInTies[j].insert(std::map<int, int>::value_type(i, v));
 	}
-
 	// Remember that the network has changed
-	this->lmodificationCount++;
+	++this->lmodificationCount;
 
+	// Act on tie withdrawal or introduction.
+	if (oldValue && !v) {
+		// The (i,j) has been withdrawn.
+		this->onTieWithdrawal(i, j);
+	} else if (!oldValue && v) {
+		// A new tie has been introduced.
+		this->onTieIntroduction(i, j);
+	}
 	return v;
 }
 
-
 /**
  * Updates the state of this network to reflect the withdrawal of a tie
  * from actor <i>i</i> to actor <i>j</i>.
  */
-void Network::onTieWithdrawal(int i, int j)
-{
-	this->ltieCount--;
+void Network::onTieWithdrawal(int i, int j) {
+	--this->ltieCount;
 }
 
-
 /**
  * Updates the state of this network to reflect the introduction of a tie
  * from actor <i>i</i> to actor <i>j</i>.
  */
-void Network::onTieIntroduction(int i, int j)
-{
-	this->ltieCount++;
+void Network::onTieIntroduction(int i, int j) {
+	++this->ltieCount;
 }
 
-
 /**
  * Returns the value of the tie from <i>i</i> to <i>j</i>. The default is 0.
  */
-int Network::tieValue(int i, int j) const
-{
+int Network::tieValue(int i, int j) const {
 	this->checkSenderRange(i);
 	this->checkReceiverRange(j);
 
@@ -353,8 +290,7 @@
 	// The default value.
 	int v = 0;
 
-	if (iter != this->lpOutTies[i].end())
-	{
+	if (iter != this->lpOutTies[i].end()) {
 		// We have found an existing tie.
 		v = iter->second;
 	}
@@ -362,21 +298,17 @@
 	return v;
 }
 
-
 /**
  * This method removes all ties from this network.
  */
-void Network::clear()
-{
+void Network::clear() {
 	// Clear the maps and reset the various degree counters.
 
-	for (int i = 0; i < this->ln; i++)
-	{
+	for (int i = 0; i < this->ln; ++i) {
 		this->lpOutTies[i].clear();
 	}
 
-	for (int i = 0; i < this->lm; i++)
-	{
+	for (int i = 0; i < this->lm; ++i) {
 		this->lpInTies[i].clear();
 	}
 
@@ -384,42 +316,33 @@
 	this->ltieCount = 0;
 
 	// The network has changed
-	this->lmodificationCount++;
+	++this->lmodificationCount;
 }
 
-
 /**
  * Removes all incoming ties of the given actor.
  */
-void Network::clearInTies(int actor)
-{
+void Network::clearInTies(int actor) {
 	// We delegate to setTieValue such that various counters are updated
 	// correctly.
-
-	while (!this->lpInTies[actor].empty())
-	{
+	while (!this->lpInTies[actor].empty()) {
 		int sender = this->lpInTies[actor].begin()->first;
 		this->setTieValue(sender, actor, 0);
 	}
 }
 
-
 /**
  * Removes all outgoing ties of the given actor.
  */
-void Network::clearOutTies(int actor)
-{
+void Network::clearOutTies(int actor) {
 	// We delegate to setTieValue such that various counters are updated
 	// correctly.
-
-	while (!this->lpOutTies[actor].empty())
-	{
+	while (!this->lpOutTies[actor].empty()) {
 		int receiver = this->lpOutTies[actor].begin()->first;
 		this->setTieValue(actor, receiver, 0);
 	}
 }
 
-
 // ----------------------------------------------------------------------------
 // Section: Iterators
 // ----------------------------------------------------------------------------
@@ -427,54 +350,44 @@
 /**
  * Returns an iterator over all ties of this network.
  */
-TieIterator Network::ties() const
-{
+TieIterator Network::ties() const {
 	return TieIterator(this);
 }
 
-
 /**
  * Returns an iterator over incoming ties of the actor <i>i</i>.
  */
-IncidentTieIterator Network::inTies(int i) const
-{
+IncidentTieIterator Network::inTies(int i) const {
 	this->checkReceiverRange(i);
 	return IncidentTieIterator(this->lpInTies[i]);
 }
 
-
 /**
  * Returns an iterator over outgoing ties of the actor <i>i</i> with
  * the receiver not less than the given bound.
  */
-IncidentTieIterator Network::outTies(int i, int lowerBound) const
-{
+IncidentTieIterator Network::outTies(int i, int lowerBound) const {
 	this->checkSenderRange(i);
 	return IncidentTieIterator(this->lpOutTies[i], lowerBound);
 }
 
-
 /**
  * Returns an iterator over incoming ties of the actor <i>i</i> with
  * the sender not less than the given bound.
  */
-IncidentTieIterator Network::inTies(int i, int lowerBound) const
-{
+IncidentTieIterator Network::inTies(int i, int lowerBound) const {
 	this->checkReceiverRange(i);
 	return IncidentTieIterator(this->lpInTies[i], lowerBound);
 }
 
-
 /**
  * Returns an iterator over outgoing ties of the actor <i>i</i>
  */
-IncidentTieIterator Network::outTies(int i) const
-{
+IncidentTieIterator Network::outTies(int i) const {
 	this->checkSenderRange(i);
 	return IncidentTieIterator(this->lpOutTies[i]);
 }
 
-
 // ----------------------------------------------------------------------------
 // Section: Degrees
 // ----------------------------------------------------------------------------
@@ -482,23 +395,19 @@
 /**
  * Returns the number of incoming ties of the actor <i>i</i>.
  */
-int Network::inDegree(int i) const
-{
+int Network::inDegree(int i) const {
 	this->checkReceiverRange(i);
 	return this->lpInTies[i].size();
 }
 
-
 /**
  * Returns the number of outgoing ties of the actor <i>i</i>.
  */
-int Network::outDegree(int i) const
-{
+int Network::outDegree(int i) const {
 	this->checkSenderRange(i);
 	return this->lpOutTies[i].size();
 }
 
-
 // ----------------------------------------------------------------------------
 // Section: Some useful statistics
 // ----------------------------------------------------------------------------
@@ -507,8 +416,7 @@
  * Returns the minimal tie value in this network. The implicit values
  * of 0 are also considered.
  */
-int Network::minTieValue() const
-{
+int Network::minTieValue() const {
 	// This method is linear in the total number of ties. Try maintaining
 	// a sorted multi-set of tie values, if this method turns out to be
 	// a bottleneck. It would add a log(ltieCount) overhead to the method
@@ -516,13 +424,11 @@
 
 	int minValue = std::numeric_limits<int>::max();
 
-	for (TieIterator iter = this->ties(); iter.valid(); iter.next())
-	{
+	for (TieIterator iter = this->ties(); iter.valid(); iter.next()) {
 		minValue = std::min(minValue, iter.value());
 	}
 
-	if (!this->complete())
-	{
+	if (!this->complete()) {
 		// The network is not complete, hence some tie variables are 0.
 		minValue = std::min(minValue, 0);
 	}
@@ -530,13 +436,11 @@
 	return minValue;
 }
 
-
 /**
  * Returns the maximal tie value in this network. The implicit values
  * of 0 are also considered.
  */
-int Network::maxTieValue() const
-{
+int Network::maxTieValue() const {
 	// This method is linear in the total number of ties. Try maintaining
 	// a sorted multi-set of tie values, if this method turns out to be
 	// a bottleneck. It would add a log(ltieCount) overhead to the method
@@ -544,13 +448,11 @@
 
 	int maxValue = std::numeric_limits<int>::min();
 
-	for (TieIterator iter = this->ties(); iter.valid(); iter.next())
-	{
+	for (TieIterator iter = this->ties(); iter.valid(); iter.next()) {
 		maxValue = std::max(maxValue, iter.value());
 	}
 
-	if (!this->complete())
-	{
+	if (!this->complete()) {
 		// The network is not complete, hence some tie variables are 0.
 		maxValue = std::max(maxValue, 0);
 	}
@@ -558,50 +460,50 @@
 	return maxValue;
 }
 
-
 /**
  * This method returns the number of actors with ties to both <i>i</i> and
  * <i>j</i>.
  */
-int Network::outTwoStarCount(int i, int j) const
-{
+int Network::outTwoStarCount(int i, int j) const {
 	this->checkReceiverRange(i);
 	this->checkReceiverRange(j);
 	return commonActorCount(this->inTies(i), this->inTies(j));
 }
 
-
 /**
  * This method returns the number of actors with ties from both <i>i</i> and
  * <i>j</i>.
  */
-int Network::inTwoStarCount(int i, int j) const
-{
+int Network::inTwoStarCount(int i, int j) const {
 	this->checkSenderRange(i);
 	this->checkSenderRange(j);
 	return commonActorCount(this->outTies(i), this->outTies(j));
 }
 
-
 /**
  * Indicates that all ties are non-zero.
  */
-bool Network::complete() const
-{
+bool Network::complete() const {
 	// Return if all possible ties are non-zero.
 	return this->ltieCount == this->maxTieCount();
 }
 
+/**
+ * Tells whether there is an edge from <i>ego</i> to <i>alter</i>.
+ */
+bool Network::hasEdge(int ego, int alter) const {
+	checkSenderRange(ego);
+	checkReceiverRange(alter);
+	return lpOutTies[ego].find(alter) != lpOutTies[ego].end();
+}
 
 /**
  * Returns the maximal possible number of ties in this network.
  */
-int Network::maxTieCount() const
-{
+int Network::maxTieCount() const {
 	return this->ln * this->lm;
 }
 
-
 // ----------------------------------------------------------------------------
 // Section: Helper checks
 // ----------------------------------------------------------------------------
@@ -610,29 +512,25 @@
  * Tests if the given actor is in the valid range of senders and throws an
  * std::out_of_range exception, if not.
  */
-void Network::checkSenderRange(int i) const
-{
-	if (i < 0 || i >= this->ln)
-	{
-		throw std::out_of_range("The number " + toString(i) +
-			" is not in the range [0," + toString(this->ln) +
-			") of actors acting as senders of ties");
+void Network::checkSenderRange(int i) const {
+	if (i < 0 || i >= this->ln) {
+		throw std::out_of_range(
+				"The number " + toString(i) + " is not in the range [0,"
+						+ toString(this->ln)
+						+ ") of actors acting as senders of ties");
 	}
 }
 
-
 /**
  * Tests if the given actor is in the valid range of receivers and throws an
  * std::out_of_range exception, if not.
  */
-void Network::checkReceiverRange(int i) const
-{
-	if (i < 0 || i >= this->lm)
-	{
-		throw std::out_of_range("The number " + toString(i) +
-			" is not in the range [0," + toString(this->lm) +
-			") of actors acting as receivers of ties");
+void Network::checkReceiverRange(int i) const {
+	if (i < 0 || i >= this->lm) {
+		throw std::out_of_range(
+				"The number " + toString(i) + " is not in the range [0,"
+						+ toString(this->lm)
+						+ ") of actors acting as receivers of ties");
 	}
 }
-
 }

Modified: pkg/RSienaTest/src/network/Network.h
===================================================================
--- pkg/RSienaTest/src/network/Network.h	2013-07-26 04:32:48 UTC (rev 233)
+++ pkg/RSienaTest/src/network/Network.h	2013-08-02 07:58:46 UTC (rev 234)
@@ -14,8 +14,7 @@
 
 #include <map>
 
-namespace siena
-{
+namespace siena {
 
 // ----------------------------------------------------------------------------
 // Section: Forward declarations
@@ -24,14 +23,14 @@
 class TieIterator;
 class IncidentTieIterator;
 
-
 // ----------------------------------------------------------------------------
 // Section: Enums
 // ----------------------------------------------------------------------------
 
-enum ChangeType {REPLACE, INCREASE};
+enum ChangeType {
+	REPLACE, INCREASE
+};
 
-
 // ----------------------------------------------------------------------------
 // Section: Network class
 // ----------------------------------------------------------------------------
@@ -43,8 +42,7 @@
  * OneModeNetwork is recommended. The ties are valued and multiple ties between
  * the same pair of actors are forbiden.
  */
-class Network
-{
+class Network {
 public:
 	Network(int n, int m);
 	Network(const Network & rNetwork);
@@ -76,6 +74,8 @@
 	int maxTieValue() const;
 
 	bool complete() const;
+	bool hasEdge(int ego, int alter) const;
+	virtual bool isOneMode();
 
 	int outTwoStarCount(int i, int j) const;
 	int inTwoStarCount(int i, int j) const;
@@ -117,13 +117,8 @@
 	// is changed.
 
 	int lmodificationCount;
-
-	// whether this network is one mode or not;
-
-	bool loneMode;
 };
 
-
 // ----------------------------------------------------------------------------
 // Section: Inline methods
 // ----------------------------------------------------------------------------
@@ -131,8 +126,7 @@
 /**
  * Returns the number of times this network has been changed.
  */
-int Network::modificationCount() const
-{
+int Network::modificationCount() const {
 	return this->lmodificationCount;
 }
 

Modified: pkg/RSienaTest/src/network/OneModeNetwork.cpp
===================================================================
--- pkg/RSienaTest/src/network/OneModeNetwork.cpp	2013-07-26 04:32:48 UTC (rev 233)
+++ pkg/RSienaTest/src/network/OneModeNetwork.cpp	2013-08-02 07:58:46 UTC (rev 234)
@@ -17,8 +17,7 @@
 #include "network/IncidentTieIterator.h"
 #include "network/CommonNeighborIterator.h"
 
-namespace siena
-{
+namespace siena {
 
 // ----------------------------------------------------------------------------
 // Section: Construction and destruction
@@ -31,27 +30,23 @@
  * are permitted
  */
 OneModeNetwork::OneModeNetwork(int n, bool loopsPermitted) :
-	Network(n, n)
-{
+		Network(n, n) {
 	this->lloopsPermitted = loopsPermitted;
 
 	// Initialize the reciprocal degree counters
 
 	this->lpReciprocalDegree = new int[n];
 
-	for (int i = 0; i < n; i++)
-	{
+	for (int i = 0; i < n; i++) {
 		this->lpReciprocalDegree[i] = 0;
 	}
 }
 
-
 /**
  * Constructs a copy of the given one-mode network.
  */
 OneModeNetwork::OneModeNetwork(const OneModeNetwork & rNetwork) :
-	Network(rNetwork)
-{
+		Network(rNetwork) {
 	// Copy the fields
 	this->lloopsPermitted = rNetwork.lloopsPermitted;
 
@@ -59,20 +54,16 @@
 
 	this->lpReciprocalDegree = new int[rNetwork.n()];
 
-	for (int i = 0; i < rNetwork.n(); i++)
-	{
+	for (int i = 0; i < rNetwork.n(); i++) {
 		this->lpReciprocalDegree[i] = rNetwork.lpReciprocalDegree[i];
 	}
 }
 
-
 /**
  * Assigns the contents of the given network to this network.
  */
-OneModeNetwork & OneModeNetwork::operator=(const OneModeNetwork & rNetwork)
-{
-	if (this != &rNetwork)
-	{
+OneModeNetwork & OneModeNetwork::operator=(const OneModeNetwork & rNetwork) {
+	if (this != &rNetwork) {
 		// Let the base class do its part
 		(Network &) *this = rNetwork;
 
@@ -86,8 +77,7 @@
 
 		// Copy the reciprocal degree counters
 
-		for (int i = 0; i < rNetwork.n(); i++)
-		{
+		for (int i = 0; i < rNetwork.n(); i++) {
 			this->lpReciprocalDegree[i] = rNetwork.lpReciprocalDegree[i];
 		}
 	}
@@ -95,23 +85,18 @@
 	return *this;
 }
 
-
-Network * OneModeNetwork::clone() const
-{
+Network * OneModeNetwork::clone() const {
 	return new OneModeNetwork(*this);
 }
 
-
 /**
  * Deallocates this network.
  */
-OneModeNetwork::~OneModeNetwork()
-{
+OneModeNetwork::~OneModeNetwork() {
 	delete[] this->lpReciprocalDegree;
 	this->lpReciprocalDegree = 0;
 }
 
-
 // ----------------------------------------------------------------------------
 // Section: Accessors
 // ----------------------------------------------------------------------------
@@ -119,12 +104,10 @@
 /**
  * Indicates if loops are permitted.
  */
-bool OneModeNetwork::loopsPermitted() const
-{
+bool OneModeNetwork::loopsPermitted() const {
 	return this->lloopsPermitted;
 }
 
-
 // ----------------------------------------------------------------------------
 // Section: Basic structural operations
 // ----------------------------------------------------------------------------
@@ -134,82 +117,65 @@
  * according to the specified type of change. The new value is returned as
  * the result.
  */
-int OneModeNetwork::changeTieValue(int i, int j, int v, ChangeType type)
-{
-	if (i == j && !this->lloopsPermitted)
-	{
-		throw std::invalid_argument(
-			"Loops are not permitted for this network");
+int OneModeNetwork::changeTieValue(int i, int j, int v, ChangeType type) {
+	if (i == j && !this->lloopsPermitted) {
+		throw std::invalid_argument("Loops are not permitted for this network");
 	}
 
 	return Network::changeTieValue(i, j, v, type);
 }
 
-
 /**
  * Updates the state of this network to reflect the withdrawal of a tie
  * from actor <i>i</i> to actor <i>j</i>.
  */
-void OneModeNetwork::onTieWithdrawal(int i, int j)
-{
+void OneModeNetwork::onTieWithdrawal(int i, int j) {
 	// Call the base method first
 	Network::onTieWithdrawal(i, j);
 
 	// A reciprocal tie might be lost.
 
-	if (i == j)
-	{
+	if (i == j) {
 		// Careful with loops!
 		this->lpReciprocalDegree[i]--;
-	}
-	else if (this->tieValue(j, i))
-	{
+	} else if (this->tieValue(j, i)) {
 		this->lpReciprocalDegree[i]--;
 		this->lpReciprocalDegree[j]--;
 	}
 }
 
-
 /**
  * Updates the state of this network to reflect the introduction of a tie
  * from actor <i>i</i> to actor <i>j</i>.
  */
-void OneModeNetwork::onTieIntroduction(int i, int j)
-{
+void OneModeNetwork::onTieIntroduction(int i, int j) {
 	Network::onTieIntroduction(i, j);
 
 	// The tie might be reciprocated.
 
-	if (i == j)
-	{
+	if (i == j) {
 		// Careful with loops!
 		this->lpReciprocalDegree[i]++;
-	}
-	else if (this->tieValue(j, i))
-	{
+	} else if (this->tieValue(j, i)) {
 		this->lpReciprocalDegree[i]++;
 		this->lpReciprocalDegree[j]++;
 	}
 }
 
-
 /**
  * This method removes all ties from this network.
  */
-void OneModeNetwork::clear()
-{
+void OneModeNetwork::clear() {
 	// Let the base class do its part
 	Network::clear();
 
 	// Reset the degree counters.
 
-	for (int i = 0; i < this->n(); i++)
-	{
+	for (int i = 0; i < this->n(); i++) {
 		this->lpReciprocalDegree[i] = 0;
 	}
 }
 
-
 // ----------------------------------------------------------------------------
 // Section: Iterators
 // ----------------------------------------------------------------------------
@@ -217,26 +183,22 @@
 /**
  * Returns an iterator over reciprocated ties of the actor <i>i</i>.
  */
-CommonNeighborIterator OneModeNetwork::reciprocatedTies(int i) const
-{
+CommonNeighborIterator OneModeNetwork::reciprocatedTies(int i) const {
 	this->checkSenderRange(i);
 	return CommonNeighborIterator(this->inTies(i), this->outTies(i));
 }
 
-
 /**
  * Returns an iterator over reciprocated ties of the actor <i>i</i> with
  * the alter not less than the given bound.
  */
 CommonNeighborIterator OneModeNetwork::reciprocatedTies(int i,
-	int lowerBound) const
-{
+		int lowerBound) const {
 	this->checkSenderRange(i);
 	return CommonNeighborIterator(this->inTies(i, lowerBound),
-		this->outTies(i, lowerBound));
+			this->outTies(i, lowerBound));
 }
 
-
 // ----------------------------------------------------------------------------
 // Section: Degrees
 // ----------------------------------------------------------------------------
@@ -244,13 +206,11 @@
 /**
  * Returns the number of reciprocated ties of the actor <i>i</i>.
  */
-int OneModeNetwork::reciprocalDegree(int i) const
-{
+int OneModeNetwork::reciprocalDegree(int i) const {
 	this->checkSenderRange(i);
 	return this->lpReciprocalDegree[i];
 }
 
-
 // ----------------------------------------------------------------------------
 // Section: Some useful statistics and properties
 // ----------------------------------------------------------------------------
@@ -258,8 +218,7 @@
 /**
  * Indicates if all ties are reciprocated with the same value.
  */
-bool OneModeNetwork::symmetric() const
-{
+bool OneModeNetwork::symmetric() const {
 	// The current implementation is linear in the total number of ties.
[TRUNCATED]

To get the complete diff run:
    svnlook diff /svnroot/rsiena -r 234


More information about the Rsiena-commits mailing list