summaryrefslogtreecommitdiffstats
path: root/lib/cppparser/cachemanager.h
blob: 3804356f022ef287f4a01e66bbd4ee7e9d5e117b (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
90
91
92
93
94
95
96
97
98
99
100
101
102

/***************************************************************************
   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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef CACHEMANAGER_H
#define CACHEMANAGER_H
#include <cstdlib>
#include <set>
#include <limits>

class CacheManager;


class CacheNode {
  typedef CacheManager Manager;
  public:
  CacheNode( Manager* manager );
  
  virtual ~CacheNode();

  void access() const;

  inline uint value() const {
    return m_value;
  }

  inline void setValue( const uint v ) const {
    m_value = v;
  }

  inline void addValue( const uint diff ) const {
    m_value += diff;
  }
  
  private:
    Manager* m_manager;
    mutable uint m_value; //This value stands for the priority of the node(higher is better)
};

class CacheNodeCompare {
  public:
  bool operator() ( const CacheNode* lhs, const CacheNode* rhs ) const {
    if( lhs->value() != rhs->value() )
      return lhs->value() < rhs->value();
    else
      return lhs < rhs; //To be able to identify nodes precisely
  }
};


class CacheManager {
  typedef std::set< const CacheNode*, CacheNodeCompare > SetType;
  public:
    CacheManager( int maxNodes = 1000 ) : m_currentFrame(1), m_maxNodes( maxNodes ), m_currentMax(1) {
    };

    inline int currentFrame() const {
      return m_currentFrame;
    }

    void access( const CacheNode* node );

    ///Can be used from outside to set the maximum count of nodes to keep.
    void setMaxNodes ( int maxNodes );

    void increaseFrame();

    ///Can be used on a regular basis(time-triggered) to save memory: Removes half of all triggered 
    void removeLowerHalf();

    virtual void saveMemory();

    int currentMax() const {
      return m_currentMax;
    }

    ///This triggered function should erase the given node.
    virtual void erase( const CacheNode* node ) = 0;
  private:
    void restart( uint normalizeby );
    friend class CacheNode;
    void remove( const CacheNode* node );
    void add( const CacheNode* node );
    int m_currentFrame;
    int m_maxNodes;
    int m_currentMax;
    SetType m_set;
};


#endif