summaryrefslogtreecommitdiffstats
path: root/lib/cppparser/cachemanager.cpp
blob: 966f4440438ae7ba4d9ff56d912d0013bfc5b161 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/***************************************************************************
   copyright            : (C) 2006 by David Nolden
   email                : [email protected]
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "cachemanager.h"
#include <kdebug.h>

void CacheNode::access() const {
  m_manager->access( this );
}

void CacheManager::remove( const CacheNode* node ) {
  m_set.erase( node );
}

void CacheManager::add( const CacheNode* node ) {
  m_set.insert( node );
}

CacheNode::CacheNode( Manager* manager ) : m_manager( manager ), m_value(manager->currentMax()) { //initialize m_value with the current maximum, so the new node has a chance even in a cache full of high-rated nodes
  m_manager->add( this  );
}
  
CacheNode::~CacheNode() {
  m_manager->remove( this );
};

void CacheManager::restart( uint normalizeby ) {
  m_currentFrame = 1;
  m_currentMax = 1;
  SetType oldSet = m_set;
  m_set = SetType();
  for( SetType::iterator it = oldSet.begin(); it != oldSet.end(); ++it ) {
    int newValue = (*it)->value() / ( normalizeby / 1000 );
    if( newValue > m_currentMax ) m_currentMax = newValue;
    (*it)->setValue( newValue ); ///This way not all information is discarded
    m_set.insert( *it );
  }
}

void CacheManager::access( const CacheNode* node ) {
  static const int limit = (std::numeric_limits<uint>::max() / 3)*2;
  m_set.erase( node );
  node->setValue( m_currentMax+1 );
  m_set.insert( node );
  if( node->value() > m_currentMax )
    m_currentMax = node->value();
  if( node->value() > limit )
    restart( node->value() );
}

void CacheManager::setMaxNodes ( int maxNodes ) {
  m_maxNodes = maxNodes;
  increaseFrame();
}

void CacheManager::increaseFrame() {
  m_currentFrame ++;
  if( m_set.size() > m_maxNodes ) {
    //kdDebug( 9007 ) << "CacheManager: Have " << m_set.size() << " nodes, maximum is " << m_maxNodes << ", erasing." << endl;
    int mustErase = m_set.size() - m_maxNodes;
    while( !m_set.empty() && mustErase != 0 ) {
      --mustErase;
      SetType::iterator it = m_set.begin();
      erase( *it );
    }
    //kdDebug( 9007 ) << "CacheManager: Have " << m_set.size() << " nodes after erasing." << endl;
  }
}

void CacheManager::removeLowerHalf() {
  int maxNodes = m_maxNodes;
  setMaxNodes( m_set.size() / 2 );
  setMaxNodes( maxNodes );
}

void CacheManager::saveMemory() {
  removeLowerHalf();
}