pluskid / pyppm

PPM (Predict by Partial Matching) compression algorithm implementation for Python

pyppm / buffer.h
100644 56 lines (45 sloc) 1.256 kb
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
#ifndef _BUFFER_H_
#define _BUFFER_H_
 
#include <cstring>
 
#include "config.h"
 
class Buffer
{
private:
    symbol_t m_buf[Max_no_contexts+Max_no_contexts];
    symbol_t *m_base;
    int m_offset;
    int m_length;
 
public:
    Buffer()
        :m_base(m_buf), m_offset(0), m_length(0)
        { }
 
    void reset() {
        m_base = m_buf;
        m_offset = 0;
        m_length = 0;
    }
 
    // Append a value to the buffer, the buffer holds
    // at most N values, earlier values will be discarded
    // when necessary.
    void operator << (symbol_t value) {
        m_buf[m_offset++] = value;
        
        if (m_length < Max_no_contexts) {
            m_length++;
        } else {
            m_base++;
            if (m_offset == Max_no_contexts+Max_no_contexts) {
                std::memmove(m_buf, m_buf+Max_no_contexts, Max_no_contexts);
                m_offset = Max_no_contexts;
                m_base = m_buf;
            }
        }
    }
 
    // Get value from the buffer. The index should be
    // within [0, m_length) , and it is not checked.
    symbol_t operator[] (int pos) const {
        return m_base[pos];
    }
 
    // Get the length of the buffer
    int length() const { return m_length; }
};
 
#endif /* _BUFFER_H_ */