| TerraLib 4.1

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files
- Examples

- File List
- File Members

E:/Projetos_Primeiro_Semestre_2012/TerraView/terralib/src/terralib/kernel/TeCoverageDecoderCacheLRU.h
Go to the documentation of this file.

    00001 /************************************************************************************
    00002 TerraLib - a library for developing GIS applications.
    00003 Copyright © 2001-2007 INPE and Tecgraf/PUC-Rio.
    00004
    00005 This code is part of the TerraLib library.
    00006 This library is free software; you can redistribute it and/or
    00007 modify it under the terms of the GNU Lesser General Public
    00008 License as published by the Free Software Foundation; either
    00009 version 2.1 of the License, or (at your option) any later version.
    00010
    00011 You should have received a copy of the GNU Lesser General Public
    00012 License along with this library.
    00013
    00014 The authors reassure the license terms regarding the warranties.
    00015 They specifically disclaim any warranties, including, but not limited to,
    00016 the implied warranties of merchantability and fitness for a particular purpose.
    00017 The library provided hereunder is on an "as is" basis, and the authors have no
    00018 obligation to provide maintenance, support, updates, enhancements, or modifications.
    00019 In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
    00020 indirect, special, incidental, or consequential damages arising out of the use
    00021 of this library and its documentation.
    00022 *************************************************************************************/
    00029 #ifndef  __TERRALIB_INTERNAL_COVERAGEDECODERCACHELRU_H
    00030 #define  __TERRALIB_INTERNAL_COVERAGEDECODERCACHELRU_H
    00031
    00032 #include "TeCoverageDecoder.h"
    00033 #include "TeCoverageDecoderDatabase.h"
    00034
    00039 #define DEFAULT_CACHE_CAPACITY 20
    00040
    00045 template <class T>
    00046 struct TeBoxIntersectionPredicate
    00047 {
    00048     TeBox box;
    00049     std::set<int>& dontSelect; // IDs of blocks that must not be included
    00050
    00052     TeBoxIntersectionPredicate(const TeBox& box, std::set<int>& dontSelect):
    00053             box(box), dontSelect(dontSelect){}
    00054
    00056
    00060     bool operator()(const TeCoverageBlock<T>& block) const {
    00061         if (dontSelect.find(block.id) == dontSelect.end()
    00062                 && TeIntersects(box, block.box))
    00063         {
    00064             dontSelect.insert(block.id);
    00065             return true;
    00066         }
    00067         return false;
    00068     }
    00069 };
    00070
    00084 template <class T>
    00085 class TeCoverageDecoderCacheLRU : public TeCoverageDecoder<T>
    00086 {
    00087 public:
    00088
    00090     TeCoverageDecoderCacheLRU(TeCoverageParams& params, const unsigned int capacity = 0) :
    00091         TeCoverageDecoder<T>(params),
    00092         capacity_(capacity),
    00093         decoder_(NULL)
    00094     {
    00095     }
    00096
    00098     virtual ~TeCoverageDecoderCacheLRU()
    00099     {
    00100         clear();
    00101     }
    00102
    00104
    00108     virtual void init()
    00109     {
    00110         if (capacity_ == 0)
    00111         {
    00112             capacity_ = DEFAULT_CACHE_CAPACITY;
    00113         }
    00114
    00115         if (!decoder_)
    00116         {
    00117             // Initialize the support decoder according to coverage parameters.
    00118             if (params_.getPersistenceType() == TePERSISTENCE_DATABASE_CACHELRU)
    00119             {
    00120                 decoder_ = new TeCoverageDecoderDatabase<T>(params_);
    00121                 decoder_->init();
    00122             }
    00123         }
    00124     }
    00125
    00127
    00131     virtual void clear()
    00132     {
    00133         if (decoder_)
    00134         {
    00135             delete(decoder_); // Destroy the support decoder.
    00136             decoder_ = NULL;
    00137         }
    00138     }
    00139
    00141
    00152     virtual void selectBlocks(const TePolygon& poly, const TeSpatialRelation relation, std::vector<TeCoverageBlock<T> >& selected, std::set<int>& dontSelect)
    00153     {
    00154         selected.clear();
    00155
    00156         // Move to the beginning the blocks on cache intersecting box
    00157         std::list<TeCoverageBlock<T> >::iterator bound;
    00158         bound = stable_partition(cache_.begin(), cache_.end(), TeBoxIntersectionPredicate<T>(poly.box(), dontSelect));
    00159
    00160         // Include blocks on the beginning of the cache in the return
    00161         selected.insert(selected.begin(), cache_.begin(), bound);
    00162
    00163         // Find other blocks on the support decoder
    00164         std::vector<TeCoverageBlock<T> >& blocksFromDecoder = std::vector<TeCoverageBlock<T> >();
    00165         decoder_->selectBlocks(poly, relation, blocksFromDecoder, dontSelect);
    00166
    00167         // For each block selected from the support decoder
    00168         for (std::vector<TeCoverageBlock<T> >::iterator it = blocksFromDecoder.begin(); it != blocksFromDecoder.end(); it++)
    00169         {
    00170             if (cache_.size() >= capacity_) // Check if cache is full
    00171             {
    00172                 cache_.pop_back(); // Remove the least recently used block
    00173             }
    00174             cache_.push_front(*it); // Put new block on the head of the list
    00175
    00176             selected.push_back(*it); // And add block to the result
    00177         }
    00178     }
    00179
    00180 protected:
    00181
    00182     TeCoverageDecoder<T>* decoder_;
    00183
    00184     std::list<TeCoverageBlock<T> > cache_;
    00185
    00186     unsigned int capacity_;
    00187
    00188 };
    00189
    00190 #endif // __TERRALIB_INTERNAL_COVERAGEDECODERCACHELRU_H

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines