OSDN Git Service

不要なクラス変数を自動プロパティにした
[fooeditengine/FooEditEngine.git] / Core / SelectCollection.cs
1 /*
2  * Copyright (C) 2013 FooProject
3  * * 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
4  * the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
5
6  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 
7  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
8
9 You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
10  */
11 using System;
12 using System.Collections.Generic;
13
14 namespace FooEditEngine
15 {
16     /// <summary>
17     /// 選択領域を表すクラス
18     /// </summary>
19     struct Selection : IRange,IEqualityComparer<Selection>
20     {
21         public int start
22         {
23             get;
24             set;
25         }
26
27         public int length
28         {
29             get;
30             set;
31         }
32
33         public static Selection Create(int start, int length)
34         {
35             return new Selection { start = start, length = length};
36         }
37
38         public bool Equals(Selection x, Selection y)
39         {
40             return x.length == y.length && x.start == y.start;
41         }
42
43         public int GetHashCode(Selection obj)
44         {
45             return this.start ^ this.length;
46         }
47     }
48
49     /// <summary>
50     /// 選択範囲が更新されたことを通知するデリゲート
51     /// </summary>
52     /// <param name="sender">送信元クラス</param>
53     /// <param name="e">イベントデータ</param>
54     public delegate void SelectChangeEventHandler(object sender,EventArgs e);
55
56     /// <summary>
57     /// 選択範囲を格納するコレクションを表します
58     /// </summary>
59     sealed class SelectCollection : IEnumerable<Selection>
60     {
61         RangeCollection<Selection> collection;
62
63         /// <summary>
64         /// コンストラクター
65         /// </summary>
66         public SelectCollection()
67             : this(null)
68         {
69         }
70
71         /// <summary>
72         /// コンストラクター
73         /// </summary>
74         /// <param name="selections">コレクションを表します</param>
75         public SelectCollection(IEnumerable<Selection> selections)
76         {
77             if (selections != null)
78                 collection = new RangeCollection<Selection>(selections);
79             else
80                 collection = new RangeCollection<Selection>();
81             this.SelectChange += new SelectChangeEventHandler((s,e)=>{});
82         }
83
84         /// <summary>
85         /// 選択領域が更新されたことを通知します
86         /// </summary>
87         public event SelectChangeEventHandler SelectChange;
88
89         /// <summary>
90         /// インデクサー
91         /// </summary>
92         /// <param name="i">0から始まるインデックス</param>
93         /// <returns>選択領域を返します</returns>
94         public Selection this[int i]
95         {
96             get
97             {
98                 return this.collection[i];
99             }
100             set
101             {
102                 this.collection[i] = value;
103             }
104         }
105
106         /// <summary>
107         /// 追加します
108         /// </summary>
109         /// <param name="sel">選択領域</param>
110         public void Add(Selection sel)
111         {
112             this.collection.Add(sel);
113             this.SelectChange(this, null);
114         }
115
116         /// <summary>
117         /// すべて削除します
118         /// </summary>
119         public void Clear()
120         {
121             this.collection.Clear();
122             this.SelectChange(this, null);
123         }
124
125         /// <summary>
126         /// 要素を取得します
127         /// </summary>
128         /// <param name="index">インデックス</param>
129         /// <param name="length">長さ</param>
130         /// <returns>要素を表すイテレーター</returns>
131         public IEnumerable<Selection> Get(int index, int length)
132         {
133             return this.collection.Get(index, length);
134         }
135
136         /// <summary>
137         /// 格納されている選択領域の数を返します
138         /// </summary>
139         public int Count
140         {
141             get
142             {
143                 return this.collection.Count;
144             }
145         }
146
147         #region IEnumerable<Selection> メンバー
148
149         /// <summary>
150         /// 列挙子を返します
151         /// </summary>
152         /// <returns>IEnumeratorオブジェクトを返す</returns>
153         public IEnumerator<Selection> GetEnumerator()
154         {
155             for (int i = 0; i < this.collection.Count; i++)
156                 yield return this.collection[i];
157         }
158
159         #endregion
160
161         #region IEnumerable メンバー
162
163         System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
164         {
165             throw new NotImplementedException();
166         }
167
168         #endregion
169     }
170 }