/*
- * $Id: Array.h,v 1.15 2003/07/15 06:50:38 robertc Exp $
+ * $Id: Array.h,v 1.16 2003/09/22 08:50:51 robertc Exp $
*
* AUTHOR: Alex Rousskov
*
reserve(size() + app_count);
}
+template<class E>
+Vector<E>::Vector<E> (Vector const &rhs)
+{
+ items = NULL;
+ capacity = 0;
+ count = 0;
+ reserve (rhs.size());
+
+ for (size_t counter = 0; counter < rhs.size(); ++counter)
+ push_back (rhs.items[counter]);
+}
+
template<class E>
Vector<E> &
Vector<E>::operator = (Vector const &old)
/*
- * $Id: Range.h,v 1.4 2003/06/09 03:10:00 robertc Exp $
+ * $Id: Range.h,v 1.5 2003/09/22 08:50:51 robertc Exp $
*
*
* SQUID Web Proxy Cache http://www.squid-cache.org/
#ifndef SQUID_RANGE_H
#define SQUID_RANGE_H
+#include <iosfwd>
+
/* represents [start, end) */
template <class C>
size_t size() const;
};
+template <class C>
+std::ostream& operator << (std::ostream &os, Range<C> const &aRange)
+{
+ os << "[" << aRange.start << "," << aRange.end << ")";
+ return os;
+}
+
template<class C>
Range<C>::Range () : start(), end() {}
/*
- * $Id: splay.h,v 1.25 2003/09/22 03:31:03 robertc Exp $
+ * $Id: splay.h,v 1.26 2003/09/22 08:50:51 robertc Exp $
*/
#ifndef SQUID_SPLAY_H
SQUIDCEXTERN void splay_walk(splayNode *, SPLAYWALKEE *, void *);
#else
+#include "Stack.h"
template <class V>
template <class V>
+class SplayConstIterator;
+
+template <class V>
+
class SplayIterator;
template <class V>
typedef V Value;
typedef int SPLAYCMP(Value const &a, Value const &b);
typedef void SPLAYFREE(Value &);
+ typedef SplayIterator<V> iterator;
+ typedef const SplayConstIterator<V> const_iterator;
Splay():head(NULL), elements (0){}
mutable SplayNode<V> * head;
size_t size() const;
+ const_iterator begin() const;
+
+ const_iterator end() const;
+
size_t elements;
};
return elements;
}
-#endif
+template <class V>
+typename Splay<V>::const_iterator
+Splay<V>::begin() const
+{
+ return const_iterator(head);
+}
+
+template <class V>
+typename Splay<V>::const_iterator
+Splay<V>::end() const
+{
+ return const_iterator(NULL);
+}
+
+template <class V>
+
+class SplayConstIterator
+{
+
+public:
+ typedef const V value_type;
+ SplayConstIterator (SplayNode<V> *aNode);
+ bool operator == (SplayConstIterator const &right) const;
+ SplayConstIterator operator ++ (int dummy);
+ SplayConstIterator &operator ++ ();
+ V const & operator * () const;
+
+private:
+ void advance();
+ void addLeftPath(SplayNode<V> *aNode);
+ void init(SplayNode<V> *);
+ Stack<SplayNode<V> *> toVisit;
+};
+
+template <class V>
+SplayConstIterator<V>::SplayConstIterator (SplayNode<V> *aNode)
+{
+ init(aNode);
+}
+
+template <class V>
+bool
+SplayConstIterator<V>::operator == (SplayConstIterator const &right) const
+{
+ return toVisit.top() == right.toVisit.top();
+}
+
+template <class V>
+SplayConstIterator<V> &
+SplayConstIterator<V>::operator ++ ()
+{
+ advance();
+ return *this;
+}
+
+template <class V>
+SplayConstIterator<V>
+SplayConstIterator<V>::operator ++ (int dummy)
+{
+ SplayConstIterator<V> result = *this;
+ advance();
+ return result;
+}
+
+/* advance is simple enough:
+* if the stack is empty, we're done.
+* otherwise, pop the last visited node
+* then, pop the next node to visit
+* if that has a right child, add it and it's
+* left-to-end path.
+* then add the node back.
+*/
+template <class V>
+void
+SplayConstIterator<V>::advance()
+{
+ if (toVisit.size() == 0)
+ return;
+
+ toVisit.pop();
+
+ if (toVisit.size() == 0)
+ return;
+
+ SplayNode<V> *currentNode = toVisit.pop();
+
+ addLeftPath(currentNode->right);
+
+ toVisit.push_back(currentNode);
+}
+
+template <class V>
+void
+SplayConstIterator<V>::addLeftPath(SplayNode<V> *aNode)
+{
+ if (aNode == NULL)
+ return;
+
+ do {
+ toVisit.push_back(aNode);
+ aNode = aNode->left;
+ } while (aNode != NULL);
+}
+
+template <class V>
+void
+SplayConstIterator<V>::init(SplayNode<V> *head)
+{
+ addLeftPath(head);
+}
+
+template <class V>
+V const &
+SplayConstIterator<V>::operator * () const
+{
+ /* can't dereference when past the end */
+
+ if (toVisit.size() == 0)
+ fatal ("Attempt to dereference SplayConstIterator past-the-end\n");
+
+ return toVisit.top()->data;
+}
+
+#endif /* cplusplus */
#endif /* SQUID_SPLAY_H */
/*
- * $Id: Generic.h,v 1.5 2003/07/14 10:36:41 robertc Exp $
+ * $Id: Generic.h,v 1.6 2003/09/22 08:50:51 robertc Exp $
*
*
* SQUID Web Proxy Cache http://www.squid-cache.org/
return visitor;
};
+template <class InputIterator , class Visitor>
+Visitor& for_each(InputIterator from, InputIterator to, Visitor& visitor)
+{
+ while (!(from == to)) {
+ typename InputIterator::value_type &value = *from;
+ ++from;
+ visitor(value);
+ }
+
+ return visitor;
+}
+
+/* generic ostream printer */
+template <class Pointer>
+
+struct PointerPrinter
+{
+ PointerPrinter(std::ostream &astream, std::string aDelimiter) : os(astream), delimiter (aDelimiter) {}
+
+ void operator () (Pointer aNode)
+ {
+ os << *aNode << delimiter;
+ }
+
+ std::ostream &os;
+ std::string delimiter;
+};
+
#endif /* SQUID_GENERIC_H */
/*
- * $Id: mem_node.h,v 1.5 2003/08/04 22:14:42 robertc Exp $
+ * $Id: mem_node.h,v 1.6 2003/09/22 08:50:51 robertc Exp $
*
*
* SQUID Web Proxy Cache http://www.squid-cache.org/
static MemPool *pool;
};
+inline std::ostream &
+operator << (std::ostream &os, mem_node &aNode)
+{
+ os << aNode.nodeBuffer.range();
+ return os;
+}
#endif /* SQUID_MEM_NODE_H */
/*
- * $Id: stmem.cc,v 1.81 2003/09/06 12:47:35 robertc Exp $
+ * $Id: stmem.cc,v 1.82 2003/09/22 08:50:50 robertc Exp $
*
* DEBUG: section 19 Store Memory Primitives
* AUTHOR: Harvest Derived
#include "stmem.h"
#include "mem_node.h"
#include "MemObject.h"
+#include "Generic.h"
int
mem_hdr::lowestOffset () const
mem_hdr::endOffset () const
{
off_t result = 0;
- const SplayNode<mem_node *> *theEnd = nodes.end();
+ const SplayNode<mem_node *> *theEnd = nodes.finish();
if (theEnd)
result = theEnd->data->dataRange().end;
SplayNode<mem_node*> const * theStart = nodes.start();
- while (theStart && theStart != nodes.end() &&
+ while (theStart && theStart != nodes.finish() &&
theStart->data->end() <= (size_t) target_offset ) {
unlink(theStart->data);
theStart = nodes.start();
return;
}
- if (!nodes.end()->data->space())
+ if (!nodes.finish()->data->space())
appendNode (new mem_node (endOffset()));
- assert (nodes.end()->data->space());
+ assert (nodes.finish()->data->space());
}
void
while (len > 0) {
makeAppendSpace();
- int copied = appendToNode (nodes.end()->data, data, len);
+ int copied = appendToNode (nodes.finish()->data, data, len);
assert (copied);
len -= copied;
return copyLen;
}
+void
+mem_hdr::debugDump() const
+{
+ std::ostringstream result;
+ PointerPrinter<mem_node *> foo(result, " - ");
+ for_each (getNodes().begin(), getNodes().end(), foo);
+ debugs (19, 1, "mem_hdr::debugDump: Current available data is: " << result.str() << ".");
+}
+
/* FIXME: how do we deal with sparse results -
* where we have (say)
* 0-500 and 1000-1500, but are asked for
debug(19, 6) ("memCopy: offset %ld: size %u\n", (long int) offset, size);
+ /* we shouldn't ever ask for absent offsets */
+
if (nodes.size() == 0) {
- /* we shouldn't ever ask for absent offsets */
+ debugs(19, 1, "mem_hdr::copy: No data to read");
+ debugDump();
assert (0);
return 0;
}
if (!p) {
debug(19, 1) ("memCopy: could not find offset %u in memory.\n", (size_t) offset);
+ debugDump();
/* we shouldn't ever ask for absent offsets */
assert (0);
return 0;
mem_hdr::dump() const
{
debug(20, 1) ("mem_hdr: %p nodes.start() %p\n", this, nodes.start());
- debug(20, 1) ("mem_hdr: %p nodes.end() %p\n", this, nodes.end());
+ debug(20, 1) ("mem_hdr: %p nodes.finish() %p\n", this, nodes.finish());
}
size_t
return NULL;
}
+
+const Splay<mem_node *> &
+mem_hdr::getNodes() const
+{
+ return nodes;
+}
/*
- * $Id: stmem.h,v 1.5 2003/09/22 03:31:03 robertc Exp $
+ * $Id: stmem.h,v 1.6 2003/09/22 08:50:51 robertc Exp $
*
*
* SQUID Web Proxy Cache http://www.squid-cache.org/
static SplayNode<mem_node *>::SPLAYCMP NodeCompare;
private:
+ void debugDump() const;
void unlink(mem_node *aNode);
void makeAppendSpace();
int appendToNode(mem_node *aNode, const char *data, int maxLength);
/*
- * $Id: mem_hdr_test.cc,v 1.4 2003/09/22 03:31:02 robertc Exp $
+ * $Id: mem_hdr_test.cc,v 1.5 2003/09/22 08:50:51 robertc Exp $
*
* DEBUG: section 19 Store Memory Primitives
* AUTHOR: Robert Collins
aSplay.destroy(SplayNode<mem_node *>::DefaultFree);
}
-struct mem_node_print
-{
- mem_node_print(std::ostream &astream) : os(astream) {}
-
- void operator () (mem_node *aNode)
- {}
-
- std::ostream &os;
-};
-
void
testHdrVisit()
{
sampleData = xstrdup ("B");
assert (aHeader.write (StoreIOBuffer(1, 102, sampleData)));
safe_free (sampleData);
- //for_each (aHeader.getNodes().begin(). aHeader.getNodes().end(), mem_node_print(std::cout));
+ std::ostringstream result;
+ PointerPrinter<mem_node *> foo(result, "\n");
+ for_each (aHeader.getNodes().end(), aHeader.getNodes().end(), foo);
+ for_each (aHeader.getNodes().begin(), aHeader.getNodes().begin(), foo);
+ for_each (aHeader.getNodes().begin(), aHeader.getNodes().end(), foo);
+ std::ostringstream expectedResult;
+ expectedResult << "[100,101)" << std::endl << "[102,103)" << std::endl;
+ assert (result.str() == expectedResult.str());
}
int