~ubuntu-branches/ubuntu/wily/qgis/wily

« back to all changes in this revision

Viewing changes to src/core/spatialindex/storagemanager/RandomEvictionsBuffer.cc

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Spatial Index Library
 
2
//
 
3
// Copyright (C) 2002 Navel Ltd.
 
4
//
 
5
// This library is free software; you can redistribute it and/or
 
6
// modify it under the terms of the GNU Lesser General Public
 
7
// License as published by the Free Software Foundation; either
 
8
// version 2.1 of the License, or (at your option) any later version.
 
9
//
 
10
// This library is distributed in the hope that it will be useful,
 
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
// Lesser General Public License for more details.
 
14
//
 
15
// You should have received a copy of the GNU Lesser General Public
 
16
// License along with this library; if not, write to the Free Software
 
17
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
//
 
19
//  Email:
 
20
//    mhadji@gmail.com
 
21
 
 
22
#include <time.h>
 
23
#include <stdlib.h>
 
24
 
 
25
#include "../spatialindex/SpatialIndexImpl.h"
 
26
 
 
27
#include "RandomEvictionsBuffer.h"
 
28
 
 
29
#ifdef WIN32
 
30
// following two functions don't exist natively on windows so we'll emulate them
 
31
// drand48() returns real number x where 0 <= x < 1
 
32
// srand48() initializes generator
 
33
 
 
34
double drand48( void ) { return ( double ) rand() / ( double )( RAND_MAX + 1 ); }
 
35
void srand48( long seed ) { srand( seed ); }
 
36
#endif
 
37
 
 
38
using namespace SpatialIndex;
 
39
using namespace SpatialIndex::StorageManager;
 
40
using std::map;
 
41
 
 
42
IBuffer* SpatialIndex::StorageManager::returnRandomEvictionsBuffer( IStorageManager& sm, Tools::PropertySet& ps )
 
43
{
 
44
  IBuffer* b = new RandomEvictionsBuffer( sm, ps );
 
45
  return b;
 
46
}
 
47
 
 
48
IBuffer* SpatialIndex::StorageManager::createNewRandomEvictionsBuffer( IStorageManager& sm, unsigned int capacity, bool bWriteThrough )
 
49
{
 
50
  Tools::Variant var;
 
51
  Tools::PropertySet ps;
 
52
 
 
53
  var.m_varType = Tools::VT_ULONG;
 
54
  var.m_val.ulVal = capacity;
 
55
  ps.setProperty( "Capacity", var );
 
56
 
 
57
  var.m_varType = Tools::VT_BOOL;
 
58
  var.m_val.blVal = bWriteThrough;
 
59
  ps.setProperty( "WriteThrough", var );
 
60
 
 
61
  return returnRandomEvictionsBuffer( sm, ps );
 
62
}
 
63
 
 
64
RandomEvictionsBuffer::RandomEvictionsBuffer( IStorageManager& sm, Tools::PropertySet& ps ) : Buffer( sm, ps )
 
65
{
 
66
  srand48(( long )time( NULL ) );
 
67
}
 
68
 
 
69
RandomEvictionsBuffer::~RandomEvictionsBuffer()
 
70
{
 
71
}
 
72
 
 
73
void RandomEvictionsBuffer::addEntry( long id, Entry* e )
 
74
{
 
75
  assert( m_buffer.size() <= m_capacity );
 
76
 
 
77
  if ( m_buffer.size() == m_capacity ) removeEntry();
 
78
  assert( m_buffer.find( id ) == m_buffer.end() );
 
79
  m_buffer.insert( std::pair<long, Entry*>( id, e ) );
 
80
}
 
81
 
 
82
void RandomEvictionsBuffer::removeEntry()
 
83
{
 
84
  if ( m_buffer.size() == 0 ) return;
 
85
 
 
86
  unsigned int entry = ( unsigned int ) floor((( double ) m_buffer.size() ) * drand48() );
 
87
 
 
88
  map<long, Entry*>::iterator it = m_buffer.begin();
 
89
  for ( unsigned int cIndex = 0; cIndex < entry; cIndex++ ) it++;
 
90
 
 
91
  if (( *it ).second->m_bDirty )
 
92
  {
 
93
    long id = ( *it ).first;
 
94
    m_pStorageManager->storeByteArray( id, (( *it ).second )->m_length, ( const byte * )(( *it ).second )->m_pData );
 
95
  }
 
96
 
 
97
  delete( *it ).second;
 
98
  m_buffer.erase( it );
 
99
}