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

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

Visual Studio 2019 と Bridge.NET でビルドできるよう更新。

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csharp;charset=UTF-8
File size: 4.6 KB
Line 
1/*
2 * TOPPERS/ECNL Prototyping tool
3 *
4 * Copyright (C) 2017 Cores Co., Ltd. Japan
5 *
6 * 上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ
7 * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
8 * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
9 * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
10 * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
11 * スコード中に含まれていること.
12 * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
13 * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
14 * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
15 * の無保証規定を掲載すること.
16 * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
17 * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
18 * と.
19 * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
20 * 作権表示,この利用条件および下記の無保証規定を掲載すること.
21 * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
22 * 報告すること.
23 * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
24 * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
25 * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
26 * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
27 * 免責すること.
28 *
29 * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
30 * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
31 * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
32 * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
33 * の責任を負わない.
34 *
35 * @(#) $Id$
36 */
37using System;
38using System.Linq;
39using System.Collections.Generic;
40using Bridge;
41using System.Collections;
42using Bridge.Html5;
43
44namespace WebMrbc
45{
46 public interface IModel
47 {
48 string Identifier { get; }
49 }
50
51 public class Collection<T> : IEnumerable<T> where T : IModel
52 {
53 T[] List = new T[0];
54
55 public Collection()
56 {
57 }
58
59 public T LastModel { get { int i = List.Length; if (i == 0) return default(T); return List[i - 1]; } }
60 public int Length { get { return List.Length; } }
61
62 public EventHandler OnAdd;
63 public EventHandler OnRemove;
64 public EventHandler OnReset;
65 public EventHandler OnChange;
66
67 internal void Add(T item)
68 {
69 List.Push(item);
70
71 if (OnAdd != null)
72 OnAdd(this, EventArgs.Empty);
73 }
74
75 internal void Remove(T item)
76 {
77 var i = Array.IndexOf(List, item);
78 if (i >= 0)
79 List.Splice(i, 1);
80
81 if (OnRemove != null)
82 OnRemove(this, EventArgs.Empty);
83 }
84
85 internal void Reset()
86 {
87 List.Splice(0, List.Length);
88
89 if (OnReset != null)
90 OnReset(this, EventArgs.Empty);
91 }
92
93 internal void Reset(IEnumerable<T> n)
94 {
95 List.Splice(0, List.Length);
96 List = List.Concat(n).ToArray();
97
98 if (OnReset != null)
99 OnReset(this, EventArgs.Empty);
100 }
101
102 internal T At(int index)
103 {
104 return List[index];
105 }
106
107 internal string UniqueName(string identifier)
108 {
109 var prefix = "";
110 if ((identifier[0] >= 'a') && (identifier[0] <= 'z'))
111 prefix = identifier[0].ToString().ToUpper();
112 else if ((identifier[0] < 'A') || (identifier[0] > 'Z'))
113 prefix = "C";
114
115 var max = 0;
116 var n = new RegExp("^" + identifier + "([0-9]+)$");
117 foreach (var c in List) {
118 var m = c.Identifier.Match(n);
119 if (m != null)
120 max = System.Math.Max(max, Script.ParseInt(m[0]));
121 break;
122 }
123 if (max == 0)
124 return prefix + identifier;
125 else
126 return prefix + identifier + max;
127 }
128
129 internal T FindWhere(object p)
130 {
131 foreach (var c in List) {
132 if (Script.Write<bool>("(function(c) { for (var i in p) if (p[i] !== c[i]) return false; return true; })(c)"))
133 return c;
134 }
135 return default(T);
136 }
137
138 public IEnumerator<T> GetEnumerator()
139 {
140 return (dynamic)List.GetEnumerator();
141 }
142
143 IEnumerator IEnumerable.GetEnumerator()
144 {
145 return List.GetEnumerator();
146 }
147 }
148}
Note: See TracBrowser for help on using the repository browser.