OSDN Git Service

0b455859225a7024872fee9e592f2a3545f0798e
[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 static Selection Create(int start, int length)\r
34         {\r
35             return new Selection { start = start, length = length};\r
36         }\r
37 \r
38         public bool Equals(Selection x, Selection y)\r
39         {\r
40             return x.length == y.length && x.start == y.start;\r
41         }\r
42 \r
43         public int GetHashCode(Selection obj)\r
44         {\r
45             return this.start ^ this.length;\r
46         }\r
47     }\r
48 \r
49     /// <summary>\r
50     /// 選択範囲が更新されたことを通知するデリゲート\r
51     /// </summary>\r
52     /// <param name="sender">送信元クラス</param>\r
53     /// <param name="e">イベントデータ</param>\r
54     public delegate void SelectChangeEventHandler(object sender,EventArgs e);\r
55 \r
56     /// <summary>\r
57     /// 選択範囲を格納するコレクションを表します\r
58     /// </summary>\r
59     sealed class SelectCollection : IEnumerable<Selection>\r
60     {\r
61         RangeCollection<Selection> collection;\r
62 \r
63         /// <summary>\r
64         /// コンストラクター\r
65         /// </summary>\r
66         public SelectCollection()\r
67             : this(null)\r
68         {\r
69         }\r
70 \r
71         /// <summary>\r
72         /// コンストラクター\r
73         /// </summary>\r
74         /// <param name="selections">コレクションを表します</param>\r
75         public SelectCollection(IEnumerable<Selection> selections)\r
76         {\r
77             if (selections != null)\r
78                 collection = new RangeCollection<Selection>(selections);\r
79             else\r
80                 collection = new RangeCollection<Selection>();\r
81             this.SelectChange += new SelectChangeEventHandler((s,e)=>{});\r
82         }\r
83 \r
84         /// <summary>\r
85         /// 選択領域が更新されたことを通知します\r
86         /// </summary>\r
87         public event SelectChangeEventHandler SelectChange;\r
88 \r
89         /// <summary>\r
90         /// インデクサー\r
91         /// </summary>\r
92         /// <param name="i">0から始まるインデックス</param>\r
93         /// <returns>選択領域を返します</returns>\r
94         public Selection this[int i]\r
95         {\r
96             get\r
97             {\r
98                 return this.collection[i];\r
99             }\r
100             set\r
101             {\r
102                 this.collection[i] = value;\r
103             }\r
104         }\r
105 \r
106         /// <summary>\r
107         /// 追加します\r
108         /// </summary>\r
109         /// <param name="sel">選択領域</param>\r
110         public void Add(Selection sel)\r
111         {\r
112             this.collection.Add(sel);\r
113             this.SelectChange(this, null);\r
114         }\r
115 \r
116         /// <summary>\r
117         /// すべて削除します\r
118         /// </summary>\r
119         public void Clear()\r
120         {\r
121             this.collection.Clear();\r
122             this.SelectChange(this, null);\r
123         }\r
124 \r
125         /// <summary>\r
126         /// 要素を取得します\r
127         /// </summary>\r
128         /// <param name="index">インデックス</param>\r
129         /// <param name="length">長さ</param>\r
130         /// <returns>要素を表すイテレーター</returns>\r
131         public IEnumerable<Selection> Get(int index, int length)\r
132         {\r
133             return this.collection.Get(index, length);\r
134         }\r
135 \r
136         /// <summary>\r
137         /// 格納されている選択領域の数を返します\r
138         /// </summary>\r
139         public int Count\r
140         {\r
141             get\r
142             {\r
143                 return this.collection.Count;\r
144             }\r
145         }\r
146 \r
147         #region IEnumerable<Selection> メンバー\r
148 \r
149         /// <summary>\r
150         /// 列挙子を返します\r
151         /// </summary>\r
152         /// <returns>IEnumeratorオブジェクトを返す</returns>\r
153         public IEnumerator<Selection> GetEnumerator()\r
154         {\r
155             for (int i = 0; i < this.collection.Count; i++)\r
156                 yield return this.collection[i];\r
157         }\r
158 \r
159         #endregion\r
160 \r
161         #region IEnumerable メンバー\r
162 \r
163         System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()\r
164         {\r
165             throw new NotImplementedException();\r
166         }\r
167 \r
168         #endregion\r
169     }\r
170 }\r