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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Jul 26 06:26:06 CEST 2013


Author: ortmann
Date: 2013-07-26 06:26:06 +0200 (Fri, 26 Jul 2013)
New Revision: 232

Added:
   pkg/RSienaTest/src/network/iterators/
   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:
added:
	ITieIterator interface
	Iterators allowing to compute the intersection, union and symmetric difference between two classes that implement ITieIterator

Added: pkg/RSienaTest/src/network/iterators/CombinedTieIterator.h
===================================================================
--- pkg/RSienaTest/src/network/iterators/CombinedTieIterator.h	                        (rev 0)
+++ pkg/RSienaTest/src/network/iterators/CombinedTieIterator.h	2013-07-26 04:26:06 UTC (rev 232)
@@ -0,0 +1,35 @@
+/*
+ * CombinedTieIterator.h
+ *
+ *  Created on: 25.07.2013
+ *      Author: ortmann
+ */
+
+#ifndef COMBINEDTIEITERATOR_H_
+#define COMBINEDTIEITERATOR_H_
+
+#include "ITieIterator.h"
+
+namespace siena {
+
+// currently not save ... will be solved once concepts become a standard
+template<class __iter1, class __iter2>
+class CombinedTieIterator: public ITieIterator {
+public:
+	virtual ~CombinedTieIterator() {
+	}
+
+protected:
+	CombinedTieIterator(__iter1 iter1, __iter2 iter2) :
+			ITieIterator(), m_iter1(iter1), m_iter2(iter2) {
+	}
+
+	inline bool isCommon() {
+		return m_iter1.actor() == m_iter2.actor();
+	}
+	__iter1 m_iter1;
+	__iter2 m_iter2;
+};
+
+} /* namespace siena */
+#endif /* COMBINEDTIEITERATOR_H_ */

Added: pkg/RSienaTest/src/network/iterators/ITieIterator.h
===================================================================
--- pkg/RSienaTest/src/network/iterators/ITieIterator.h	                        (rev 0)
+++ pkg/RSienaTest/src/network/iterators/ITieIterator.h	2013-07-26 04:26:06 UTC (rev 232)
@@ -0,0 +1,38 @@
+/*
+ * ITieIterator.h
+ *
+ *  Created on: 16.07.2013
+ *      Author: ortmann
+ */
+
+#ifndef ITIEITERATOR_H_
+#define ITIEITERATOR_H_
+
+#include "../../utils/Utils.h"
+
+namespace siena {
+
+class ITieIterator {
+public:
+	virtual ~ITieIterator() {
+	}
+
+	virtual int actor() const = 0;
+	virtual void next() = 0;
+	virtual bool valid() const = 0;
+
+protected:
+
+	ITieIterator() {
+	}
+
+	ITieIterator(const ITieIterator&) {
+	}
+
+	ITieIterator& operator=(const ITieIterator&) {
+		return *this;
+	}
+};
+
+} /* namespace siena */
+#endif /* ITIEITERATOR_H_ */

Added: pkg/RSienaTest/src/network/iterators/IntersectionTieIterator.h
===================================================================
--- pkg/RSienaTest/src/network/iterators/IntersectionTieIterator.h	                        (rev 0)
+++ pkg/RSienaTest/src/network/iterators/IntersectionTieIterator.h	2013-07-26 04:26:06 UTC (rev 232)
@@ -0,0 +1,71 @@
+/*
+ * IntersectionTieIterator.h
+ *
+ *  Created on: 25.07.2013
+ *      Author: ortmann
+ */
+
+#ifndef INTERSECTIONTIEITERATOR_H_
+#define INTERSECTIONTIEITERATOR_H_
+
+#include "CombinedTieIterator.h"
+
+namespace siena {
+
+template<class __iter1, class __iter2>
+class IntersectionTieIterator: public CombinedTieIterator<__iter1, __iter2 > {
+
+public:
+	IntersectionTieIterator(__iter1 iter1, __iter2 iter2) :
+			CombinedTieIterator<__iter1, __iter2 >(iter1, iter2) {
+		// move to the first common position
+		if (valid() && !isCommon()) {
+			skip();
+		}
+	}
+
+	~IntersectionTieIterator() {
+	}
+
+	inline int actor() const {
+		// we only have to check whether iter2 is valid cause
+		// iter1 will throw anyway
+		if (this->m_iter2.valid()) {
+			return this->m_iter1.actor();
+		}
+		throw InvalidIteratorException();
+	}
+
+	inline void next() {
+		this->m_iter1.next();
+		this->m_iter2.next();
+		skip();
+	}
+
+	inline void skip() {
+		while (valid() && !this->isCommon()) {
+			while (this->m_iter1.valid()
+					&& this->m_iter1.actor() < this->m_iter2.actor()) {
+				this->m_iter1.next();
+			}
+			if (!this->m_iter1.valid()) {
+				return;
+			}
+			while (this->m_iter2.valid()
+					&& this->m_iter2.actor() < this->m_iter1.actor()) {
+				this->m_iter2.next();
+			}
+		}
+	}
+
+	inline bool valid() const {
+		return this->m_iter1.valid() && this->m_iter2.valid();
+	}
+
+};
+
+//typedef IntersectionTieIterator<IncidentTieIterator, IncidentTieIterator> CommonNeighborIterator;
+
+} /* namespace siena */
+
+#endif /* INTERSECTIONTIEITERATOR_H_ */

Added: pkg/RSienaTest/src/network/iterators/SymDiffTieIterator.h
===================================================================
--- pkg/RSienaTest/src/network/iterators/SymDiffTieIterator.h	                        (rev 0)
+++ pkg/RSienaTest/src/network/iterators/SymDiffTieIterator.h	2013-07-26 04:26:06 UTC (rev 232)
@@ -0,0 +1,40 @@
+/*
+ * SymDiffTieIterator.h
+ *
+ *  Created on: 25.07.2013
+ *      Author: ortmann
+ */
+
+#ifndef SYMDIFFTIEITERATOR_H_
+#define SYMDIFFTIEITERATOR_H_
+
+#include "UnionTieIterator.h"
+
+namespace siena {
+
+template<class __iter1, class __iter2>
+class SymDiffTieIterator: public UnionTieIterator<__iter1, __iter2 > {
+
+public:
+	SymDiffTieIterator(__iter1 iter1, __iter2 iter2) :
+			UnionTieIterator<__iter1, __iter2 >(iter1, iter2) {
+		if (this->m_iter1.valid() && this->m_iter2.valid()
+				&& this->isCommon()) {
+			next();
+		}
+	}
+
+	~SymDiffTieIterator() {
+	}
+
+	inline void next() {
+		do {
+			UnionTieIterator<__iter1, __iter2 >::next();
+		} while (this->m_iter1.valid() && this->m_iter2.valid()
+				&& this->isCommon());
+	}
+};
+
+} /* namespace siena */
+
+#endif /* SYMDIFFTIEITERATOR_H_ */

Added: pkg/RSienaTest/src/network/iterators/UnionTieIterator.h
===================================================================
--- pkg/RSienaTest/src/network/iterators/UnionTieIterator.h	                        (rev 0)
+++ pkg/RSienaTest/src/network/iterators/UnionTieIterator.h	2013-07-26 04:26:06 UTC (rev 232)
@@ -0,0 +1,67 @@
+/*
+ * UnionTieIterator.h
+ *
+ *  Created on: 25.07.2013
+ *      Author: ortmann
+ */
+
+#ifndef UNIONTIEITERATOR_H_
+#define UNIONTIEITERATOR_H_
+
+#include "CombinedTieIterator.h"
+
+namespace siena {
+
+template<class __iter1, class __iter2>
+class UnionTieIterator: public CombinedTieIterator<__iter1, __iter2 > {
+
+public:
+	UnionTieIterator(__iter1 iter1, __iter2 iter2) :
+			CombinedTieIterator<__iter1, __iter2 >(iter1, iter2) {
+	}
+
+	virtual ~UnionTieIterator() {
+	}
+
+	inline int actor() const {
+		// if both iterators are valid return the actor with the lower value
+		if (this->m_iter1.valid() && this->m_iter2.valid()) {
+			return std::min(this->m_iter1.actor(), this->m_iter2.actor());
+		} else if (this->m_iter1.valid()) {
+			return this->m_iter1.actor();
+		} else if (this->m_iter2.valid()) {
+			return this->m_iter2.actor();
+		}
+		throw InvalidIteratorException();
+	}
+
+	virtual inline void next() {
+		// at least one of the iterators has to be valid
+		if (!valid()) {
+			return;
+		}
+		// if both iterators are valid move to the next position
+		if (this->m_iter1.valid() && this->m_iter2.valid()) {
+			if (this->m_iter1.actor() < this->m_iter2.actor()) {
+				this->m_iter1.next();
+			} else if (this->m_iter1.actor() > this->m_iter2.actor()) {
+				this->m_iter2.next();
+			} else {
+				this->m_iter1.next();
+				this->m_iter2.next();
+			}
+		} else if (this->m_iter1.valid()) {
+			this->m_iter1.next();
+		} else {
+			this->m_iter2.next();
+		}
+	}
+
+	inline bool valid() const {
+		return this->m_iter1.valid() || this->m_iter2.valid();
+	}
+};
+
+} /* namespace siena */
+
+#endif /* UNIONTIEITERATOR_H_ */



More information about the Rsiena-commits mailing list