summaryrefslogtreecommitdiffstats
path: root/sca-cpp/trunk/kernel/list.hpp
diff options
context:
space:
mode:
authorjsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68>2010-01-02 10:27:26 +0000
committerjsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68>2010-01-02 10:27:26 +0000
commit95fa76f5f3208d913320c13a05171ecdcd7134c2 (patch)
tree872e101cd2fb1505baf313940e48c6b615fd6725 /sca-cpp/trunk/kernel/list.hpp
parent1d04916fda43146fb62488c20ba03b7b3006c8e9 (diff)
Performance improvements when running both in multi-threaded and pre-forked HTTPD. Changed memory management to use Apache APR pools instead of ref counting pointers as it's much faster and easier to integrate with the Python and Ruby interpreters. Changed to use simple pool-based string and stream implementations instead of the STL ones, which cause a lots of mutex locks in a multi-threaded environment. Added build options to compile with threading and profiling.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@895165 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-cpp/trunk/kernel/list.hpp')
-rw-r--r--sca-cpp/trunk/kernel/list.hpp110
1 files changed, 47 insertions, 63 deletions
diff --git a/sca-cpp/trunk/kernel/list.hpp b/sca-cpp/trunk/kernel/list.hpp
index 88163e63d2..653f49dfe5 100644
--- a/sca-cpp/trunk/kernel/list.hpp
+++ b/sca-cpp/trunk/kernel/list.hpp
@@ -26,9 +26,9 @@
* Simple list functions.
*/
-#include <string>
-#include <sstream>
-#include <iostream>
+#include <assert.h>
+#include "string.hpp"
+#include "fstream.hpp"
#include "function.hpp"
#include "debug.hpp"
@@ -56,28 +56,33 @@ bool checkListCounters() {
}
bool printListCounters() {
- std::cout << "countLists " << countLists << std::endl;
- std::cout << "countELists " << countELists << std::endl;
- std::cout << "countILists " << countILists << std::endl;
- std::cout << "countCLists " << countCLists << std::endl;
+ cout << "countLists " << countLists << endl;
+ cout << "countELists " << countELists << endl;
+ cout << "countILists " << countILists << endl;
+ cout << "countCLists " << countCLists << endl;
return true;
}
-#define debug_watchList() do { \
- this->watch = watchList(*this); \
- } while (0)
-
#else
#define resetListCounters()
#define checkListCounters() true
#define printListCounters()
+#endif
+
+#ifdef _DEBUG_WATCH
+
+#define debug_watchList() do { \
+ this->watch = watchList(*this); \
+ } while (0)
+
+#else
+
#define debug_watchList();
#endif
-
/**
* A car/cdr lisp-like pair, base structure to construct lists.
*/
@@ -91,28 +96,26 @@ public:
debug_watchList();
}
- list(const T car, const lambda<list<T>()>& cdr) :
- car(car), cdr(cdr) {
+ list(const T car, const lambda<list<T>()>& cdr) : car(car), cdr(cdr) {
debug_inc(countLists);
debug_inc(countILists);
debug_watchList();
}
- list(const list& p) :
- car(p.car), cdr(p.cdr) {
+ list(const list& p) : car(p.car), cdr(p.cdr) {
debug_inc(countLists);
debug_inc(countCLists);
-#ifdef _DEBUG
+#ifdef _DEBUG_WATCH
watch = p.watch;
#endif
}
- const list& operator=(const list<T>& p) {
+ const list<T>& operator=(const list<T>& p) {
if(this == &p)
return *this;
car = p.car;
cdr = p.cdr;
-#ifdef _DEBUG
+#ifdef _DEBUG_WATCH
watch = p.watch;
#endif
return *this;
@@ -172,41 +175,34 @@ public:
return (list<list<T> >)T(*this);
}
- list<T>& operator<<(const T& v) {
- *this = append(*this, mklist(v));
- return *this;
- }
+private:
+#ifdef _DEBUG_WATCH
+ template<typename X> friend const string watchList(const list<X>& p);
+ string watch;
+#endif
template<typename X> friend const bool isNil(const list<X>& p);
template<typename X> friend const X car(const list<X>& p);
template<typename X> friend const list<X> cdr(const list<X>& p);
- template<typename X> friend const bool setCar(list<X>& p, const X& car);
- template<typename X> friend const bool setCdr(list<X>& p, const list<X>& cdr);
- template<typename X> friend const bool setCdr(list<X>& p, const lambda<list<X>()>& cdr);
-
-private:
-#ifdef _DEBUG
- template<typename X> friend const std::string watchList(const list<X>& p);
- std::string watch;
-#endif
T car;
lambda<list<T>()> cdr;
};
-#ifdef _DEBUG
+#ifdef _DEBUG_WATCH
/**
* Debug utility used to write the contents of a list to a string, easier
* to watch than the list itself in a debugger.
*/
-template<typename T> const std::string watchList(const list<T>& p) {
+template<typename T> const string watchList(const list<T>& p) {
if(isNil(p))
return "()";
- std::ostringstream os;
+ ostringstream<string::npos> os;
os << "(" << car(p) << " ...)";
- return os.str();
+ return str(os);
}
+
#endif
/**
@@ -219,14 +215,14 @@ template<typename T> const bool isNil(const list<T>& p) {
/**
* Write a list to an output stream.
*/
-template<typename T> std::ostream& writeHelper(std::ostream& out, const list<T>& l) {
+template<typename T> ostream& writeHelper(ostream& out, const list<T>& l) {
if (isNil(l))
return out;
out << " " << car(l);
return writeHelper(out, cdr(l));
}
-template<typename T> std::ostream& operator<<(std::ostream& out, const list<T>& l) {
+template<typename T> ostream& operator<<(ostream& out, const list<T>& l) {
if(isNil(l))
return out << "()";
out << "(" << car(l);
@@ -305,6 +301,8 @@ template<typename T> const list<T> mklist(const T& a, const T& b, const T& c, co
* Returns the car of a list.
*/
template<typename T> const T car(const list<T>& p) {
+ // Abort if trying to access the car of a nil list
+ assert(!isNil(p.cdr));
return p.car;
}
@@ -316,31 +314,6 @@ template<typename T> const list<T> cdr(const list<T>& p) {
}
/**
- * Sets the car of a list.
- */
-template<typename T> const bool setCar(list<T>& p, const T& car) {
- p.car = car;
- return true;
-}
-
-/**
- * Sets the cdr of a list.
- */
-template<typename T> const bool setCdr(list<T>& p, const list<T>& c) {
- p.cdr = result(c);
- return true;
-}
-
-/**
- * Sets the cdr of a list to a lambda function.
- */
-template<typename T> const bool setCdr(list<T>& p, const lambda<list<T>()>& cdr) {
- p.cdr = cdr;
- return true;
-}
-
-
-/**
* Returns the car of the cdr of a list.
*/
template<typename T> const T cadr(const list<T>& p) {
@@ -419,6 +392,17 @@ template<typename T> const list<T> append(const list<T>&a, const list<T>& b) {
}
/**
+ * Append a value to a list.
+ */
+template<typename T> const list<T> operator+(const list<T>& l, const T& v) {
+ return append(l, mklist(v));
+}
+
+template<typename T, typename V> const list<T> operator+(const list<T>& l, const V& v) {
+ return append(l, mklist<T>(v));
+}
+
+/**
* Map a lambda function on a list.
*/
template<typename T, typename R> const list<R> map(const lambda<R(const T)>& f, const list<T>& p) {