[Liblas-commits] libpc: added LRU cache

liblas-commits at liblas.org liblas-commits at liblas.org
Thu Mar 10 20:43:05 EST 2011


details:   http://hg.liblas.orglibpc/rev/c58dbf308fe2
changeset: 203:c58dbf308fe2
user:      Michael P. Gerlek <mpg at flaxen.com>
date:      Thu Mar 10 16:09:32 2011 -0800
description:
added LRU cache
Subject: libpc: lru cache testing

details:   http://hg.liblas.orglibpc/rev/5bd8d86f702a
changeset: 204:5bd8d86f702a
user:      Michael P. Gerlek <mpg at flaxen.com>
date:      Thu Mar 10 17:42:39 2011 -0800
description:
lru cache testing
Subject: libpc: merge

details:   http://hg.liblas.orglibpc/rev/862128245ebc
changeset: 205:862128245ebc
user:      Michael P. Gerlek <mpg at flaxen.com>
date:      Thu Mar 10 17:42:55 2011 -0800
description:
merge

diffstat:

 CMakeLists.txt             |    2 +-
 apps/Application.cpp       |   28 +++---
 apps/pc2pc.cpp             |   31 +++++++
 include/libpc/LruCache.hpp |  150 ++++++++++++++++++++++++++++++++++++
 src/CMakeLists.txt         |    1 +
 src/chipper.cpp            |    2 +
 src/drivers/oci/writer.cpp |   97 +++++++++++++++--------
 src/drivers/oci/writer.hpp |    8 +-
 test/unit/CMakeLists.txt   |    1 +
 test/unit/LruCacheTest.cpp |  183 +++++++++++++++++++++++++++++++++++++++++++++
 10 files changed, 449 insertions(+), 54 deletions(-)

diffs (truncated from 684 to 300 lines):

diff -r 25fdd984e99d -r 862128245ebc CMakeLists.txt
--- a/CMakeLists.txt	Thu Mar 10 14:56:29 2011 -0800
+++ b/CMakeLists.txt	Thu Mar 10 17:42:55 2011 -0800
@@ -355,7 +355,7 @@
 # installation commands
 #------------------------------------------------------------------------------
 
-install(FILES AUTHORS COPYING INSTALL LICENSE.txt README.txt
+install(FILES AUTHORS.txt LICENSE.txt
   DESTINATION ${LIBPC_DATA_DIR}/doc)
 
 
diff -r 25fdd984e99d -r 862128245ebc apps/Application.cpp
--- a/apps/Application.cpp	Thu Mar 10 14:56:29 2011 -0800
+++ b/apps/Application.cpp	Thu Mar 10 17:42:55 2011 -0800
@@ -90,21 +90,21 @@
     // call derived function
     int status = 0;
     
-    try
-    {
+    // try
+    // {
         status = execute();
-    }
-    catch (std::exception e)
-    {
-        const std::string s(e.what());
-        runtimeError("Caught exception: " + s);
-        status = 1;
-    }
-    catch (...)
-    {
-        runtimeError("Caught unknown exception");
-        status = 1;
-    }
+    // }
+    // catch (std::exception e)
+    // {
+    //     const std::string s(e.what());
+    //     runtimeError("Caught exception: " + s);
+    //     status = 1;
+    // }
+    // catch (...)
+    // {
+    //     runtimeError("Caught unknown exception");
+    //     status = 1;
+    // }
 
     if (status == 0 && hasOption("timer"))
     {
diff -r 25fdd984e99d -r 862128245ebc apps/pc2pc.cpp
--- a/apps/pc2pc.cpp	Thu Mar 10 14:56:29 2011 -0800
+++ b/apps/pc2pc.cpp	Thu Mar 10 17:42:55 2011 -0800
@@ -26,10 +26,13 @@
 #include "libpc/LasReader.hpp"
 //#include "libpc/LasHeader.hpp"
 #include "libpc/LasWriter.hpp"
+#include "libpc/CacheFilter.hpp"
 
 #include "libpc/../../src/drivers/liblas/writer.hpp"
 #include "libpc/../../src/drivers/liblas/reader.hpp"
 
+#include "libpc/../../src/drivers/oci/writer.hpp"
+
 
 #include "Application.hpp"
 
@@ -84,6 +87,7 @@
         ("input,i", po::value<std::string>(&m_inputFile), "input file name")
         ("output,o", po::value<std::string>(&m_outputFile), "output file name")
         ("native", "use native LAS classes (not liblas)")
+        ("oracle", "oracle test")
         ;
 
     addOptionSet(file_options);
@@ -116,6 +120,33 @@
         assert(numPoints == np); // BUG
         writer.write(np);
     }
+    else if (hasOption("oracle"))
+    {
+        LasReader reader(*ifs);
+    
+        const boost::uint64_t numPoints = reader.getHeader().getNumPoints();
+        
+        libpc::driver::oci::Options options;
+        boost::property_tree::ptree& tree = options.GetPTree();
+        
+        tree.put("capacity", 15);
+        tree.put("connection", "lidar/lidar at oracle.hobu.biz/crrel");
+        tree.put("debug", true);
+        tree.put("verbose", true);
+        
+        CacheFilter cache(reader);
+        
+        libpc::driver::oci::Writer writer(cache, options);
+        
+        
+        //BUG: handle laz writer.setCompressed(false);
+
+        //writer.setPointFormat( reader.getPointFormatNumber() );
+
+        size_t np = (size_t)numPoints;
+        assert(numPoints == np); // BUG
+        writer.write(np);
+    }    
     else
     {
         LiblasReader reader(*ifs);
diff -r 25fdd984e99d -r 862128245ebc include/libpc/LruCache.hpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/libpc/LruCache.hpp	Thu Mar 10 17:42:55 2011 -0800
@@ -0,0 +1,150 @@
+/*
+ * Copyright (c) 2010, Tim Day <timday at timday.com>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+// This code is from http://www.bottlenose.demon.co.uk/article/lru.htm.  It is
+// under a Internet Systems Consortium (ISC) license (an OSI-approved BSD-alike license).
+
+#ifndef INCLUDED_LIBPC_LRUCACHE_HPP
+#define INCLUDED_LIBPC_LRUCACHE_HPP
+
+
+#ifdef _MSC_VER
+#  pragma warning(push)
+#  pragma warning(disable: 4512)  // assignment operator could not be generated
+#endif
+#include <boost/bimap.hpp>
+#include <boost/bimap/list_of.hpp>
+#include <boost/bimap/set_of.hpp>
+#include <boost/function.hpp>
+#ifdef _MSC_VER
+#  pragma warning(pop)
+#endif
+
+namespace libpc
+{
+
+// Class providing fixed-size (by number of records)
+// LRU-replacement cache of a function with signature
+// V f(K)
+
+
+template <typename K,typename V> class LruCache
+{
+public:
+
+    typedef int dummy_type;
+
+    // Bimap with key access on left view, key access
+    // history on right view, and associated value.
+    typedef boost::bimaps::bimap<
+    boost::bimaps::set_of<K>,
+          boost::bimaps::list_of<dummy_type>,
+          boost::bimaps::with_info<V>
+          > cache_type;
+
+    // Constuctor specifies the cached function and
+    // the maximum number of records to be stored.
+    LruCache(
+        const boost::function<V(const K&)>& f,
+        size_t c
+    )
+        :_fn(f)
+        ,_capacity(c)
+    {
+        assert(_capacity!=0);
+    }
+
+    // Obtain value of the cached function for k
+    V operator()(const K& k)
+    {
+
+        // Attempt to find existing record
+        const typename cache_type::left_iterator it
+        =_cache.left.find(k);
+
+        if (it==_cache.left.end())
+        {
+
+            // We don't have it:
+            // Evaluate function and create new record
+            const V v=_fn(k);
+            insert(k,v);
+            return v;
+
+        }
+        else
+        {
+
+            // We do have it:
+            // Update the access record view.
+            _cache.right.relocate(
+                _cache.right.end(),
+                _cache.project_right(it)
+            );
+
+            return it->info;
+        }
+    }
+
+    // Obtain the cached keys, most recently used element
+    // at head, least recently used at tail.
+    // This method is provided purely to support testing.
+    template <typename IT> void get_keys(IT dst) const
+    {
+        typename cache_type::right_const_reverse_iterator src
+        =_cache.right.rbegin();
+        while (src!=_cache.right.rend())
+        {
+            dst=(*src).second;
+            ++src;
+            ++dst;
+        }
+    }
+
+private:
+
+    void insert(const K& k,const V& v)
+    {
+
+        assert(_cache.size()<=_capacity);
+
+        // If necessary, make space
+        if (_cache.size()==_capacity)
+        {
+            // by purging the least-recently-used element
+            _cache.right.erase(_cache.right.begin());
+        }
+
+        // Create a new record from the key, a dummy and the value
+        _cache.insert(
+            typename cache_type::value_type(
+                k,0,v
+            )
+        );
+    }
+
+    const boost::function<V(const K&)> _fn;
+    const size_t _capacity;
+    cache_type _cache;
+
+    LruCache& operator=(const LruCache&); // not implemented
+    LruCache(const LruCache&); // not implemented
+};
+
+
+} // namespace
+
+#endif
diff -r 25fdd984e99d -r 862128245ebc src/CMakeLists.txt
--- a/src/CMakeLists.txt	Thu Mar 10 14:56:29 2011 -0800
+++ b/src/CMakeLists.txt	Thu Mar 10 17:42:55 2011 -0800
@@ -41,6 +41,7 @@
   ${LIBPC_HEADERS_DIR}/LasHeaderWriter.hpp
   ${LIBPC_HEADERS_DIR}/LasReader.hpp
   ${LIBPC_HEADERS_DIR}/LasWriter.hpp
+  ${LIBPC_HEADERS_DIR}/LruCache.hpp
   ${LIBPC_HEADERS_DIR}/Metadata.hpp
   ${LIBPC_HEADERS_DIR}/MosaicFilter.hpp
   ${LIBPC_HEADERS_DIR}/Vector.hpp  
diff -r 25fdd984e99d -r 862128245ebc src/chipper.cpp
--- a/src/chipper.cpp	Thu Mar 10 14:56:29 2011 -0800
+++ b/src/chipper.cpp	Thu Mar 10 17:42:55 2011 -0800
@@ -142,6 +142,7 @@
 
         for (boost::uint32_t j = 0; j < m_threshold; j++)
         {
+            if (j == num_to_read) break; // we're outta here
             // (v * m_header->GetScaleX()) + m_header->GetOffsetX();
             const double x = (buffer.getField<boost::int32_t>(j, indexX) * xscale) + xoffset;
             const double y = (buffer.getField<boost::int32_t>(j, indexY) * yscale) + yoffset;
@@ -153,6 +154,7 @@
             ref.m_pos = y;
             yvec.push_back(ref);
             counter++;
+
         }
 
         num_points_loaded += num_read;
diff -r 25fdd984e99d -r 862128245ebc src/drivers/oci/writer.cpp
--- a/src/drivers/oci/writer.cpp	Thu Mar 10 14:56:29 2011 -0800
+++ b/src/drivers/oci/writer.cpp	Thu Mar 10 17:42:55 2011 -0800
@@ -277,7 +277,6 @@
 
     if (srid == 0) {


More information about the Liblas-commits mailing list