Created
November 10, 2010 10:24
-
-
Save anonymous/670654 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* main.cc | |
* | |
* Created on: Nov 9, 2010 | |
* Author: julien | |
*/ | |
#include <iostream> | |
#include "memory/trail_impl.hh" | |
#include "memory/restorable_impl.hh" | |
int main(int argc, char **argv) { | |
trail<int> _trail; | |
restorable<int> a(1,_trail); | |
restorable<int> b(2,_trail); | |
std::cout << a.get() << std::endl; | |
a.set(10); | |
b.set(200); | |
a.set(20); | |
std::cout << a.get() << " " << b.get() << std::endl; | |
_trail.restore(1); | |
std::cout << a.get() << " " << b.get() << std::endl; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* restorable.hh | |
* | |
* Created on: Nov 9, 2010 | |
* Author: julien | |
*/ | |
#ifndef RESTORABLE_HH_ | |
#define RESTORABLE_HH_ | |
#include "trail.hh" | |
template < typename T > | |
class trail; | |
template < typename T > | |
class restorable | |
{ | |
protected: | |
trail<T>& _trail; | |
T _value; | |
public: | |
restorable(T,trail<T>&); | |
virtual ~restorable(); | |
void _set(T); | |
void set(T); | |
inline T get(){ return _value;} | |
}; | |
#endif /* RESTORABLE_HH_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* restorable_impl.hh | |
* | |
* Created on: Nov 9, 2010 | |
* Author: julien | |
*/ | |
#ifndef RESTORABLE_IMPL_HH_ | |
#define RESTORABLE_IMPL_HH_ | |
#include "restorable.hh" | |
#include "trail.hh" | |
template < typename T > | |
restorable<T>::restorable(T init, trail<T>& trail) : _value(init),_trail(trail) {} | |
template < typename T > | |
restorable<T>::~restorable(){} | |
template < typename T > | |
void restorable<T>::_set(T value) | |
{ | |
_value = value; | |
} | |
template < typename T > | |
void restorable<T>::set(T value) | |
{ | |
if (value != _value) | |
{ | |
_trail.push(this,_value); | |
_value = value; | |
} | |
} | |
#endif /* RESTORABLE_IMPL_HH_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* solver.cc | |
* | |
* Created on: Nov 9, 2010 | |
* Author: julien | |
*/ | |
#include "solver.hh" | |
#include <algorithm> | |
solver::solver() { | |
_trails.push_back(new trail<int>()); | |
_trails.push_back(new trail<bool>()); | |
_trails.push_back(new trail<unsigned long>()); | |
} | |
solver::~solver() { | |
std::for_each(_trails.begin(),_trails.end(),my_delete<trail_base*>); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* solver.hh | |
* | |
* Created on: Nov 9, 2010 | |
* Author: julien | |
*/ | |
#ifndef SOLVER_HH_ | |
#define SOLVER_HH_ | |
#include "memory/trail_impl.hh" | |
class trail_base; | |
template < typename T > | |
void my_delete(T& point) | |
{ | |
delete point; | |
} | |
class solver { | |
protected: | |
std::vector<trail_base*> _trails; | |
size_t _world; | |
public: | |
solver(); | |
virtual ~solver(); | |
inline size_t depth() { return _world; } | |
}; | |
#endif /* SOLVER_HH_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* trail.hh | |
* | |
* Created on: Nov 9, 2010 | |
* Author: julien | |
*/ | |
#ifndef TRAIL_HH_ | |
#define TRAIL_HH_ | |
#include <vector> | |
#include "restorable_impl.hh" | |
class solver; | |
class trail_base | |
{ | |
public: | |
virtual void restore(size_t) = 0; | |
}; | |
template < typename T > | |
class trail : public trail_base | |
{ | |
protected: | |
solver* _solver; | |
std::vector<size_t> _stamps; | |
std::vector<T> _values; | |
std::vector< restorable<T>* > _objects; | |
public: | |
trail(); | |
void push(restorable<T>*,T); | |
void restore(size_t); | |
}; | |
#endif /* TRAIL_HH_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* trail_impl.hh | |
* | |
* Created on: Nov 9, 2010 | |
* Author: julien | |
*/ | |
#ifndef TRAIL_IMPL_HH_ | |
#define TRAIL_IMPL_HH_ | |
#include "trail.hh" | |
#include "../solver.hh" | |
template < typename T > | |
trail<T>::trail(){} | |
template < typename T > | |
void trail<T>::push(restorable<T>* obj, T old) | |
{ | |
size_t world = _solver->depth(); | |
_stamps.push_back(world); | |
_values.push_back(old); | |
_objects.push_back(obj); | |
} | |
template < typename T > | |
void trail<T>::restore(size_t world) | |
{ | |
while ( _stamps.size() > 0 && _stamps.back() > world) | |
{ | |
restorable<T>* object = _objects.back(); | |
_objects.pop_back(); | |
T value = _values.back(); | |
_values.pop_back(); | |
object->_set(value); | |
_stamps.pop_back(); | |
} | |
} | |
#endif /* TRAIL_IMPL_HH_ */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment