C3204
2026-01-19 0468353b5e2265935846b299afc38bb34ae23e24
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace LB_VisionProcesses
{
    [Serializable]
    public class ProcessCollections<T> : ICollection<ProcessCollectionItem<T>>
    {
        private readonly object _lock = new object(); // 线程安全锁
        private List<ProcessCollectionItem<T>> _items = new List<ProcessCollectionItem<T>>();
 
        // 通过名称查找
        public ProcessCollectionItem<T> Get(string name)
        {
            lock (_lock)
            {
                var item = _items.FirstOrDefault(item => item.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
                if (item == null)
                {
                    //// 如果不存在,创建新项并分配索引
                    //_items.Add(item);
                    return default;
                }
                return item;
            }
        }
 
        // 通过索引查找
        public ProcessCollectionItem<T> Get(int index)
        {
            lock (_lock)
            {
                var item = _items.FirstOrDefault(item => item.Index == index);
                if (item == null)
                {
                    // 如果不存在,创建新项并分配索引
                    _items.Add(item);
                    return default;
                }
                return item;
            }
        }
 
        public Dictionary<string, object> GetAllByName()
        {
            try
            {
                Dictionary<string, object> dic = new Dictionary<string, object>();
 
                for (int i = 0; i <= _items.Count - 1; i++)
                    dic.TryAdd(_items[i].Name, _items[i].Value);
 
                return dic;
            }
            catch { return new Dictionary<string, object>(); }
        }
 
        public List<object> GetAllByIndex()
        {
            try
            {
                List<object> list = new List<object>();
 
                for (int i = 0; i <= _items.Count - 1; i++)
                    list.Add(_items[i].Value);
 
                return list;
            }
            catch { return new List<object>(); }
        }
 
        // ICollection<NamedItem<T>> 实现
        public int Count => _items.Count;
 
        public bool IsReadOnly => false;
 
        void ICollection<ProcessCollectionItem<T>>.Add(ProcessCollectionItem<T> item)
        {
            lock (_lock)
            {
                var existingItem = Get(item.Name);
 
                if (existingItem != null)
                {
                    // 如果已存在,更新现有项的值
                    existingItem = item;
                }
                else
                {
                    // 如果不存在,创建新项并分配索引
                    _items.Add(item);
                }
 
                // 更新后自动排序
                SortItems();
            }
 
        }
        // 添加项时为新项分配一个索引
        public void Add(string name, T value)
        {
            lock (_lock)
            {
                var existingItem = Get(name);
 
                if (existingItem != null)
                {
                    // 如果已存在,更新现有项的值
                    existingItem.Value = value;
                }
                else
                {
                    // 如果不存在,创建新项并分配索引
                    var newItem = new ProcessCollectionItem<T>(name, _items.Count > 0 ? _items.Max(i => i.Index) + 1 : 0, value);
                    _items.Add(newItem);
                }
 
                // 更新后自动排序
                SortItems();
            }
        }
        // 按 Index 排序集合
        private void SortItems()
        {
            lock (_lock)
            {
                _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
            {
                lock (_lock)
                {
                    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
            {
                lock (_lock)
                {
                    var item = _items.FirstOrDefault(i => i.Name == name);
                    if (item != null)
                    {
                        item.Value = value;
                    }
                }
            }
        }
 
        public void Clear()
        {
            lock (_lock)
            {
                _items.Clear();
                _items.TrimExcess(); // 将容量缩减到实际元素数量
            }
        }
 
        public bool Contains(ProcessCollectionItem<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 bool ContainsKey(string name)
        {
            var item = _items.FirstOrDefault(i => i.Name == name);
            if (item != null)
                return true;
 
            return false;
        }
 
        public bool GetKey(int index, out string name)
        {
            if (index < 0 || index >= _items.Count)
            {
                name = null;
                return false;
            }
            name = _items[index].Name;
            return true;
        }
 
        public void CopyTo(ProcessCollectionItem<T>[] array, int arrayIndex)
        {
            lock (_lock)
            {
                _items.CopyTo(array, arrayIndex);
            }
        }
 
        public bool Remove(ProcessCollectionItem<T> item)
        {
            lock (_lock)
            {
                var removed = _items.Remove(item);
                if (removed)
                {
                    // 移除元素后,调整后续元素的索引
                    UpdateIndexes();
                }
                return removed;
            }
        }
 
        public IEnumerator<ProcessCollectionItem<T>> GetEnumerator()
        {
            lock (_lock)
            {
                return _items.GetEnumerator();
            }
        }
 
        IEnumerator IEnumerable.GetEnumerator()
        {
            lock (_lock)
            {
                return _items.GetEnumerator();
            }
        }
 
        // 通过名称删除
        public bool Remove(string name)
        {
            lock (_lock)
            {
                var namedItem = Get(name);
                if (namedItem != null)
                {
                    return Remove(namedItem);
                }
                return false;
            }
        }
 
        // 通过索引删除
        public bool Remove(int index)
        {
            lock (_lock)
            {
                var namedItem = Get(index);
                if (namedItem != null)
                {
                    return Remove(namedItem);
                }
                return false;
            }
        }
 
        // 删除项后,调整其他项的索引
        private void UpdateIndexes()
        {
            lock (_lock)
            {
                for (int i = 0; i < _items.Count; i++)
                {
                    _items[i].Index = i;
                }
            }
        }
    }
 
    public class ProcessCollectionItem<T>
    {
        public string Name { get; set; }
        public int Index { get; set; }
        public T Value { get; set; }       // 存储的对象
 
        public ProcessCollectionItem(string name, int index, T value)
        {
            Name = name;
            Index = index;
            Value = value;
        }
 
        public override string ToString()
        {
            return $"{Name} (Index: {Index}, Value: {Value})";
        }
    }
}