[Liblas-commits] hg-main-tree: refactor to remove duplicated code

liblas-commits at liblas.org liblas-commits at liblas.org
Fri Mar 25 14:14:04 EDT 2011


details:   http://hg.libpc.orghg-main-tree/rev/e0605abd4e2b
changeset: 459:e0605abd4e2b
user:      Michael P. Gerlek <mpg at flaxen.com>
date:      Fri Mar 25 11:00:54 2011 -0700
description:
refactor to remove duplicated code
Subject: hg-main-tree: updated comments

details:   http://hg.libpc.orghg-main-tree/rev/9182d7217fa6
changeset: 460:9182d7217fa6
user:      Michael P. Gerlek <mpg at flaxen.com>
date:      Fri Mar 25 11:05:00 2011 -0700
description:
updated comments
Subject: hg-main-tree: refactor to remove duplicated code

details:   http://hg.libpc.orghg-main-tree/rev/f0e1e2cef160
changeset: 461:f0e1e2cef160
user:      Michael P. Gerlek <mpg at flaxen.com>
date:      Fri Mar 25 11:05:10 2011 -0700
description:
refactor to remove duplicated code
Subject: hg-main-tree: allow for 64b writes

details:   http://hg.libpc.orghg-main-tree/rev/b86cf9014e60
changeset: 462:b86cf9014e60
user:      Michael P. Gerlek <mpg at flaxen.com>
date:      Fri Mar 25 11:10:15 2011 -0700
description:
allow for 64b writes

diffstat:

 include/libpc/Iterator.hpp               |   5 +++
 include/libpc/Writer.hpp                 |   6 ++--
 include/libpc/filters/CacheFilter.hpp    |  13 +++++---
 src/Iterator.cpp                         |  47 ++++++++++++++++++++++++++++++++
 src/Writer.cpp                           |  16 ++++++----
 src/drivers/las/Iterator.cpp             |  12 +-------
 src/filters/CropFilterIterator.cpp       |  18 +-----------
 src/filters/DecimationFilterIterator.cpp |  17 +----------
 8 files changed, 76 insertions(+), 58 deletions(-)

diffs (255 lines):

diff -r cc6156bfe890 -r b86cf9014e60 include/libpc/Iterator.hpp
--- a/include/libpc/Iterator.hpp	Fri Mar 25 10:38:57 2011 -0700
+++ b/include/libpc/Iterator.hpp	Fri Mar 25 11:10:15 2011 -0700
@@ -78,6 +78,11 @@
 protected:
     virtual boost::uint32_t readImpl(PointBuffer&) = 0;
 
+    // This is provided as a sample implementation that some stages could use
+    // to implement their own skip or seek functions. It uses the read() call 
+    // to advance "count" points forward, so it is not at all optimal.
+    boost::uint64_t naiveSkipImpl(boost::uint64_t count);
+
     boost::uint64_t m_index;
 
 private:
diff -r cc6156bfe890 -r b86cf9014e60 include/libpc/Writer.hpp
--- a/include/libpc/Writer.hpp	Fri Mar 25 10:38:57 2011 -0700
+++ b/include/libpc/Writer.hpp	Fri Mar 25 11:10:15 2011 -0700
@@ -62,7 +62,7 @@
     // Read the  given number of points (or less, if the reader runs out first), 
     // and then write them out to wherever.  Returns total number of points
     // actually written.
-    boost::uint64_t write(std::size_t targetNumPointsToWrite);
+    boost::uint64_t write(boost::uint64_t targetNumPointsToWrite);
 
 protected:
     // this is called once before the loop with the writeBuffer calls
@@ -77,8 +77,8 @@
     Stage& getPrevStage();
 
     // these two are valid for use after writeBegin has been called
-    std::size_t m_actualNumPointsWritten;
-    std::size_t m_targetNumPointsToWrite;
+    boost::uint64_t m_actualNumPointsWritten;
+    boost::uint64_t m_targetNumPointsToWrite;
 
 private:
     Stage& m_prevStage;
diff -r cc6156bfe890 -r b86cf9014e60 include/libpc/filters/CacheFilter.hpp
--- a/include/libpc/filters/CacheFilter.hpp	Fri Mar 25 10:38:57 2011 -0700
+++ b/include/libpc/filters/CacheFilter.hpp	Fri Mar 25 11:10:15 2011 -0700
@@ -47,11 +47,14 @@
     
 namespace filters {
 
-// This is just a very simple MRU filter -- future versions will be smarter.
-// This cache has the following constraints:
-//   - only one block is cached
-//   - read chunk sizes are assumed to be just 1 point
-// If more than one point is read, the cache is skipped.
+//
+// This is just a very simple MRU cache filter. It has the following constraints:
+//   - up to 'numBlocks' will be cached
+//   - we cache blocks of points, of size 'blockSize'
+//   - we only cache full blocks
+//   - we only cache on 'blockSize' boundaries
+//   - we only look into the cache if 1 point is being requested
+//
 class LIBPC_DLL CacheFilter : public Filter
 {
 public:
diff -r cc6156bfe890 -r b86cf9014e60 src/Iterator.cpp
--- a/src/Iterator.cpp	Fri Mar 25 10:38:57 2011 -0700
+++ b/src/Iterator.cpp	Fri Mar 25 11:10:15 2011 -0700
@@ -34,11 +34,24 @@
 
 #include <libpc/Iterator.hpp>
 
+#include <algorithm> // for std::min/max
+
+#include <libpc/Stage.hpp>
+#include <libpc/Header.hpp>
+#include <libpc/PointBuffer.hpp>
+
 namespace libpc
 {
 
 static boost::uint32_t s_defaultChunkSize = 1024;
 
+
+//---------------------------------------------------------------------------
+//
+// Iterator
+//
+//---------------------------------------------------------------------------
+
 Iterator::Iterator(const Stage& stage)
     : m_index(0)
     , m_stage(stage)
@@ -88,7 +101,36 @@
 }
 
 
+boost::uint64_t Iterator::naiveSkipImpl(boost::uint64_t count)
+{
+    boost::uint64_t totalNumRead = 0;
 
+    // read (and discard) all the next 'count' points
+    // in case count is really big, we do this in blocks of size 'chunk'
+    while (count > 0)
+    {
+        const boost::uint64_t thisCount64 = std::min<boost::uint64_t>(getChunkSize(), count);
+        // getChunkSize is a uint32, so this cast is safe
+        const boost::uint32_t thisCount = static_cast<boost::uint32_t>(thisCount64);
+
+        PointBuffer junk(getStage().getHeader().getSchema(), thisCount);
+        
+        const boost::uint32_t numRead = read(junk);
+        if (numRead == 0) break; // end of file or something
+
+        count -= numRead;
+        totalNumRead += numRead;
+    }
+
+    return totalNumRead;
+}
+
+
+//---------------------------------------------------------------------------
+//
+// SequentialIterator
+//
+//---------------------------------------------------------------------------
 
 SequentialIterator::SequentialIterator(const Stage& stage)
     : Iterator(stage)
@@ -119,6 +161,11 @@
 }
 
 
+//---------------------------------------------------------------------------
+//
+// RandomIterator
+//
+//---------------------------------------------------------------------------
 
 RandomIterator::RandomIterator(const Stage& stage)
     : Iterator(stage)
diff -r cc6156bfe890 -r b86cf9014e60 src/Writer.cpp
--- a/src/Writer.cpp	Fri Mar 25 10:38:57 2011 -0700
+++ b/src/Writer.cpp	Fri Mar 25 11:10:15 2011 -0700
@@ -75,7 +75,7 @@
 }
 
 
-boost::uint64_t Writer::write(std::size_t targetNumPointsToWrite)
+boost::uint64_t Writer::write(boost::uint64_t targetNumPointsToWrite)
 {
     m_targetNumPointsToWrite = targetNumPointsToWrite;
     m_actualNumPointsWritten = 0;
@@ -84,18 +84,22 @@
 
     writeBegin();
 
+    // in case targetNumPointsToWrite is really big, we will process just one chunk at a time
+
     while (m_actualNumPointsWritten < m_targetNumPointsToWrite)
     {
-        boost::uint64_t numRemainingPointsToRead = m_targetNumPointsToWrite - m_actualNumPointsWritten;
-        boost::uint32_t numPointsToReadThisChunk = static_cast<boost::uint32_t>(std::min<boost::uint64_t>(numRemainingPointsToRead, m_chunkSize));
+        const boost::uint64_t numRemainingPointsToRead = m_targetNumPointsToWrite - m_actualNumPointsWritten;
+        
+        const boost::uint64_t numPointsToReadThisChunk64 = std::min<boost::uint64_t>(numRemainingPointsToRead, m_chunkSize);
+        // this case is safe because m_chunkSize is a uint32
+        const boost::uint32_t numPointsToReadThisChunk = static_cast<boost::uint32_t>(numPointsToReadThisChunk64);
 
         PointBuffer buffer(m_prevStage.getHeader().getSchema(), numPointsToReadThisChunk);
 
-
-        boost::uint32_t numPointsReadThisChunk = iter->read(buffer);
+        const boost::uint32_t numPointsReadThisChunk = iter->read(buffer);
         assert(numPointsReadThisChunk <= numPointsToReadThisChunk);
 
-        boost::uint32_t numPointsWrittenThisChunk = writeBuffer(buffer);
+        const boost::uint32_t numPointsWrittenThisChunk = writeBuffer(buffer);
         assert(numPointsWrittenThisChunk == numPointsReadThisChunk);
 
         m_actualNumPointsWritten += numPointsWrittenThisChunk;
diff -r cc6156bfe890 -r b86cf9014e60 src/drivers/las/Iterator.cpp
--- a/src/drivers/las/Iterator.cpp	Fri Mar 25 10:38:57 2011 -0700
+++ b/src/drivers/las/Iterator.cpp	Fri Mar 25 11:10:15 2011 -0700
@@ -62,17 +62,7 @@
 
 boost::uint64_t SequentialIterator::skipImpl(boost::uint64_t count)
 {
-    const LasReader& reader = m_reader;
-    const Header& header = reader.getHeader();
-
-    boost::uint32_t chunk = (boost::uint32_t)count; // BUG: this needs to be done in blocks if pointNum is large
-
-    // BUG: we can move the stream a constant amount
-    PointBuffer PointBuffer(header.getSchema(), chunk);
-    read(PointBuffer);
-    // just drop the points on the floor and return
-    
-    return count;
+    return naiveSkipImpl(count);
 }
 
 
diff -r cc6156bfe890 -r b86cf9014e60 src/filters/CropFilterIterator.cpp
--- a/src/filters/CropFilterIterator.cpp	Fri Mar 25 10:38:57 2011 -0700
+++ b/src/filters/CropFilterIterator.cpp	Fri Mar 25 11:10:15 2011 -0700
@@ -51,23 +51,7 @@
 
 boost::uint64_t CropFilterSequentialIterator::skipImpl(boost::uint64_t count)
 {
-    boost::uint64_t totalNumRead = 0;
-
-    while (count > 0)
-    {
-        const boost::uint32_t thisCount = (boost::uint32_t)std::min<boost::uint64_t>(getChunkSize(), count);
-
-        PointBuffer junk(getStage().getHeader().getSchema(), thisCount);
-        
-        const boost::uint32_t numRead = read(junk);
-        if (numRead == 0) break; // end of file
-
-        count -= numRead;
-
-        totalNumRead += numRead;
-    }
-
-    return totalNumRead;
+    return naiveSkipImpl(count);
 }
 
 
diff -r cc6156bfe890 -r b86cf9014e60 src/filters/DecimationFilterIterator.cpp
--- a/src/filters/DecimationFilterIterator.cpp	Fri Mar 25 10:38:57 2011 -0700
+++ b/src/filters/DecimationFilterIterator.cpp	Fri Mar 25 11:10:15 2011 -0700
@@ -51,22 +51,7 @@
 
 boost::uint64_t DecimationFilterSequentialIterator::skipImpl(boost::uint64_t count)
 {
-    boost::uint64_t totalNumRead = 0;
-
-    while (count > 0)
-    {
-        const boost::uint32_t thisCount = (boost::uint32_t)std::min<boost::uint64_t>(getChunkSize(), count);
-
-        PointBuffer junk(getStage().getHeader().getSchema(), thisCount);
-        
-        const boost::uint32_t numRead = read(junk);
-        if (numRead == 0) break; // end of file
-
-        count -= numRead;
-        totalNumRead += numRead;
-    }
-
-    return totalNumRead;
+    return naiveSkipImpl(count);
 }
 
 


More information about the Liblas-commits mailing list