source: EcnlProtoTool/trunk/webapp/webmrbc/Collection.cs@ 270

Last change on this file since 270 was 270, checked in by coas-nagasima, 7 years ago

mruby版ECNLプロトタイピング・ツールを追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csharp
File size: 2.2 KB
Line 
1using System;
2using System.Linq;
3using System.Collections.Generic;
4using Bridge;
5using Bridge.Linq;
6using Bridge.Text.RegularExpressions;
7using System.Collections;
8
9namespace WebMrbc
10{
11 public interface IModel
12 {
13 string Identifier { get; }
14 }
15
16 public class Collection<T> : IEnumerable<T> where T : IModel
17 {
18 T[] List = new T[0];
19
20 public Collection()
21 {
22 }
23
24 public T LastModel { get { int i = List.Length; if (i == 0) return default(T); return List[i - 1]; } }
25 public int Length { get { return List.Length; } }
26
27 public EventHandler OnAdd;
28 public EventHandler OnRemove;
29 public EventHandler OnReset;
30 public EventHandler OnChange;
31
32 internal void Add(T item)
33 {
34 List.Push(item);
35
36 if (OnAdd != null)
37 OnAdd(this, EventArgs.Empty);
38 }
39
40 internal void Remove(T item)
41 {
42 var i = List.IndexOf(item);
43 if (i >= 0)
44 List.Splice(i, 1);
45
46 if (OnRemove != null)
47 OnRemove(this, EventArgs.Empty);
48 }
49
50 internal void Reset()
51 {
52 List.Splice(0, List.Length);
53
54 if (OnReset != null)
55 OnReset(this, EventArgs.Empty);
56 }
57
58 internal void Reset(IEnumerable<T> n)
59 {
60 List.Splice(0, List.Length);
61 List = (T[])List.Concat(n);
62
63 if (OnReset != null)
64 OnReset(this, EventArgs.Empty);
65 }
66
67 internal T At(int index)
68 {
69 return List[index];
70 }
71
72 internal string UniqueName(string identifier)
73 {
74 var prefix = "";
75 if ((identifier[0] >= 'a') && (identifier[0] <= 'z'))
76 prefix = identifier[0].ToString().ToUpper();
77 else if ((identifier[0] < 'A') || (identifier[0] > 'Z'))
78 prefix = "C";
79
80 var max = 0;
81 var n = new Regex("^" + identifier + "([0-9]+)$");
82 foreach (var c in List) {
83 var m = c.Identifier.Match(n);
84 if (m != null)
85 max = System.Math.Max(max, Script.ParseInt(m[0]));
86 break;
87 }
88 if (max == 0)
89 return prefix + identifier;
90 else
91 return prefix + identifier + max;
92 }
93
94 internal T FindWhere(object p)
95 {
96 foreach (var c in List) {
97 if (Script.Write<bool>("(function(c) { for (var i in p) if (p[i] !== c[i]) return false; return true; })(c)"))
98 return c;
99 }
100 return default(T);
101 }
102
103 public IEnumerator<T> GetEnumerator()
104 {
105 return (dynamic)List.GetEnumerator();
106 }
107
108 IEnumerator IEnumerable.GetEnumerator()
109 {
110 return List.GetEnumerator();
111 }
112 }
113}
Note: See TracBrowser for help on using the repository browser.