C3204
2025-12-18 d85cbf0ccd61d95b96695756e0c90db8b7679545
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace LB_SmartVision
{
    [Serializable]
    public class LB_Collections<T> : ICollection<CollectionItem<T>>
    {
        private List<CollectionItem<T>> _items = new List<CollectionItem<T>>();
 
        // 通过名称查找
        public CollectionItem<T> Get(string name)
        {
            var item = _items.FirstOrDefault(item => item.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
            if (item == null)
                return default;
            return item;
        }
 
        // 通过索引查找
        public CollectionItem<T> Get(int index)
        {
            var item = _items.FirstOrDefault(item => item.Index == index);
            if (item == null)
                return default;
            return item;
        }
 
        // ICollection<NamedItem<T>> 实现
        public int Count => _items.Count;
 
        public bool IsReadOnly => false;
 
        void ICollection<CollectionItem<T>>.Add(CollectionItem<T> item)
        {
            var existingItem = Get(item.Name);
 
            if (existingItem != null)
            {
                // 如果已存在,更新现有项的值
                existingItem = item;
            }
            else
            {
                // 如果不存在,创建新项并分配索引
                _items.Add(item);
            }
 
            // 更新后自动排序
            SortItems();
        }
 
        // 添加项时为新项分配一个索引
        public void Add(string name, T value)
        {
            var existingItem = Get(name);
 
            if (existingItem != null)
            {
                // 如果已存在,更新现有项的值
                existingItem.Value = value;
            }
            else
            {
                // 如果不存在,创建新项并分配索引
                var newItem = new CollectionItem<T>(name, _items.Count > 0 ? _items.Max(i => i.Index) + 1 : 0, value);
                _items.Add(newItem);
            }
 
            // 更新后自动排序
            SortItems();
        }
 
        // 按 Index 排序集合
        private void SortItems()
        {
            _items = _items.OrderBy(item => item.Index).ToList();
        }
 
        // 通过索引器实现基于序号的访问
        public T this[int index]
        {
            get
            {
                if (index < 0 || index >= _items.Count)
                {
                    return default;
                }
                return _items[index].Value;
            }
            set
            {
                if (index < 0 || index >= _items.Count)
                {
                    return;
                }
                _items[index].Value = value;
            }
        }
 
        public T this[string name]
        {
            get
            {
                var existingItem = Get(name);
                if (existingItem != null)
                {
                    // 如果已存在,更新现有项的值
                    return existingItem.Value;
                }
                return default;
            }
            set
            {
                var item = _items.FirstOrDefault(i => i.Name == name);
                if (item != null)
                {
                    item.Value = value;
                }
            }
        }
 
        public void Clear()
        {
            _items.Clear();
            _items.TrimExcess(); // 将容量缩减到实际元素数量
        }
 
        public bool Contains(CollectionItem<T> item)
        {
            return _items.Contains(item);
        }
 
        public bool Contains(string name)
        {
            var item = _items.FirstOrDefault(i => i.Name == name);
            if (item != null)
                return true;
 
            return false;
        }
 
        public void CopyTo(CollectionItem<T>[] array, int arrayIndex)
        {
            _items.CopyTo(array, arrayIndex);
        }
 
        public bool Remove(CollectionItem<T> item)
        {
            var removed = _items.Remove(item);
            if (removed)
            {
                // 移除元素后,调整后续元素的索引
                UpdateIndexes();
            }
            return removed;
        }
 
        public IEnumerator<CollectionItem<T>> GetEnumerator()
        {
            return _items.GetEnumerator();
        }
 
        IEnumerator IEnumerable.GetEnumerator()
        {
            return _items.GetEnumerator();
        }
 
        // 通过名称删除
        public bool Remove(string name)
        {
            var namedItem = Get(name);
            if (namedItem != null)
            {
                return Remove(namedItem);
            }
            return false;
        }
 
        // 通过索引删除
        public bool Remove(int index)
        {
            var namedItem = Get(index);
            if (namedItem != null)
            {
                return Remove(namedItem);
            }
            return false;
        }
 
        // 删除项后,调整其他项的索引
        private void UpdateIndexes()
        {
            for (int i = 0; i < _items.Count; i++)
            {
                _items[i].Index = i;
            }
        }
    }
 
    public class CollectionItem<T>
    {
        public string Name { get; set; }
        public int Index { get; set; }
        public T Value { get; set; }       // 存储的对象
 
        public CollectionItem(string name, int index, T value)
        {
            Name = name;
            Index = index;
            Value = value;
        }
 
        public override string ToString()
        {
            return $"{Name} (Index: {Index}, Value: {Value})";
        }
    }
}