00001
00002
00021 #include <stdio.h>
00022 #include <cassert>
00023 #include "xfft.h"
00024 #include "uti.h"
00025 #include "xalloc.h"
00026
00027
00029
00061 class buffer2D
00062 {
00063 protected:
00064 DOUBLE** m_buffer;
00065 INT m_Nelem;
00066 INT m_Ndim;
00067 INT m_head;
00068 INT m_tail;
00069
00070
00071
00072 public:
00073
00074 inline
00075 buffer2D ()
00076 : m_buffer(NULL), m_Nelem (0), m_Ndim(0), m_head (0), m_tail (-1)
00077 {}
00078
00079 buffer2D (INT Nelem, INT Nfft);
00080
00081 ~buffer2D();
00082
00084 void
00085 Resize (INT Nelem, INT Nfft);
00086
00088
00094 inline
00095 const DOUBLE*
00096 operator[] (INT idx) const
00097 { return m_buffer[(m_head + idx)%m_Nelem]; }
00098
00100
00106 inline
00107 DOUBLE*
00108 operator[] (INT idx)
00109 { return m_buffer[(m_head + idx)%m_Nelem]; }
00110
00112 inline
00113 void
00114 Push (const DOUBLE* e)
00115 {
00116
00117 Advance();
00118
00119
00120 memcpy (m_buffer[m_tail], e, m_Ndim*sizeof(DOUBLE));
00121 }
00122
00124 inline
00125 INT
00126 Size() const
00127 { return m_Nelem; }
00128
00130 inline
00131 INT
00132 Dim() const
00133 { return m_Ndim; }
00134
00136 inline
00137 bool
00138 Empty() const
00139 { return m_tail == -1; }
00140
00142 inline
00143 const DOUBLE*
00144 Head () const
00145 { return m_buffer[m_head]; }
00146
00148 inline
00149 void
00150 Head (DOUBLE* h) const
00151 { memcpy(h, m_buffer[m_head], m_Ndim*sizeof(DOUBLE)); }
00152
00154 inline
00155 const DOUBLE*
00156 Tail () const
00157 { return m_buffer[m_tail]; }
00158
00160 inline
00161 void
00162 Tail (DOUBLE* t) const
00163 { memcpy(t, m_buffer[m_tail], m_Ndim*sizeof(DOUBLE)); }
00164
00165
00166 protected:
00167
00169 inline
00170 void
00171 Advance ()
00172 {
00173 if (m_tail >= 0 && m_head == (m_tail+1)%m_Nelem)
00174
00175 m_head = (m_head+1)%m_Nelem;
00176
00177 m_tail = (m_tail+1)%m_Nelem;
00178 }
00179
00180 };
00181
00182
00184
00214 class buffer1D
00215 {
00216 protected:
00217 DOUBLE* m_buffer;
00218 INT m_Nelem;
00219 INT m_head;
00220 INT m_tail;
00221
00222
00223
00224 public:
00225
00226 inline
00227 buffer1D ()
00228 : m_buffer(NULL), m_Nelem (0), m_head (0), m_tail (-1)
00229 {}
00230
00231 inline
00232 buffer1D (INT Nelem)
00233 : m_Nelem (Nelem), m_head (0), m_tail (-1)
00234 { m_buffer = (DOUBLE*)xmalloc (Nelem*sizeof(DOUBLE)); }
00235
00236 inline
00237 ~buffer1D()
00238 { if (m_buffer) xfree(m_buffer); }
00239
00240
00242 void
00243 Resize (INT Nelem);
00244
00246
00252 inline
00253 const DOUBLE
00254 operator[] (INT idx) const
00255 { return m_buffer[(m_head + idx)%m_Nelem]; }
00256
00258
00264 inline
00265 DOUBLE
00266 operator[] (INT idx)
00267 { return m_buffer[(m_head + idx)%m_Nelem]; }
00268
00270 inline
00271 void
00272 Push (DOUBLE e)
00273 {
00274
00275 Advance();
00276
00277
00278 m_buffer[m_tail] = e;
00279 }
00280
00282 inline
00283 INT
00284 Size() const
00285 { return m_Nelem; }
00286
00288 inline
00289 bool
00290 Empty() const
00291 { return m_tail == -1; }
00292
00294 inline
00295 DOUBLE
00296 Head () const
00297 { return m_buffer[m_head]; }
00298
00300 inline
00301 DOUBLE
00302 Tail () const
00303 { return m_buffer[m_tail]; }
00304
00305
00306 protected:
00307
00309 inline
00310 void
00311 Advance ()
00312 {
00313 if (m_tail >= 0 && m_head == (m_tail+1)%m_Nelem)
00314
00315 m_head = (m_head+1)%m_Nelem;
00316 m_tail = (m_tail+1)%m_Nelem;
00317 }
00318
00319 };
00320
00321
00322
00323
00324
00325