RollingPointPairList.cs
20.9 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
//============================================================================
//ZedGraph Class Library - A Flexible Line Graph/Bar Graph Library in C#
//Copyright © 2006 John Champion
//RollingPointPairList class Copyright © 2006 by Colin Green
//
//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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//=============================================================================
using System;
using System.Text;
using System.Runtime.Serialization;
using System.Security.Permissions;
namespace ZedGraph
{
/// <summary>
/// A class that provides a rolling list of <see cref="PointPair" /> objects.
/// This is essentially a
/// first-in-first-out (FIFO) queue with a fixed capacity which allows 'rolling'
/// (or oscilloscope like) graphs to be be animated without having the overhead of an
/// ever-growing ArrayList.
///
/// The queue is constructed with a fixed capacity and new points can be enqueued. When the
/// capacity is reached the oldest (first in) PointPair is overwritten. However, when
/// accessing via <see cref="IPointList" />, the <see cref="PointPair" /> objects are
/// seen in the order in which they were enqeued.
///
/// RollingPointPairList supports data editing through the <see cref="IPointListEdit" />
/// interface.
///
/// <author>Colin Green with mods by John Champion</author>
/// <version> $Date: 2007/11/05 04:33:26 $ </version>
/// </summary>
[Serializable]
public class RollingPointPairList : IPointList, ISerializable, IPointListEdit
{
#region Fields
/// <summary>
/// An array of PointPair objects that acts as the underlying buffer.
/// </summary>
protected PointPair[] _mBuffer;
/// <summary>
/// The index of the previously enqueued item. -1 if buffer is empty.
/// </summary>
protected int _headIdx;
/// <summary>
/// The index of the next item to be dequeued. -1 if buffer is empty.
/// </summary>
protected int _tailIdx;
#endregion
#region Constructors
/// <summary>
/// Constructs an empty buffer with the specified capacity.
/// </summary>
/// <param name="capacity">Number of elements in the rolling list. This number
/// cannot be changed once the RollingPointPairList is constructed.</param>
public RollingPointPairList( int capacity )
: this( capacity, false )
{
_mBuffer = new PointPair[capacity];
_headIdx = _tailIdx = -1;
}
/// <summary>
/// Constructs an empty buffer with the specified capacity. Pre-allocates space
/// for all PointPair's in the list if <paramref name="preLoad"/> is true.
/// </summary>
/// <param name="capacity">Number of elements in the rolling list. This number
/// cannot be changed once the RollingPointPairList is constructed.</param>
/// <param name="preLoad">true to pre-allocate all PointPair instances in
/// the list, false otherwise. Note that in order to be memory efficient,
/// the <see cref="Add(double,double,double)"/> method should be used to add
/// data. Avoid the <see cref="Add(PointPair)"/> method.
/// </param>
/// <seealso cref="Add(double,double,double)"/>
public RollingPointPairList( int capacity, bool preLoad )
{
_mBuffer = new PointPair[capacity];
_headIdx = _tailIdx = -1;
if ( preLoad )
for ( int i = 0; i < capacity; i++ )
_mBuffer[i] = new PointPair();
}
/// <summary>
/// Constructs a buffer with a copy of the items within the provided
/// <see cref="IPointList" />.
/// The <see cref="Capacity" /> is set to the length of the provided list.
/// </summary>
/// <param name="rhs">The <see cref="IPointList" /> to be copied.</param>
public RollingPointPairList( IPointList rhs )
{
_mBuffer = new PointPair[rhs.Count];
for ( int i = 0; i < rhs.Count; i++ )
{
_mBuffer[i] = new PointPair( rhs[i] );
}
_headIdx = rhs.Count - 1;
_tailIdx = 0;
}
#endregion
#region Properties
/// <summary>
/// Gets the capacity of the rolling buffer.
/// </summary>
public int Capacity
{
get { return _mBuffer.Length; }
}
/// <summary>
/// Gets the count of items within the rolling buffer. Note that this may be less than
/// the capacity.
/// </summary>
public int Count
{
get
{
if ( _headIdx == -1 )
return 0;
if ( _headIdx > _tailIdx )
return ( _headIdx - _tailIdx ) + 1;
if ( _tailIdx > _headIdx )
return ( _mBuffer.Length - _tailIdx ) + _headIdx + 1;
return 1;
}
}
/// <summary>
/// Gets a bolean that indicates if the buffer is empty.
/// Alternatively you can test Count==0.
/// </summary>
public bool IsEmpty
{
get { return _headIdx == -1; }
}
/// <summary>
/// Gets or sets the <see cref="PointPair" /> at the specified index in the buffer.
/// </summary>
/// <remarks>
/// Index must be within the current size of the buffer, e.g., the set
/// method will not expand the buffer even if <see cref="Capacity" /> is available
/// </remarks>
public PointPair this[int index]
{
get
{
if ( index >= Count || index < 0 )
throw new ArgumentOutOfRangeException();
index += _tailIdx;
if ( index >= _mBuffer.Length )
index -= _mBuffer.Length;
return _mBuffer[index];
}
set
{
if ( index >= Count || index < 0 )
throw new ArgumentOutOfRangeException();
index += _tailIdx;
if ( index >= _mBuffer.Length )
index -= _mBuffer.Length;
_mBuffer[index] = value;
}
}
#endregion
#region Public Methods
/// <summary>
/// Implement the <see cref="ICloneable" /> interface in a typesafe manner by just
/// calling the typed version of <see cref="Clone" />
/// </summary>
/// <returns>A deep copy of this object</returns>
object ICloneable.Clone()
{
return this.Clone();
}
/// <summary>
/// Typesafe, deep-copy clone method.
/// </summary>
/// <returns>A new, independent copy of this class</returns>
public RollingPointPairList Clone()
{
return new RollingPointPairList( this );
}
/// <summary>
/// Clear the buffer of all <see cref="PointPair"/> objects.
/// Note that the <see cref="Capacity" /> remains unchanged.
/// </summary>
public void Clear()
{
_headIdx = _tailIdx = -1;
}
/// <summary>
/// Calculate that the next index in the buffer that should receive a new data point.
/// Note that this method actually advances the buffer, so a datapoint should be
/// added at _mBuffer[_headIdx].
/// </summary>
/// <returns>The index position of the new head element</returns>
private int GetNextIndex()
{
if ( _headIdx == -1 )
{ // buffer is currently empty.
_headIdx = _tailIdx = 0;
}
else
{
// Determine the index to write to.
if ( ++_headIdx == _mBuffer.Length )
{ // Wrap around.
_headIdx = 0;
}
if ( _headIdx == _tailIdx )
{ // Buffer overflow. Increment tailIdx.
if ( ++_tailIdx == _mBuffer.Length )
{ // Wrap around.
_tailIdx = 0;
}
}
}
return _headIdx;
}
/// <summary>
/// Add a <see cref="PointPair"/> onto the head of the queue,
/// overwriting old values if the buffer is full.
/// </summary>
/// <param name="item">The <see cref="PointPair" /> to be added.</param>
public void Add( PointPair item )
{
_mBuffer[ GetNextIndex() ] = item;
}
/// <summary>
/// Add an <see cref="IPointList"/> object to the head of the queue.
/// </summary>
/// <param name="pointList">A reference to the <see cref="IPointList"/> object to
/// be added</param>
public void Add( IPointList pointList )
{ // A slightly more efficient approach would be to determine where the new points should placed within
// the buffer and to then copy them in directly - updating the head and tail indexes appropriately.
for ( int i = 0; i < pointList.Count; i++ )
Add( pointList[i] );
}
/// <summary>
/// Remove an old item from the tail of the queue.
/// </summary>
/// <returns>The removed item. Throws an <see cref="InvalidOperationException" />
/// if the buffer was empty.
/// Check the buffer's length (<see cref="Count" />) or the <see cref="IsEmpty" />
/// property to avoid exceptions.</returns>
public PointPair Remove()
{
if ( _tailIdx == -1 )
{ // buffer is currently empty.
throw new InvalidOperationException( "buffer is empty." );
}
PointPair o = _mBuffer[_tailIdx];
if ( _tailIdx == _headIdx )
{ // The buffer is now empty.
_headIdx = _tailIdx = -1;
return o;
}
if ( ++_tailIdx == _mBuffer.Length )
{ // Wrap around.
_tailIdx = 0;
}
return o;
}
/// <summary>
/// Remove the <see cref="PointPair" /> at the specified index
/// </summary>
/// <remarks>
/// All items in the queue that lie after <paramref name="index"/> will
/// be shifted back by one, and the queue will be one item shorter.
/// </remarks>
/// <param name="index">The ordinal position of the item to be removed.
/// Throws an <see cref="ArgumentOutOfRangeException" /> if index is less than
/// zero or greater than or equal to <see cref="Count" />
/// </param>
public void RemoveAt( int index )
{
int count = this.Count;
if ( index >= count || index < 0 )
throw new ArgumentOutOfRangeException();
// shift all the items that lie after index back by 1
for ( int i = index + _tailIdx; i < _tailIdx + count - 1; i++ )
{
i = ( i >= _mBuffer.Length ) ? 0 : i;
int j = i + 1;
j = ( j >= _mBuffer.Length ) ? 0 : j;
_mBuffer[i] = _mBuffer[j];
}
// Remove the item from the head (it's been duplicated already)
Pop();
}
/// <summary>
/// Remove a range of <see cref="PointPair" /> objects starting at the specified index
/// </summary>
/// <remarks>
/// All items in the queue that lie after <paramref name="index"/> will
/// be shifted back, and the queue will be <paramref name="count" /> items shorter.
/// </remarks>
/// <param name="index">The ordinal position of the item to be removed.
/// Throws an <see cref="ArgumentOutOfRangeException" /> if index is less than
/// zero or greater than or equal to <see cref="Count" />
/// </param>
/// <param name="count">The number of items to be removed. Throws an
/// <see cref="ArgumentOutOfRangeException" /> if <paramref name="count" /> is less than zero
/// or greater than the total available items in the queue</param>
public void RemoveRange( int index, int count )
{
int totalCount = this.Count;
if ( index >= totalCount || index < 0 || count < 0 || count > totalCount )
throw new ArgumentOutOfRangeException();
for ( int i = 0; i < count; i++ )
this.RemoveAt( index );
}
/// <summary>
/// Pop an item off the head of the queue.
/// </summary>
/// <returns>The popped item. Throws an exception if the buffer was empty.</returns>
public PointPair Pop()
{
if ( _tailIdx == -1 )
{ // buffer is currently empty.
throw new InvalidOperationException( "buffer is empty." );
}
PointPair o = _mBuffer[_headIdx];
if ( _tailIdx == _headIdx )
{ // The buffer is now empty.
_headIdx = _tailIdx = -1;
return o;
}
if ( --_headIdx == -1 )
{ // Wrap around.
_headIdx = _mBuffer.Length - 1;
}
return o;
}
/// <summary>
/// Peek at the <see cref="PointPair" /> item at the head of the queue.
/// </summary>
/// <returns>The <see cref="PointPair" /> item at the head of the queue.
/// Throws an <see cref="InvalidOperationException" /> if the buffer was empty.
/// </returns>
public PointPair Peek()
{
if ( _headIdx == -1 )
{ // buffer is currently empty.
throw new InvalidOperationException( "buffer is empty." );
}
return _mBuffer[_headIdx];
}
#endregion
#region Auxilliary Methods
/// <summary>
/// Add a set of values onto the head of the queue,
/// overwriting old values if the buffer is full.
/// </summary>
/// <remarks>
/// This method is much more efficient that the <see cref="Add(PointPair)">Add(PointPair)</see>
/// method, since it does not require that a new PointPair instance be provided.
/// If the buffer already contains a <see cref="PointPair"/> at the head position,
/// then the x, y, z, and tag values will be copied into the existing PointPair.
/// Otherwise, a new PointPair instance must be created.
/// In this way, each PointPair position in the rolling list will only be allocated one time.
/// To truly be memory efficient, the <see cref="Remove" />, <see cref="RemoveAt" />,
/// and <see cref="Pop" /> methods should be avoided. Also, the <paramref name="tag"/> property
/// for this method should be null, since it is a reference type.
/// </remarks>
/// <param name="x">The X value</param>
/// <param name="y">The Y value</param>
/// <param name="z">The Z value</param>
/// <param name="tag">The Tag value for the PointPair</param>
public void Add( double x, double y, double z, object tag )
{
// advance the rolling list
GetNextIndex();
if ( _mBuffer[_headIdx] == null )
_mBuffer[_headIdx] = new PointPair( x, y, z, tag );
else
{
_mBuffer[_headIdx].X = x;
_mBuffer[_headIdx].Y = y;
_mBuffer[_headIdx].Z = z;
_mBuffer[_headIdx].Tag = tag;
}
}
/// <summary>
/// Add a set of values onto the head of the queue,
/// overwriting old values if the buffer is full.
/// </summary>
/// <remarks>
/// This method is much more efficient that the <see cref="Add(PointPair)">Add(PointPair)</see>
/// method, since it does not require that a new PointPair instance be provided.
/// If the buffer already contains a <see cref="PointPair"/> at the head position,
/// then the x, y, z, and tag values will be copied into the existing PointPair.
/// Otherwise, a new PointPair instance must be created.
/// In this way, each PointPair position in the rolling list will only be allocated one time.
/// To truly be memory efficient, the <see cref="Remove" />, <see cref="RemoveAt" />,
/// and <see cref="Pop" /> methods should be avoided.
/// </remarks>
/// <param name="x">The X value</param>
/// <param name="y">The Y value</param>
public void Add( double x, double y )
{
Add( x, y, PointPair.Missing, null );
}
/// <summary>
/// Add a set of values onto the head of the queue,
/// overwriting old values if the buffer is full.
/// </summary>
/// <remarks>
/// This method is much more efficient that the <see cref="Add(PointPair)">Add(PointPair)</see>
/// method, since it does not require that a new PointPair instance be provided.
/// If the buffer already contains a <see cref="PointPair"/> at the head position,
/// then the x, y, z, and tag values will be copied into the existing PointPair.
/// Otherwise, a new PointPair instance must be created.
/// In this way, each PointPair position in the rolling list will only be allocated one time.
/// To truly be memory efficient, the <see cref="Remove" />, <see cref="RemoveAt" />,
/// and <see cref="Pop" /> methods should be avoided. Also, the <paramref name="tag"/> property
/// for this method should be null, since it is a reference type.
/// </remarks>
/// <param name="x">The X value</param>
/// <param name="y">The Y value</param>
/// <param name="tag">The Tag value for the PointPair</param>
public void Add( double x, double y, object tag )
{
Add( x, y, PointPair.Missing, tag );
}
/// <summary>
/// Add a set of values onto the head of the queue,
/// overwriting old values if the buffer is full.
/// </summary>
/// <remarks>
/// This method is much more efficient that the <see cref="Add(PointPair)">Add(PointPair)</see>
/// method, since it does not require that a new PointPair instance be provided.
/// If the buffer already contains a <see cref="PointPair"/> at the head position,
/// then the x, y, z, and tag values will be copied into the existing PointPair.
/// Otherwise, a new PointPair instance must be created.
/// In this way, each PointPair position in the rolling list will only be allocated one time.
/// To truly be memory efficient, the <see cref="Remove" />, <see cref="RemoveAt" />,
/// and <see cref="Pop" /> methods should be avoided.
/// </remarks>
/// <param name="x">The X value</param>
/// <param name="y">The Y value</param>
/// <param name="z">The Z value</param>
public void Add( double x, double y, double z )
{
Add( x, y, z, null );
}
/// <summary>
/// Add a set of points to the <see cref="RollingPointPairList"/>
/// from two arrays of type double.
/// If either array is null, then a set of ordinal values is automatically
/// generated in its place (see <see cref="AxisType.Ordinal"/>).
/// If the arrays are of different size, then the larger array prevails and the
/// smaller array is padded with <see cref="PointPairBase.Missing"/> values.
/// </summary>
/// <param name="x">A double[] array of X values</param>
/// <param name="y">A double[] array of Y values</param>
public void Add( double[] x, double[] y )
{
int len = 0;
if ( x != null )
len = x.Length;
if ( y != null && y.Length > len )
len = y.Length;
for ( int i = 0; i < len; i++ )
{
PointPair point = new PointPair( 0, 0, 0 );
if ( x == null )
point.X = (double)i + 1.0;
else if ( i < x.Length )
point.X = x[i];
else
point.X = PointPair.Missing;
if ( y == null )
point.Y = (double)i + 1.0;
else if ( i < y.Length )
point.Y = y[i];
else
point.Y = PointPair.Missing;
Add( point );
}
}
/// <summary>
/// Add a set of points to the <see cref="RollingPointPairList"/> from
/// three arrays of type double.
/// If the X or Y array is null, then a set of ordinal values is automatically
/// generated in its place (see <see cref="AxisType.Ordinal"/>.
/// If the <see paramref="z"/> value
/// is null, then it is set to zero.
/// If the arrays are of different size, then the larger array prevails and the
/// smaller array is padded with <see cref="PointPairBase.Missing"/> values.
/// </summary>
/// <param name="x">A double[] array of X values</param>
/// <param name="y">A double[] array of Y values</param>
/// <param name="z">A double[] array of Z values</param>
public void Add( double[] x, double[] y, double[] z )
{
int len = 0;
if ( x != null )
len = x.Length;
if ( y != null && y.Length > len )
len = y.Length;
if ( z != null && z.Length > len )
len = z.Length;
for ( int i = 0; i < len; i++ )
{
PointPair point = new PointPair();
if ( x == null )
point.X = (double)i + 1.0;
else if ( i < x.Length )
point.X = x[i];
else
point.X = PointPair.Missing;
if ( y == null )
point.Y = (double)i + 1.0;
else if ( i < y.Length )
point.Y = y[i];
else
point.Y = PointPair.Missing;
if ( z == null )
point.Z = (double)i + 1.0;
else if ( i < z.Length )
point.Z = z[i];
else
point.Z = PointPair.Missing;
Add( point );
}
}
#endregion
#region Serialization
/// <summary>
/// Current schema value that defines the version of the serialized file
/// </summary>
public const int schema = 10;
/// <summary>
/// Constructor for deserializing objects
/// </summary>
/// <param name="info">A <see cref="SerializationInfo"/> instance that defines the serialized data
/// </param>
/// <param name="context">A <see cref="StreamingContext"/> instance that contains the serialized data
/// </param>
protected RollingPointPairList( SerializationInfo info, StreamingContext context )
{
// The schema value is just a file version parameter. You can use it to make future versions
// backwards compatible as new member variables are added to classes
int sch = info.GetInt32( "schema" );
_headIdx = info.GetInt32( "headIdx" );
_tailIdx = info.GetInt32( "tailIdx" );
_mBuffer = (PointPair[])info.GetValue( "mBuffer", typeof( PointPair[] ) );
}
/// <summary>
/// Populates a <see cref="SerializationInfo"/> instance with the data needed to serialize the target object
/// </summary>
/// <param name="info">A <see cref="SerializationInfo"/> instance that defines the serialized data</param>
/// <param name="context">A <see cref="StreamingContext"/> instance that contains the serialized data</param>
[SecurityPermissionAttribute( SecurityAction.Demand, SerializationFormatter = true )]
public virtual void GetObjectData( SerializationInfo info, StreamingContext context )
{
info.AddValue( "schema", schema );
info.AddValue( "headIdx", _headIdx );
info.AddValue( "tailIdx", _tailIdx );
info.AddValue( "mBuffer", _mBuffer );
}
#endregion
}
}