summaryrefslogtreecommitdiff
path: root/src/delta.hpp
blob: 1c7d3e2c0c7f17ebf68f575361281e5b92a0cb82 (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
/* $Id: delta.hpp,v 1.5 2006-11-05 04:42:43 ganzhorn Exp $
 * This file is part of lfhex.
 * Copyright (C) 2006 Salem Ganzhorn <eyekode@yahoo.com>
 *
 * 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 version 2.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#ifndef _DELTA_MAP_
#define _DELTA_MAP_

#include <sys/types.h>
#include <list>
#include <map>
#include <vector>
#include <stack>

#include <iostream>

#include "local.h"

class Delta {
public:
  Delta( off_t i, const vector<uchar>& oldData, const vector<uchar>& newData) :
    _offset(i), _old(oldData), _new(newData) {}

  void setNewData( const vector<uchar>& ndata ) { _new = ndata; }

  off_t          offset()  const { return _offset; }
  const vector<uchar>& newData() const { return _new;}
  const vector<uchar>& oldData() const { return _old;}
protected:
  off_t                 _offset;
  vector<uchar>               _old;
  vector<uchar>               _new;
};


// allocation for all delta's is dynamic, but it is all handled 
// within the class.
// the user of the class does not need to worry about memory 
// allocation/deallocation.
class DeltaMap {
public:
  DeltaMap();
  ~DeltaMap();

public:
  // insert a new delta
  size_t insert(off_t offset, 
	     const vector<uchar>& oldData,
	     const vector<uchar>& newData);
  // return the offset of the last inserted Delta.
  off_t lastOffset() const;
  // set the last delta's newData to data
  void setLastData( const vector<uchar>& data);
  int  numEdits() const { return _undo.size(); }
  // find the most recent delta
  const Delta*  search(off_t offset) const;

  // cleans undo/redo stacks and frees memory for all deltas.
  // Makes references to delta's invalid.
  void clear();

  bool undo(); //undo last insert
  bool redo(); //redo last insert

  // method for iterating over all of the modifications
  // finds the first offset that is is not less than lb
  // so lower_bound(0) returns the offset of the first delta
  // (off_t) -1 is returned on failure
  off_t lower_bound(off_t lb) const;

protected:
  typedef map<off_t,list<Delta*> > delta_map_t;
  // notes:
  // the back of the delta list is always the most recent/non undone delta.
  map<off_t,list<Delta*> > _map;
  stack<Delta*>   _undo;
  stack<Delta*>   _redo;
};

#endif