/*
* Copyright (C) 2013 FooProject
* * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
* This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see .
*/
using System;
using System.Collections.Generic;
namespace FooEditEngine
{
///
/// 選択領域を表すクラス
///
struct Selection : IRange,IEqualityComparer
{
public int start
{
get;
set;
}
public int length
{
get;
set;
}
public static Selection Create(int start, int length)
{
return new Selection { start = start, length = length};
}
public bool Equals(Selection x, Selection y)
{
return x.length == y.length && x.start == y.start;
}
public int GetHashCode(Selection obj)
{
return this.start ^ this.length;
}
}
///
/// 選択範囲が更新されたことを通知するデリゲート
///
/// 送信元クラス
/// イベントデータ
public delegate void SelectChangeEventHandler(object sender,EventArgs e);
///
/// 選択範囲を格納するコレクションを表します
///
sealed class SelectCollection : IEnumerable
{
RangeCollection collection;
///
/// コンストラクター
///
public SelectCollection()
: this(null)
{
}
///
/// コンストラクター
///
/// コレクションを表します
public SelectCollection(IEnumerable selections)
{
if (selections != null)
collection = new RangeCollection(selections);
else
collection = new RangeCollection();
this.SelectChange += new SelectChangeEventHandler((s,e)=>{});
}
///
/// 選択領域が更新されたことを通知します
///
public event SelectChangeEventHandler SelectChange;
///
/// インデクサー
///
/// 0から始まるインデックス
/// 選択領域を返します
public Selection this[int i]
{
get
{
return this.collection[i];
}
set
{
this.collection[i] = value;
}
}
///
/// 追加します
///
/// 選択領域
public void Add(Selection sel)
{
this.collection.Add(sel);
this.SelectChange(this, null);
}
///
/// すべて削除します
///
public void Clear()
{
this.collection.Clear();
this.SelectChange(this, null);
}
///
/// 要素を取得します
///
/// インデックス
/// 長さ
/// 要素を表すイテレーター
public IEnumerable Get(int index, int length)
{
return this.collection.Get(index, length);
}
///
/// 格納されている選択領域の数を返します
///
public int Count
{
get
{
return this.collection.Count;
}
}
#region IEnumerable メンバー
///
/// 列挙子を返します
///
/// IEnumeratorオブジェクトを返す
public IEnumerator GetEnumerator()
{
for (int i = 0; i < this.collection.Count; i++)
yield return this.collection[i];
}
#endregion
#region IEnumerable メンバー
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
#endregion
}
}