Markos Zaharioudakis has proposed merging lp:~zorba-coders/zorba/markos-tempseq 
into lp:zorba.

Requested reviews:
  Markos Zaharioudakis (markos-za)

For more details, see:
https://code.launchpad.net/~zorba-coders/zorba/markos-tempseq/+merge/85602

Optimized and cleaned up SimpleTempSeq implementation and its usage.
-- 
https://code.launchpad.net/~zorba-coders/zorba/markos-tempseq/+merge/85602
Your team Zorba Coders is subscribed to branch lp:zorba.
=== modified file 'src/store/api/shared_types.h'
--- src/store/api/shared_types.h	2011-09-22 21:50:20 +0000
+++ src/store/api/shared_types.h	2011-12-14 04:15:30 +0000
@@ -62,6 +62,9 @@
 class TempSeq;
 typedef rchandle<TempSeq> TempSeq_t;
 
+class TempSeqIterator;
+typedef rchandle<TempSeqIterator> TempSeqIterator_t;
+
 class Collection;
 typedef rchandle<Collection> Collection_t;
 

=== modified file 'src/store/api/store.h'
--- src/store/api/store.h	2011-10-12 10:12:26 +0000
+++ src/store/api/store.h	2011-12-14 04:15:30 +0000
@@ -72,15 +72,17 @@
    *                   evaluated lazily. For XQueryP it might be necassary to set
    *                   this to false.
    */
-  virtual TempSeq_t createTempSeq(
-        Iterator_t& iterator,
-        bool copyNodes,
-        bool lazy) = 0;
-
-  /**
-   * Creates a new TempSeq that is initialized with the given vector.
-   */
-  virtual TempSeq_t createTempSeq(const std::vector<Item_t>& item_v) = 0;
+  virtual TempSeq_t createTempSeq(const Iterator_t& iterator, bool lazy) = 0;
+
+  /**
+   * Creates a new TempSeq that is initialized with the given vector.
+   */
+  virtual TempSeq_t createTempSeq(std::vector<Item_t>& item_v) = 0;
+
+  /**
+   * Creates a new TempSeq that is initialized with the given vector.
+   */
+  virtual TempSeq_t createTempSeq(Item_t& item) = 0;
 
   /**
    * Creates a new, empty TempSeq.

=== modified file 'src/store/api/temp_seq.h'
--- src/store/api/temp_seq.h	2011-06-28 23:34:08 +0000
+++ src/store/api/temp_seq.h	2011-12-14 04:15:30 +0000
@@ -39,56 +39,13 @@
   /**
    * Initializes a temp sequence with the given input iterator
    */
-  virtual void init(store::Iterator_t& iter, bool copy = false) = 0;
+  virtual void init(const store::Iterator_t& iter) = 0;
 
   /**
    * Appends the items from the iterator to the temp sequence
    */
-  virtual void append(Iterator_t iter, bool copy) = 0;
-	
-  /**
-   * Reads the whole Sequence from beginning to end; it is allowed to have several 
-   * concurrent iterators on the same TempSeq.
-   * 
-   * @return Iterator which iterates over the complete TempSeq
-   * 
-   */
-  virtual Iterator_t getIterator() = 0;
-	
-  /**
-   * Returns an iterator which reads just a part of the underlying TempSeq
-   * Starts counting with 1.
-   *
-   * @param startPos The first item which the iterator returns. Starts counting with 1.
-   * @param endPos The last item which the iterator returns 
-   * @return Iterator
-   */
-  virtual Iterator_t getIterator(
-        xs_integer startPos,
-        xs_integer endPos,
-        bool streaming = false) = 0;
-		
-  /**
-   * Gets an item at a certain position.
-   * 
-   * Starts counting with 1.
-   *
-   * @param position (first position in XQuery is 1 and not 0!)
-   * @return item
-   */
-  virtual void getItem(xs_integer position, Item_t& result) = 0;
-		
-		
-  /**
-   * Returns true if the item at the passed position is available.
-   * 
-   * Starts counting with 1.
-   *
-   * @param position 
-   * @return 
-   */
-  virtual bool containsItem(xs_integer position) = 0;
-		
+  virtual void append(const Iterator_t& iter) = 0;
+	
   /**
    * purge() allows the store to do proper garbage collection. If e.g. a let 
    * has created iterators for all his bindings he has to produce, it can
@@ -107,11 +64,54 @@
    * @param upTo boundary for garbage collector
    */
   virtual void purgeUpTo(xs_integer upTo) = 0;
-		
+
   /**
    * @return Does this TempSeq save an empty sequence? 
    */
   virtual bool empty() = 0;
+
+  /**
+   * Gets an item at a certain position.
+   * 
+   * Starts counting with 1.
+   *
+   * @param position (first position in XQuery is 1 and not 0!)
+   * @return item
+   */
+  virtual void getItem(xs_integer position, Item_t& result) = 0;
+		
+		
+  /**
+   * Returns true if the item at the passed position is available.
+   * 
+   * Starts counting with 1.
+   *
+   * @param position 
+   * @return 
+   */
+  virtual bool containsItem(xs_integer position) = 0;
+
+  /**
+   * Reads the whole Sequence from beginning to end; it is allowed to have several 
+   * concurrent iterators on the same TempSeq.
+   * 
+   * @return Iterator which iterates over the complete TempSeq
+   * 
+   */
+  virtual Iterator_t getIterator() const = 0;
+	
+  /**
+   * Returns an iterator which reads just a part of the underlying TempSeq
+   * Starts counting with 1.
+   *
+   * @param startPos The first item which the iterator returns. Starts counting with 1.
+   * @param endPos The last item which the iterator returns 
+   * @return Iterator
+   */
+  virtual Iterator_t getIterator(
+      xs_integer startPos,
+      xs_integer endPos,
+      bool streaming = false) const = 0;
 };
 
 } // namespace store

=== modified file 'src/store/naive/atomic_items.cpp'
--- src/store/naive/atomic_items.cpp	2011-10-27 20:55:51 +0000
+++ src/store/naive/atomic_items.cpp	2011-12-14 04:15:30 +0000
@@ -2441,10 +2441,8 @@
   }
   catch ( std::range_error const& )
   {
-    throw XQUERY_EXCEPTION(
-      err::FORG0001,
-      ERROR_PARAMS( theValue, ZED( CastFromToFailed_34 ), "integer", "long" )
-    );
+    RAISE_ERROR_NO_LOC(err::FORG0001,
+    ERROR_PARAMS(theValue, ZED(CastFromToFailed_34), "integer", "long"));
   }
 }
 

=== modified file 'src/store/naive/simple_lazy_temp_seq.cpp'
--- src/store/naive/simple_lazy_temp_seq.cpp	2011-07-13 17:53:51 +0000
+++ src/store/naive/simple_lazy_temp_seq.cpp	2011-12-14 04:15:30 +0000
@@ -16,7 +16,9 @@
 #include "stdafx.h"
 #include <limits>
 
+#include "diagnostics/xquery_exception.h"
 #include "diagnostics/assert.h"
+#include "diagnostics/util_macros.h"
 
 #include "store/api/item.h"
 #include "store/naive/simple_lazy_temp_seq.h"
@@ -31,9 +33,9 @@
 /*******************************************************************************
 
 ********************************************************************************/
-SimpleLazyTempSeq::SimpleLazyTempSeq(store::Iterator_t& aIter, bool aCopy)
+SimpleLazyTempSeq::SimpleLazyTempSeq(const store::Iterator_t& aIter)
 {
-  init(aIter, aCopy);
+  init(aIter);
 }
 
 
@@ -48,34 +50,9 @@
 /*******************************************************************************
 
 ********************************************************************************/
-bool SimpleLazyTempSeq::empty()
-{
-  if ( theItems.size() > 0 )
-  {
-    return false;
-  }
-  else
-  {
-    if ( theMatFinished )
-    {
-      return true;
-    }
-    else
-    {
-      matNextItem();
-      return theItems.empty();
-    }
-  }
-}
-
-
-/*******************************************************************************
-
-********************************************************************************/
-void SimpleLazyTempSeq::init(store::Iterator_t& aIter, bool aCopy)
+void SimpleLazyTempSeq::init(const store::Iterator_t& aIter)
 {
   theIterator = aIter;
-  theCopy = aCopy;
   theMatFinished = false;
   thePurgedUpTo = 0;
   theItems.clear();
@@ -85,15 +62,15 @@
 /*******************************************************************************
 
 ********************************************************************************/
-void SimpleLazyTempSeq::append(store::Iterator_t iter, bool copy)
+void SimpleLazyTempSeq::append(const store::Iterator_t& iter)
 {
   while (!theMatFinished)
   {
     matNextItem();
   }
+
   theMatFinished = false;
   theIterator = iter;
-  theCopy = copy;
 }
 
 
@@ -111,18 +88,17 @@
 ********************************************************************************/
 void SimpleLazyTempSeq::purgeUpTo(xs_integer upTo)
 {
-  ulong lUpTo;
-  try {
-    lUpTo = to_xs_unsignedLong(upTo);
-  } catch (std::range_error& e)
-  {
-    throw ZORBA_EXCEPTION(
-        zerr::ZSTR0060_RANGE_EXCEPTION,
-        ERROR_PARAMS(
-          BUILD_STRING("sequence too big (" << e.what() << ")")
-        )
-      );
-  }
+  xs_long lUpTo;
+  try 
+  {
+    lUpTo = to_xs_long(upTo);
+  }
+  catch (std::range_error& e)
+  {
+    throw ZORBA_EXCEPTION(zerr::ZSTR0060_RANGE_EXCEPTION,
+    ERROR_PARAMS(BUILD_STRING("sequence too big (" << e.what() << ")")));
+  }
+
   ZORBA_ASSERT(lUpTo >= thePurgedUpTo);
   ZORBA_ASSERT(lUpTo - thePurgedUpTo <= theItems.size());
 
@@ -132,12 +108,137 @@
 
 
 /*******************************************************************************
+
+********************************************************************************/
+bool SimpleLazyTempSeq::empty()
+{
+  if (!theItems.empty())
+  {
+    return false;
+  }
+  else
+  {
+    if (theMatFinished)
+    {
+      return true;
+    }
+    else
+    {
+      matNextItem();
+      return theItems.empty();
+    }
+  }
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+void SimpleLazyTempSeq::getItem(xs_integer position, store::Item_t& result)
+{
+  xs_long lPos;
+  try 
+  {
+    lPos = to_xs_long(position);
+  }
+  catch (std::range_error& e)
+  {
+    RAISE_ERROR_NO_LOC(zerr::ZSTR0060_RANGE_EXCEPTION,
+    ERROR_PARAMS(BUILD_STRING("access out of bounds " << e.what() << ")")));
+  }
+
+  if (this->containsItem(position)) 
+  {
+    result = theItems[lPos - thePurgedUpTo - 1];
+  }
+  else 
+  {
+    result = NULL;
+  }
+}
+
+
+/*******************************************************************************
+  This method checks if the i-th item in the result sequences of the input
+  iterator is in the queue or not. In general, the item may be missing from
+  the queue because:
+  (a) it has either been purged, or
+  (b) it has not been computed yet, or
+  (c) the result sequence contains less than i items.
+ 
+  Case (a) should never arise: it is the user of the lazy temp sequence that
+  decided when and how many items to purge, so he shouldn't come back to ask
+  for an item that has been purged.
+
+  In case (c), the method will compute and buffer any input items that have not
+  been computed already and then return false.
+
+  In case (b), the method will compute and buffer all the items starting after
+  the last computed item and up to the i-th item; then it will return true.
+
+  If the i-th item is already in the queue, the method will simply return true. 
+********************************************************************************/
+inline bool SimpleLazyTempSeq::containsItem(xs_integer position) 
+{
+  xs_long lPos;
+  try 
+  {
+    lPos = to_xs_long(position);
+  }
+  catch (std::range_error& e)
+  {
+    RAISE_ERROR_NO_LOC(zerr::ZSTR0060_RANGE_EXCEPTION,
+    ERROR_PARAMS(BUILD_STRING("access out of bounds " << e.what() << ")")));
+  }
+
+  assert(lPos > thePurgedUpTo);
+
+  xs_long numItemsToBuffer = lPos - thePurgedUpTo;
+
+  while (!theMatFinished && theItems.size() <  numItemsToBuffer) 
+  {
+    matNextItem();
+  }
+
+  return theItems.size() >= numItemsToBuffer;
+}
+
+
+/*******************************************************************************
+  Get the next item (if any) from the input iterator and put it at the end of 
+  the queue.  
+********************************************************************************/
+void SimpleLazyTempSeq::matNextItem() 
+{
+  theItems.push_back(NULL);
+
+  store::Item_t& lLocation = theItems.back();
+
+  if (theIterator->next(lLocation)) 
+  {
+    if (theCopy && lLocation->isNode()) 
+    {
+      store::CopyMode lCopyMode;
+      lLocation = lLocation->copy(NULL, lCopyMode);
+    }
+  }
+  else 
+  {
+    // We do not want to have an empty item materialized.
+    theItems.pop_back();
+    theMatFinished = true;
+    theIterator->close();
+  }
+}
+
+
+/*******************************************************************************
   Reads the whole Sequence from beginning to end; it is allowed to have several
   concurrent iterators on the same TempSeq.
 
   @return Iterator which iterates over the complete TempSeq
 ********************************************************************************/
-store::Iterator_t SimpleLazyTempSeq::getIterator()
+store::Iterator_t SimpleLazyTempSeq::getIterator() const
 {
   return new SimpleLazyTempSeqIter(this, 1, std::numeric_limits<long>::max());
 }
@@ -153,7 +254,7 @@
 store::Iterator_t SimpleLazyTempSeq::getIterator(
     xs_integer startPos,
     xs_integer endPos,
-    bool streaming)
+    bool streaming) const
 {
   return new SimpleLazyTempSeqIter(this, startPos, endPos);
 }
@@ -162,62 +263,23 @@
 /*******************************************************************************
 
 ********************************************************************************/
-store::Iterator_t SimpleLazyTempSeq::getIterator(
-    xs_integer startPos,
-    store::Iterator_t function,
-    const std::vector<store::Iterator_t>& vars,
-    bool streaming)
-{
-  assert(false); //Not implemented
-  return store::Iterator_t(NULL);
-}
-
-
-/*******************************************************************************
-
-********************************************************************************/
-store::Iterator_t SimpleLazyTempSeq::getIterator(
-    const std::vector<xs_integer>& positions,
-    bool streaming)
-{
-  assert (false); //Not implemented
-  return store::Iterator_t(NULL);
-}
-
-
-/*******************************************************************************
-
-********************************************************************************/
-store::Iterator_t SimpleLazyTempSeq::getIterator(
-    store::Iterator_t positions,
-    bool streaming)
-{
-  assert(false); //Not implemented
-  return store::Iterator_t ( NULL );
-}
-
-
-/*******************************************************************************
-
-********************************************************************************/
 SimpleLazyTempSeqIter::SimpleLazyTempSeqIter(
-    SimpleLazyTempSeq_t aTempSeq,
+    const SimpleLazyTempSeq* aTempSeq,
     xs_integer aStartPos,
     xs_integer aEndPos)
-  : theTempSeq(aTempSeq)
+  :
+  theTempSeq(const_cast<SimpleLazyTempSeq*>(aTempSeq))
 {
-  try {
-    theCurPos = to_xs_unsignedLong(aStartPos) - 1;
-    theStartPos = to_xs_unsignedLong(aStartPos);
-    theEndPos = to_xs_unsignedLong(aEndPos);
-  } catch (std::range_error& e)
-  {
-    throw ZORBA_EXCEPTION(
-        zerr::ZSTR0060_RANGE_EXCEPTION,
-        ERROR_PARAMS(
-          BUILD_STRING("sequence too big (" << e.what() << ")")
-        )
-      );
+  try 
+  {
+    theCurPos = to_xs_long(aStartPos) - 1;
+    theStartPos = to_xs_long(aStartPos);
+    theEndPos = to_xs_long(aEndPos);
+  }
+  catch (std::range_error& e)
+  {
+    RAISE_ERROR_NO_LOC(zerr::ZSTR0060_RANGE_EXCEPTION,
+    ERROR_PARAMS(BUILD_STRING("sequence too big (" << e.what() << ")")));
   }
 }
 

=== modified file 'src/store/naive/simple_lazy_temp_seq.h'
--- src/store/naive/simple_lazy_temp_seq.h	2011-07-29 23:01:30 +0000
+++ src/store/naive/simple_lazy_temp_seq.h	2011-12-14 04:15:30 +0000
@@ -51,71 +51,60 @@
   store::Iterator_t          theIterator;
   bool                       theCopy;
   bool                       theMatFinished;
-  uint64_t                      thePurgedUpTo;
+  xs_long                    thePurgedUpTo;
   
   std::vector<store::Item_t> theItems;
 
  public:
   SimpleLazyTempSeq() { }
 
-  SimpleLazyTempSeq(store::Iterator_t& iter, bool copy = false);
+  SimpleLazyTempSeq(const store::Iterator_t& iter);
 
   virtual ~SimpleLazyTempSeq();
 
-  bool empty();
-
-  void init(store::Iterator_t& iter, bool copy = false);
-
-  void append(store::Iterator_t iter, bool copy);
-
-  inline void getItem(xs_integer position, store::Item_t& result);
-
-  inline bool containsItem(xs_integer position);
-
-  store::Iterator_t getIterator();
-
-  store::Iterator_t getIterator(
-        xs_integer startPos,
-        xs_integer endPos,
-        bool streaming = false);
-
-  store::Iterator_t getIterator(
-        xs_integer startPos,
-        store::Iterator_t function,
-        const std::vector<store::Iterator_t>& var,
-        bool streaming = false);
-
-  store::Iterator_t getIterator(
-        const std::vector<xs_integer>& positions,
-        bool streaming = false);
-
-  store::Iterator_t getIterator(
-        store::Iterator_t positions,
-        bool streaming = false);
-        
+  void init(const store::Iterator_t& iter);
+
+  void append(const store::Iterator_t& iter);
+
   void purge();
 
   void purgeUpTo(xs_integer upTo);
 
+  bool empty();
+
+  void getItem(xs_integer position, store::Item_t& result);
+
+  bool containsItem(xs_integer position);
+
+  store::Iterator_t getIterator() const;
+
+  store::Iterator_t getIterator(
+      xs_integer startPos,
+      xs_integer endPos,
+      bool streaming = false) const;
+
  private:
-  inline void matNextItem();
+  void matNextItem();
 };
  
  
+/*******************************************************************************
+
+********************************************************************************/
 class SimpleLazyTempSeqIter : public store::Iterator 
 {
  private:
   SimpleLazyTempSeq_t  theTempSeq;
 
-  uint64_t             theCurPos;
-  uint64_t             theStartPos;
-  uint64_t             theEndPos;
+  xs_long              theCurPos;
+  xs_long              theStartPos;
+  xs_long              theEndPos;
 
  public:
   SimpleLazyTempSeqIter(
-        SimpleLazyTempSeq_t aTempSeq,
-        xs_integer aStartPos,
-        xs_integer aEndPos);
+      const SimpleLazyTempSeq* aTempSeq,
+      xs_integer aStartPos,
+      xs_integer aEndPos);
 
   virtual ~SimpleLazyTempSeqIter();
 
@@ -126,110 +115,6 @@
 };
 
 
-
-/*******************************************************************************
-
-********************************************************************************/
-inline void SimpleLazyTempSeq::getItem(xs_integer position, store::Item_t& result)
-{
-  uint64_t lPos;
-  try {
-    lPos = to_xs_unsignedLong(position);
-  } catch (std::range_error& e)
-  {
-    throw ZORBA_EXCEPTION(
-        zerr::ZSTR0060_RANGE_EXCEPTION,
-        ERROR_PARAMS(
-          BUILD_STRING("access out of bounds " << e.what() << ")")
-        )
-      );
-  }
-  if ( this->containsItem ( lPos ) ) 
-  {
-    result = theItems[lPos - thePurgedUpTo - 1];
-  }
-  else 
-  {
-    result = NULL;
-  }
-}
-
-
-/*******************************************************************************
-  This method checks if the i-th item in the result sequences of the input
-  iterator is in the queue or not. In general, the item may be missing from
-  the queue because:
-  (a) it has either been purged, or
-  (b) it has not been computed yet, or
-  (c) the result sequence contains less than i items.
- 
-  Case (a) should never arise: it is the user of the lazy temp sequence that
-  decided when and how many items to purge, so he shouldn't come back to ask
-  for an item that has been purged.
-
-  In case (c), the method will compute and buffer any input items that have not
-  been computed already and then return false.
-
-  In case (b), the method will compute and buffer all the items starting after
-  the last computed item and up to the i-th item; then it will return true.
-
-  If the i-th item is already in the queue, the method will simply return true. 
-********************************************************************************/
-inline bool SimpleLazyTempSeq::containsItem(xs_integer position) 
-{
-  uint64_t lPos;
-  try {
-    lPos = to_xs_unsignedLong(position);
-  } catch (std::range_error& e)
-  {
-    throw ZORBA_EXCEPTION(
-        zerr::ZSTR0060_RANGE_EXCEPTION,
-        ERROR_PARAMS(
-          BUILD_STRING("access out of bounds " << e.what() << ")")
-        )
-      );
-  }
-  assert(lPos > thePurgedUpTo);
-
-  uint64_t numItemsToBuffer = lPos - thePurgedUpTo;
-
-  while (!theMatFinished && theItems.size() <  numItemsToBuffer) 
-  {
-    matNextItem();
-  }
-
-  return theItems.size() >= numItemsToBuffer;
-}
-
-
-/*******************************************************************************
-  Get the next item (if any) from the input iterator and put it at the end of 
-  the queue.  
-********************************************************************************/
-inline void SimpleLazyTempSeq::matNextItem() 
-{
-  theItems.push_back(NULL);
-
-  store::Item_t& lLocation = theItems.back();
-
-  if (theIterator->next(lLocation)) 
-  {
-    if (theCopy && lLocation->isNode()) 
-    {
-      store::CopyMode lCopyMode;
-      lLocation = lLocation->copy(NULL, lCopyMode);
-    }
-  }
-  else 
-  {
-    // We do not want to have an empty item materialized.
-    theItems.pop_back();
-    theMatFinished = true;
-    theIterator->close();
-  }
-}
-
-
 } // namespace store
 } // namespace zorba
 

=== modified file 'src/store/naive/simple_store.cpp'
--- src/store/naive/simple_store.cpp	2011-11-17 03:04:59 +0000
+++ src/store/naive/simple_store.cpp	2011-12-14 04:15:30 +0000
@@ -1484,25 +1484,20 @@
   Creates a new TempSeq. The instance can be used, e.g. for variable bindings
 
   @param iterator   The source for the XMDInstance
-  @param copyNodes  If true, all nodes are copied before they are saved in the
-                    temp sequence.
   @param lazy       Hint for the store. If possible a XMDInstance should be
                     evaluated lazily. For XQueryP it might be necassary to set
                     this to false.
 ********************************************************************************/
-TempSeq_t SimpleStore::createTempSeq(
-    store::Iterator_t& iterator,
-    bool copyNodes,
-    bool lazy)
+TempSeq_t SimpleStore::createTempSeq(const store::Iterator_t& iterator, bool lazy)
 {
   if(lazy)
   {
     //tempSeq = new SimpleTempSeq(iterator, copyNodes);
-    return new SimpleLazyTempSeq(iterator, copyNodes);
+    return new SimpleLazyTempSeq(iterator);
   }
   else
   {
-    return new SimpleTempSeq(iterator, copyNodes);
+    return new SimpleTempSeq(iterator);
   }
 }
 
@@ -1512,7 +1507,6 @@
 ********************************************************************************/
 TempSeq_t SimpleStore::createTempSeq(bool lazy)
 {
-  TempSeq_t tempSeq;
   if (lazy)
   {
     return new SimpleLazyTempSeq();
@@ -1528,27 +1522,43 @@
   Creates a temp seq initialized by the given vector.
   @param item_v - The vector to use to initialize the seq.
 ********************************************************************************/
-TempSeq_t SimpleStore::createTempSeq(const std::vector<store::Item_t>& item_v)
-{
-  TempSeq_t tempSeq = new SimpleTempSeq(item_v);
-  return tempSeq;
-}
+TempSeq_t SimpleStore::createTempSeq(std::vector<store::Item_t>& items)
+{
+  return new SimpleTempSeq(items);
+}
+
+
+/*******************************************************************************
+  Creates a temp seq initialized by the given item.
+********************************************************************************/
+TempSeq_t SimpleStore::createTempSeq(store::Item_t& item)
+{
+  return new SimpleTempSeq(item);
+}
+
 
 #ifndef ZORBA_NO_FULL_TEXT
-void SimpleStore::setStemmerProvider( internal::StemmerProvider const *p ) {
+void SimpleStore::setStemmerProvider( internal::StemmerProvider const *p ) 
+{
   theStemmerProvider = p;
 }
 
-void SimpleStore::setTokenizerProvider( TokenizerProvider const *p ) {
+
+void SimpleStore::setTokenizerProvider( TokenizerProvider const *p ) 
+{
   theTokenizerProvider = p;
 }
 
-internal::StemmerProvider const* SimpleStore::getStemmerProvider() const {
+
+internal::StemmerProvider const* SimpleStore::getStemmerProvider() const 
+{
   return theStemmerProvider ?
     theStemmerProvider : &internal::StemmerProvider::get_default();
 }
 
-TokenizerProvider const* SimpleStore::getTokenizerProvider() const {
+
+TokenizerProvider const* SimpleStore::getTokenizerProvider() const 
+{
   return theTokenizerProvider ?
     theTokenizerProvider : &default_tokenizer_provider();
 }

=== modified file 'src/store/naive/simple_store.h'
--- src/store/naive/simple_store.h	2011-12-01 11:02:25 +0000
+++ src/store/naive/simple_store.h	2011-12-14 04:15:30 +0000
@@ -343,12 +343,11 @@
 
   store::TempSeq_t createTempSeq(bool lazy);
 
-  store::TempSeq_t createTempSeq(
-        store::Iterator_t& iterator,
-        bool copyNodes ,
-        bool lazy);
-
-  store::TempSeq_t createTempSeq(const std::vector<store::Item_t>& item_v);
+  store::TempSeq_t createTempSeq(const store::Iterator_t& iterator, bool lazy);
+
+  store::TempSeq_t createTempSeq(std::vector<store::Item_t>& item_v);
+
+  store::TempSeq_t createTempSeq(store::Item_t& item);
 
 #ifndef ZORBA_NO_FULL_TEXT
   internal::StemmerProvider const* getStemmerProvider() const;

=== modified file 'src/store/naive/simple_temp_seq.cpp'
--- src/store/naive/simple_temp_seq.cpp	2011-12-13 02:35:39 +0000
+++ src/store/naive/simple_temp_seq.cpp	2011-12-14 04:15:30 +0000
@@ -15,21 +15,32 @@
  */
 #include "stdafx.h"
 
-#include "diagnostics/zorba_exception.h"
+#include "diagnostics/xquery_exception.h"
 #include "zorba/diagnostic_list.h"
 #include "diagnostics/diagnostic.h"
+#include "diagnostics/util_macros.h"
+
 #include "store/api/item.h"
 #include "store/naive/simple_temp_seq.h"
 #include "store/api/copymode.h"
 
 namespace zorba { namespace simplestore {
 
+
 /*******************************************************************************
 
 ********************************************************************************/
-SimpleTempSeq::SimpleTempSeq(store::Iterator_t& iter, bool copy)
+SimpleTempSeq::SimpleTempSeq(std::vector<store::Item_t>& items)
 {
-  init(iter, copy);
+  theItems.resize(items.size());
+
+  std::vector<store::Item_t>::iterator ite = items.begin();
+  std::vector<store::Item_t>::iterator end = items.end();
+  std::vector<store::Item*>::iterator ite2 = theItems.begin();
+  for (; ite != end; ++ite, ++ite2)
+  {
+    (*ite2) = (*ite).release();
+  }
 }
 
 
@@ -38,6 +49,73 @@
 ********************************************************************************/
 SimpleTempSeq::~SimpleTempSeq()
 {
+  clear();
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+void SimpleTempSeq::clear()
+{
+  std::vector<store::Item*>::iterator ite = theItems.begin();
+  std::vector<store::Item*>::iterator end = theItems.end();
+  for (; ite != end; ++ite)
+  {
+    (*ite)->removeReference();
+  }
+
+  theItems.clear();
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+void SimpleTempSeq::init(const store::Iterator_t& iter)
+{
+  store::Item_t curItem;
+
+  clear();
+
+  while (iter->next(curItem)) 
+  {
+    // TODO ???? Check that the size is less than max(csize, xs_integer)
+    theItems.push_back(NULL);
+    theItems.back() = curItem.release();
+  }
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+void SimpleTempSeq::append(const store::Iterator_t& iter)
+{
+  store::Item_t curItem;
+
+  while (iter->next(curItem))
+  {
+    // TODO ???? Check that the size is less than max(csize, xs_integer)
+    theItems.push_back(NULL);
+    theItems.back() = curItem.release();
+  }
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+void SimpleTempSeq::purge()
+{
+}
+
+
+/*******************************************************************************
+
+********************************************************************************/
+void SimpleTempSeq::purgeUpTo(xs_integer upTo)
+{
 }
 
 
@@ -46,14 +124,14 @@
 ********************************************************************************/
 bool SimpleTempSeq::empty()
 {
-  return theItems.size() == 0;
+  return theItems.empty();
 }
 
 
 /*******************************************************************************
 
 ********************************************************************************/
-xs_integer SimpleTempSeq::getSize() 
+xs_integer SimpleTempSeq::getSize() const
 {
   return theItems.size();
 }
@@ -64,21 +142,20 @@
 ********************************************************************************/
 void SimpleTempSeq::getItem(xs_integer position, store::Item_t& res)
 {
-  uint64_t lPos;
-  try {
-    lPos = to_xs_unsignedLong(position);
-  } catch (std::range_error& e)
-  {
-    throw ZORBA_EXCEPTION(
-        zerr::ZSTR0060_RANGE_EXCEPTION,
-        ERROR_PARAMS(
-          BUILD_STRING("access out of bounds " << e.what() << ")")
-        )
-      );
-  }
-  if (containsItem(lPos))
+  xs_long pos;
+  try 
+  {
+    pos = to_xs_long(position);
+  }
+  catch (std::range_error& e)
+  {
+    RAISE_ERROR_NO_LOC(zerr::ZSTR0060_RANGE_EXCEPTION,
+    ERROR_PARAMS(BUILD_STRING("access out of bounds " << e.what() << ")")));
+  }
+
+  if (0 < pos && pos <= theItems.size())
 	{
-    res = theItems[lPos - 1];
+    res = theItems[pos - 1];
   }
   else
 	{
@@ -92,59 +169,18 @@
 ********************************************************************************/
 bool SimpleTempSeq::containsItem(xs_integer position)
 {
-  uint64_t lPos;
-  try {
-    lPos = to_xs_unsignedLong(position);
-  } catch (std::range_error& e)
-  {
-    throw ZORBA_EXCEPTION(
-        zerr::ZSTR0060_RANGE_EXCEPTION,
-        ERROR_PARAMS(
-          BUILD_STRING("access out of bounds " << e.what() << ")")
-        )
-      );
-  }
-  return theItems.size() >= lPos;
-}
-
-
-/*******************************************************************************
-
-********************************************************************************/
-void SimpleTempSeq::init(store::Iterator_t& iter, bool copy)
-{
-  store::CopyMode lCopyMode;
-  store::Item_t curItem;
-
-  theItems.clear();
-
-  while (iter->next(curItem)) 
-  {
-    if (copy && curItem->isNode()) 
-      curItem = curItem->copy(NULL, lCopyMode);
-
-    theItems.push_back(NULL);
-    theItems.back().transfer(curItem);
-  }
-}
-
-
-/*******************************************************************************
-
-********************************************************************************/
-void SimpleTempSeq::append(store::Iterator_t iter, bool copy)
-{
-  store::Item_t curItem;
-  store::CopyMode lCopyMode;
-
-  while (iter->next(curItem))
-  {
-    if (copy && curItem->isNode())
-      curItem = curItem->copy(NULL, lCopyMode);
-
-    theItems.push_back(NULL);
-    theItems.back().transfer(curItem);
-  }
+  xs_long pos;
+  try 
+  {
+    pos = to_xs_long(position);
+  }
+  catch (std::range_error& e)
+  {
+    RAISE_ERROR_NO_LOC(zerr::ZSTR0060_RANGE_EXCEPTION,
+    ERROR_PARAMS(BUILD_STRING("access out of bounds " << e.what() << ")")));
+  }
+
+  return 0 < pos && pos <= theItems.size();
 }
 
 
@@ -154,7 +190,7 @@
 
   @return Iterator which iterates over the complete TempSeq
 ********************************************************************************/
-store::Iterator_t SimpleTempSeq::getIterator()
+store::Iterator_t SimpleTempSeq::getIterator() const
 {
   return new SimpleTempSeqIter(this);
 }
@@ -170,7 +206,7 @@
 store::Iterator_t SimpleTempSeq::getIterator(
     xs_integer startPos,
     xs_integer endPos,
-    bool streaming)
+    bool streaming) const
 {
   return new SimpleTempSeqIter(this, startPos, endPos);
 }
@@ -178,86 +214,49 @@
 
 /*******************************************************************************
 
-********************************************************************************/
-store::Iterator_t SimpleTempSeq::getIterator(
-    xs_integer startPos,
-    store::Iterator_t function,
-    const std::vector<store::Iterator_t>& vars,
-    bool streaming)
-{
-  return store::Iterator_t ( NULL );
-}
-
-
-/*******************************************************************************
-
-********************************************************************************/
-store::Iterator_t SimpleTempSeq::getIterator(
-    const std::vector<xs_integer>& positions,
-    bool streaming)
-{
-  return store::Iterator_t ( NULL );
-}
-
-
-/*******************************************************************************
-
-********************************************************************************/
-store::Iterator_t SimpleTempSeq::getIterator(
-    store::Iterator_t positions,
-    bool streaming)
-{
-  return store::Iterator_t(NULL);
-}
-
-
-/*******************************************************************************
-
-********************************************************************************/
-void SimpleTempSeq::purge()
-{
-
-}
-
-
-/*******************************************************************************
-
-********************************************************************************/
-void SimpleTempSeq::purgeUpTo(xs_integer upTo)
-{
-
-}
-
-
-/*******************************************************************************
-
 ********************************************************************************/	
-SimpleTempSeqIter::SimpleTempSeqIter(
-    SimpleTempSeq_t aTempSeq)
+SimpleTempSeqIter::SimpleTempSeqIter(const SimpleTempSeq* aTempSeq)
 	:
-    theTempSeq(aTempSeq),
-    theBorderType(none),
-    theCurPos(-1)
+  theTempSeq(const_cast<SimpleTempSeq*>(aTempSeq)),
+  theBorderType(none)
 {
+  theBegin = theTempSeq->theItems.begin();
+  theEnd = theTempSeq->theItems.end();
 }
 
 
 SimpleTempSeqIter::SimpleTempSeqIter(
-    SimpleTempSeq_t aTempSeq,
+    const SimpleTempSeq* aTempSeq,
     xs_integer startPos,
     xs_integer endPos)
 	:
-		theTempSeq(aTempSeq),
-    theBorderType(startEnd),
-    theCurPos(to_xs_long(startPos) - 2),
-    theStartPos(to_xs_long(startPos)),
-    theEndPos(to_xs_long(endPos))
-{
-}
-
-
-SimpleTempSeqIter::~SimpleTempSeqIter()
-{
+  theTempSeq(const_cast<SimpleTempSeq*>(aTempSeq)),
+  theBorderType(startEnd)
+{
+  xs_long start;
+  xs_long end;
+
+  try 
+  {
+    start = to_xs_long(startPos);
+    end = to_xs_long(endPos);
+  }
+  catch (std::range_error& e)
+  {
+    RAISE_ERROR_NO_LOC(zerr::ZSTR0060_RANGE_EXCEPTION,
+    ERROR_PARAMS(BUILD_STRING("access out of bounds " << e.what() << ")")));
+  }
+
+  if (start > 0 && end > 0)
+  {
+    theBegin = theTempSeq->theItems.begin() + (start - 1);
+    theEnd = theTempSeq->theItems.begin() + end;
+  }
+  else
+  {
+    theBegin = theTempSeq->theItems.end();
+    theEnd = theTempSeq->theItems.end();
+  }
 }
 
 
@@ -265,45 +264,27 @@
 {
   theTempSeq = aTempSeq;
   theBorderType = none;
-  theCurPos = -1;
+
+  theBegin = theTempSeq->theItems.begin();
+  theEnd = theTempSeq->theItems.end();
 }
 
 
 void SimpleTempSeqIter::open()
 {
-  switch (theBorderType)
-	{
-  case startEnd:
-    theCurPos = theStartPos - 2;
-    break;
-  case none:
-    theCurPos = -1;
-    break;
-  }
+  theIte = theBegin;
 }
 
 
 bool SimpleTempSeqIter::next(store::Item_t& result)
 {
-  theCurPos++;
-  switch (theBorderType)
-	{
-  case none:
-    if ( theCurPos < to_xs_unsignedLong(theTempSeq->getSize()) ) 
-    {
-      result = theTempSeq->theItems[theCurPos];
-      return true;
-    }
-    break;
+  if (theIte != theEnd)
+  {
+    result = *theIte;
+    ++theIte;
+    return true;
+  }
 
-  case startEnd:
-    if ( theCurPos < theEndPos ) 
-    {
-      result = theTempSeq->theItems[theCurPos];
-      return true;
-    }
-    break;
-  }
   result = NULL;
   return false;
 }
@@ -311,17 +292,11 @@
 
 store::Item* SimpleTempSeqIter::next()
 {
-  theCurPos++;
-  switch (theBorderType)
-	{
-  case none:
-    if ( theCurPos < to_xs_unsignedLong(theTempSeq->getSize()) ) 
-      return theTempSeq->theItems[theCurPos];
-    break;
-  case startEnd:
-    if ( theCurPos < theEndPos ) 
-      return theTempSeq->theItems[theCurPos];
-    break;
+  if (theIte != theEnd)
+  {
+    store::Item* result = *theIte;
+    ++theIte;
+    return result;
   }
 
   return NULL;
@@ -330,15 +305,7 @@
 
 void SimpleTempSeqIter::reset()
 {
-  switch (theBorderType)
-	{
-  case startEnd:
-    theCurPos = theStartPos - 2;
-    break;
-  case none:
-    theCurPos = -1;
-    break;
-  }
+  theIte = theBegin;
 }
 
 

=== modified file 'src/store/naive/simple_temp_seq.h'
--- src/store/naive/simple_temp_seq.h	2011-12-13 02:35:39 +0000
+++ src/store/naive/simple_temp_seq.h	2011-12-14 04:15:30 +0000
@@ -38,52 +38,51 @@
   friend class SimpleTempSeqIter;
 
 private:
-  std::vector<store::Item_t> theItems;
+  std::vector<store::Item*> theItems; // ref-counting is done manually
+
+private:
+  void clear();
 
 public:
   SimpleTempSeq() { }
 
-  SimpleTempSeq(const std::vector<store::Item_t>& items) : theItems(items) {}
-
-  SimpleTempSeq(store::Iterator_t& iter, bool copy = false);
+  SimpleTempSeq(store::Item_t& item) 
+  {
+    theItems.push_back(NULL);
+    theItems[0] = item.release();
+  }
+
+  SimpleTempSeq(std::vector<store::Item_t>& items);
+
+  SimpleTempSeq(const store::Iterator_t& iter)
+  {
+    init(iter);
+  }
 
   virtual ~SimpleTempSeq();
 
+  void init(const store::Iterator_t& iter);
+
+  void append(const store::Iterator_t& iter);
+
+  void purge();
+
+  void purgeUpTo(xs_integer upTo);
+
   bool empty();
   
-  xs_integer getSize();
-
-  void init(store::Iterator_t& iter, bool copy = false);
-
-  void append(store::Iterator_t iter, bool copy);
+  xs_integer getSize() const;
 
   void getItem(xs_integer position, store::Item_t& res);
 
   bool containsItem(xs_integer position);
 
-  store::Iterator_t getIterator();
-
-  store::Iterator_t getIterator(
-        xs_integer startPos,
-        xs_integer endPos,
-        bool streaming = false);
-
-  store::Iterator_t getIterator(
-        xs_integer startPos,
-        store::Iterator_t function,
-        const std::vector<store::Iterator_t>& var,
-        bool streaming = false );
-
-  store::Iterator_t getIterator(
-        const std::vector<xs_integer>& positions,
-        bool streaming = false);
-
-  store::Iterator_t getIterator(
-        store::Iterator_t positions,
-        bool streaming = false);
-  
-  void purge();
-  void purgeUpTo(xs_integer upTo );
+  store::Iterator_t getIterator() const;
+
+  store::Iterator_t getIterator(
+      xs_integer startPos,
+      xs_integer endPos,
+      bool streaming = false) const;
 };
 
 
@@ -99,24 +98,24 @@
     startEnd
   };
 
-  SimpleTempSeq_t     theTempSeq;
-  BorderType          theBorderType;
+  SimpleTempSeq_t                      theTempSeq;
+  BorderType                           theBorderType;
 
-  uint64_t            theCurPos;
-  uint64_t            theStartPos;
-  uint64_t            theEndPos;
+  std::vector<store::Item*>::iterator  theIte;
+  std::vector<store::Item*>::iterator  theBegin;
+  std::vector<store::Item*>::iterator  theEnd;
 
  public:
   SimpleTempSeqIter() {}
 
-  SimpleTempSeqIter(SimpleTempSeq_t aTempSeq);
+  SimpleTempSeqIter(const SimpleTempSeq* aTempSeq);
 
   SimpleTempSeqIter(
-      SimpleTempSeq_t aTempSeq,
+      const SimpleTempSeq* aTempSeq,
       xs_integer startPos,
       xs_integer endPos);
 
-  virtual ~SimpleTempSeqIter();
+  ~SimpleTempSeqIter() {}
 
   void init(const store::TempSeq_t& seq);
 

-- 
Mailing list: https://launchpad.net/~zorba-coders
Post to     : zorba-coders@lists.launchpad.net
Unsubscribe : https://launchpad.net/~zorba-coders
More help   : https://help.launchpad.net/ListHelp

Reply via email to