OSDN Git Service

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