/*
* VsqBPList.cs
* Copyright (c) 2008-2009 kbinani
*
* This file is part of Boare.Lib.Vsq.
*
* Boare.Lib.Vsq is free software; you can redistribute it and/or
* modify it under the terms of the BSD License.
*
* Boare.Lib.Vsq 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.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace Boare.Lib.Vsq {
///
/// BPListのデータ部分を取り扱うためのクラス。
///
[Serializable]
public class VsqBPList : ICloneable {
private SortedList m_list = new SortedList();
private int m_default = 0;
private int m_maximum = 127;
private int m_minimum = 0;
private class KeyClockIterator : Iterator {
private SortedList m_list;
private int m_pos;
public KeyClockIterator( SortedList list ) {
m_list = list;
m_pos = -1;
}
public bool hasNext() {
if ( m_pos + 1 < m_list.Keys.Count ) {
return true;
} else {
return false;
}
}
public object next() {
m_pos++;
return m_list.Keys[m_pos];
}
public void remove() {
if ( 0 <= m_pos && m_pos < m_list.Keys.Count ) {
int key = m_list.Keys[m_pos];
m_list.Remove( key );
}
}
}
///
/// このVsqBPListの同一コピーを作成します
///
///
public object Clone() {
VsqBPList res = new VsqBPList( m_default, m_minimum, m_maximum );
foreach ( int key in m_list.Keys ) {
res.m_list.Add( key, m_list[key] );
}
return res;
}
///
/// コンストラクタ。デフォルト値はココで指定する。
///
///
public VsqBPList( int default_value, int minimum, int maximum ) {
m_default = default_value;
m_maximum = maximum;
m_minimum = minimum;
}
///
/// このリストに設定された最大値を取得します。
///
public int getMaximum() {
return m_maximum;
}
///
/// このリストに設定された最小値を取得します
///
public int getMinimum() {
return m_minimum;
}
public Iterator keyClockIterator() {
return new KeyClockIterator( m_list );
}
public void remove( int clock ) {
if ( m_list.ContainsKey( clock ) ) {
m_list.Remove( clock );
}
}
public bool isContainsKey( int clock ) {
return m_list.ContainsKey( clock );
}
public int getCount() {
return m_list.Count;
}
public int[] getKeys() {
List t = new List( m_list.Keys );
return t.ToArray();
}
public void clear() {
m_list.Clear();
}
///
/// 新しいデータ点を追加します。
///
///
///
public void add( int clock, int value ) {
lock ( m_list ) {
if ( m_list.ContainsKey( clock ) ) {
m_list[clock] = value;
} else {
m_list.Add( clock, value );
}
}
}
public int getElement( int clock ) {
if ( m_list.Count == 0 ) {
return getDefault();
} else {
if ( m_list.ContainsKey( clock ) ) {
return m_list[clock];
} else {
int index = 0;
int prev = 0;
foreach ( int key in m_list.Keys ) {
if ( clock < key ) {
index = prev;
break;
}
prev = key;
}
if ( m_list.ContainsKey( index ) ) {
return m_list[index];
} else {
return m_default;
}
}
}
}
///
/// このBPListのデフォルト値を取得します
///
public int getDefault() {
return m_default;
}
///
/// このBPListの内容をテキストファイルに書き出します
///
///
public void print( StreamWriter writer ) {
bool first = true;
foreach ( int key in m_list.Keys ) {
int val = m_list[key];
if ( first ) {
writer.WriteLine( key + "=" + val );
first = false;
} else {
writer.WriteLine( key + "=" + val );
}
}
}
///
/// このBPListの内容をテキストファイルに書き出します
///
///
public void print( TextMemoryStream writer, int start, string header ) {
bool first = true;
foreach ( int key in m_list.Keys ) {
if ( start <= key ) {
if ( first ) {
writer.writeLine( header );
first = false;
}
int val = m_list[key];
writer.writeLine( key + "=" + val );
}
}
}
///
/// テキストファイルからデータ点を読込み、現在のリストに追加します
///
///
///
public string appendFromText( TextMemoryStream reader ) {
string last_line = reader.readLine();
while ( !last_line.StartsWith( "[" ) ) {
string[] spl = last_line.Split( new char[] { '=' } );
int i1 = int.Parse( spl[0] );
int i2 = int.Parse( spl[1] );
m_list.Add( i1, i2 );
if ( reader.peek() < 0 ) {
break;
} else {
last_line = reader.readLine();
}
}
return last_line;
}
}
}