File: combined_buffer.h

package info (click to toggle)
chromaprint 1.1-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 1,316 kB
  • sloc: cpp: 5,515; ansic: 617; python: 164; sh: 79; makefile: 15
file content (176 lines) | stat: -rw-r--r-- 3,521 bytes parent folder | download | duplicates (3)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * Chromaprint -- Audio fingerprinting toolkit
 * Copyright (C) 2010  Lukas Lalinsky <lalinsky@gmail.com>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
 * USA
 */

#ifndef CHROMAPRINT_COMBINED_BUFFER_H_
#define CHROMAPRINT_COMBINED_BUFFER_H_

#include <math.h>
#include <assert.h>
#include <algorithm>

namespace Chromaprint
{

	template<class T>
	class CombinedBuffer;

	template<class T>
	class _CombinedBufferIterator
	{
	public:
		typedef std::input_iterator_tag iterator_category;
		typedef T value_type;
		typedef int difference_type;
		typedef T* pointer;
		typedef T& reference;

		_CombinedBufferIterator(CombinedBuffer<T> *buffer = 0, int pos = 0)
			: m_buffer(buffer)
		{
			pos += buffer->Offset();
			if (pos < buffer->BufferSize(0)) {
				m_ptr = buffer->Buffer(0) + pos;
				m_ptr_end = buffer->Buffer(0) + buffer->BufferSize(0);
			}
			else {
				pos -= buffer->BufferSize(0);
				m_ptr = buffer->Buffer(1) + pos;
				m_ptr_end = buffer->Buffer(1) + buffer->BufferSize(1);
			}
		}

		_CombinedBufferIterator<T> &operator=(const _CombinedBufferIterator<T> &rhs)
		{
			m_buffer = rhs.m_buffer;
			m_ptr = rhs.m_ptr;
			m_ptr_end = rhs.m_pre_end;
			return *this;
		}

		bool operator==(const _CombinedBufferIterator<T> &rhs) const
		{
			return (m_ptr == rhs.m_ptr) && (m_buffer == rhs.m_buffer);
		}

		bool operator!=(const _CombinedBufferIterator<T> &rhs) const
		{
			return !(operator==(rhs));
		}

		void operator++()
		{
			++m_ptr;
			if (m_ptr >= m_ptr_end) {
				if (m_ptr_end == m_buffer->Buffer(0) + m_buffer->BufferSize(0)) {
					m_ptr = m_buffer->Buffer(1);
					m_ptr_end = m_buffer->Buffer(1) + m_buffer->BufferSize(1);
				}
			}
		}
	
		void operator++(int)
		{
			++(*this);
		}

		short &operator*()
		{
			assert(m_ptr);
			return *m_ptr;
		}

	private:
		CombinedBuffer<T> *m_buffer;
		T *m_ptr_end;
		T *m_ptr;
	};

	template<class T>
	class CombinedBuffer
	{
	public:
		typedef _CombinedBufferIterator<T> Iterator;

		CombinedBuffer(T *buffer1, int size1, T *buffer2, int size2)
			: m_offset(0)
		{
			m_buffer[0] = buffer1;
			m_buffer[1] = buffer2;
			m_buffer[2] = 0;
			m_size[0] = size1;
			m_size[1] = size2;
			m_size[2] = -1;
		}

		int Size()
		{
			return m_size[0] + m_size[1] - m_offset;
		}

		int Shift(int shift)
		{
			m_offset += shift;
			return m_offset;
		}

		int Offset() const
		{
			return m_offset;
		}

		Iterator Begin()
		{
			return Iterator(this, 0);
		}

		Iterator End()
		{
			return Iterator(this, Size());
		}

		T &operator[](int i)
		{
			i += m_offset;
			if (i < m_size[0]) {
				return m_buffer[0][i];
			}
			i -= m_size[0];
			return m_buffer[1][i];
		}

		T *Buffer(int i)
		{
			return m_buffer[i];
		}

		int BufferSize(int i)
		{
			return m_size[i];
		}

	private:
		T *m_buffer[3];
		int m_size[3];
		int m_offset;
	};

};

#endif