cloudflight
2025-03-05 197329a6ff17789652955e7be314506301332580
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using Newtonsoft.Json;
using System.Diagnostics;
using System.Data;
//using NPOI.SS.Formula.Functions;
//using 文件读写;
 
 
namespace Hydro.CommonBase
{
 
    //public class IndexedDictionary<TKey, TValue> : Dictionary<TKey, TValue>
    //{
    //    private List<TKey> keys = new List<TKey>();
 
    //    public TValue this[int index]
    //    {
    //        get { return this[keys[index]]; }
    //        set { this[keys[index]] = value; }
    //    }
 
    //    public new void Add(TKey key, TValue value)
    //    {
    //        base.Add(key, value);
    //        keys.Add(key);
    //    }
 
    //    public new bool Remove(TKey key)
    //    {
    //        if (base.Remove(key))
    //        {
    //            keys.Remove(key);
    //            return true;
    //        }
    //        return false;
    //    }
    //}
 
 
    
 
    [Serializable]
    public class dict<TKey, TValue> : Dictionary<TKey, TValue>
    {
        private List<TKey> keys 
        {
            get
            {
                return this.Keys.ToList();
            }
        }
 
        public bool emptyCreate = true;
        //List<dict> _list = null;
        public dict() : base()
        {
        }
 
        public dict(int capacity) : base(capacity)
        {
        }
        public dict(IEnumerable<KeyValuePair<TKey, TValue>> collection) : base((IDictionary<TKey, TValue>)collection)
        {
        }
        public bool Value { get; set; }
 
        public bool SetKey(TKey key, dynamic Value)
        {
            if (this.Contains(key))
            {
                this[key] = Value;
                return false;
            }
            else
            {
                this.Add(key, Value);
                return true;
            }
        }
        public TKey[] GetKeys()
        {
            return this.Keys.ToArray();
        }
        public List<TempForEnum<TKey,TValue>> items()
        {
            var temp_list = new List<TempForEnum<TKey, TValue>>();
            foreach (var Pair in this)
            {
                temp_list.Add(new TempForEnum<TKey, TValue>(Pair.Key, Pair.Value));
            }
            return temp_list;
        }
        public bool Contains(TKey[] keys)
        {
            foreach (var key in keys)
                if (!this.Contains(key)) return false;
            return true;
        }
        public bool Contains(TKey key)
        {
            return this.ContainsKey(key);
        }
        public bool Contains(KeyValuePair<TKey, dynamic> key)
        {
            return this.ContainsKey(key.Key);
        }
        public TValue Get(TKey key, TValue @default = default(TValue))
        {
            if (this.Contains(key))
                return this[key];//obj.FirstOrDefault((k) => k.Key == key);
            else
                return @default;
        }
        public TValue Get(KeyValuePair<TKey, TValue> key, TValue @default = default(TValue))
        {
            if (this.Contains(key))
                return this[key.Key];//obj.FirstOrDefault((k) => k.Key == key);
            else
                return @default;
        }
        public TValue pop(TKey key, TValue @default = default(TValue))
        {
            if (this.Contains(key))
            {
                var temp = this[key];
                this.Remove(key);
                return temp;//obj.FirstOrDefault((k) => k.Key == key);
            }
            else
                return @default;
        }
        public TValue pop(KeyValuePair<TKey, TValue> key, TValue @default = default(TValue))
        {
            if (this.Contains(key))
            {
                var temp = this[key.Key];
                this.Remove(key);
                return temp;//obj.FirstOrDefault((k) => k.Key == key);
            }
            else
                return @default;
        }
        public TValue this[KeyValuePair<TKey, TValue> key]
        {
            get { return this[key.Key]; }
            set { this[key.Key] = value; }
        }
        public TValue this[TKey key]
        {
            get
            {
                if (!this.ContainsKey(key))
                {
                    lock (this)
                    {
                        if (!this.ContainsKey(key))
                        {
                            if (emptyCreate)
                                this.Add(key, default(TValue));
                            else
                                return default(TValue);
                        }
                    }
                }                   
                return base[key];
 
            }
            set
            {
                if (!this.ContainsKey(key))
                {
                    lock (this)
                    {
                        if (!this.ContainsKey(key))
                        {
                            if (emptyCreate)
                                this.Add(key, value);
                            else
                                return;
                        }
                    }
                }
                base[key] = value;
            }
 
        }
 
        //public TValue this[int index]
        //{
        //    get 
        //    {
        //        //如果是int类型的key,直接返回base[index],帮我补全以下return后的内容
        //        TKey key = default(TKey);
        //        dynamic k = index;
        //        key = k;
        //        if (typeof(TKey)==typeof(int) )
        //            return base[key];
        //        else
        //            return this[keys[index]]; 
        //    }
        //    set 
        //    { 
        //        this[keys[index]] = value; 
        //    }
        //}
 
        public TValue this[int index]
        {
            get
            {
                //如果是int类型的key,直接返回base[index],帮我补全以下return后的内容
                TKey key = default(TKey);
                if (typeof(TKey) == typeof(int))
                    key = (TKey)(object)index;
                else
                    key = keys[index];
                if (!base.ContainsKey(key)) 
                    base.Add(key, default(TValue));
 
                return base[key];
            }
            set
            {
                TKey key = default(TKey);
                if (typeof(TKey) == typeof(int))
                {
                    key = (TKey)(object)index;
                    base[key] = value;
                }
                    
                else
                {
                    if (index<keys.Count)
                    {
                        key = keys[index];
                        base[key] = value;
                    }
                }
                    
                
            }
        }
 
        public override string ToString()
        {
            return JsonConvert.SerializeObject(this);
        }
        public void LoadFromString(string s)
        {
            dict<TKey,TValue> d= JsonConvert.DeserializeObject<dict<TKey,TValue>>(s);
            this.Clear();
            foreach(var kp in d)
            {
                this.Add(kp.Key,kp.Value);
            }
            
        }
        //public void ExportToExcel()
        //{
        //    DataTable dt = new DataTable();
        //    dt.Columns.Add("Key");
        //    dt.Columns.Add("Value",typeof(double));
        //    foreach (var kp in this)
        //    {
        //        dt.Rows.Add(kp.Key, kp.Value);
        //    }
        //    ExcelExport.DtToExcel(dt);
        //}
        public bool Remove(KeyValuePair<TKey, TValue> key)
        {
            return this.Remove(key.Key);
        }
 
 
        public List<TValue> values()
        {
            return this.Values.ToList();
        }
 
        #region 将Session的元素简单放进dict中,简单处理方便使用
        private DateTime 发送计时;
        private string msgList = "";
        private System.Timers.Timer t = new System.Timers.Timer();
        
       
        public dict<TKey,TValue> ctx
        {
            get { return this; }
        }
 
        #endregion
 
        #region IntentCommand
        public double confidence;
        public string methodName;
        public dict<TKey, TValue> args;
        public string current_arg;
        //current_arg: str = ''
        public dict(double confidence, string methodName, string current_arg = "", dict<TKey, TValue> args = null)
        {
            this.confidence = confidence;
            this.methodName = methodName;
            this.args = args;
            this.current_arg = current_arg;
        }
        #endregion
 
    }
    public class TempForEnum<TKey,TValue>
    {
        public TKey Item1;
        public TValue Item2;
        public TempForEnum(TKey key, TValue value)
        {
            Item1 = key;
            Item2 = value;
        }
    }
    [Serializable]
    public class dict : dict<string, double>
    {
        public dict() : base()
        {
        }
 
        public dict(int capacity) : base(capacity)
        {
        }
        public dict(IEnumerable<KeyValuePair<string, double>> collection) : base((IDictionary<string, double>)collection)
        {
        }
    }
 
 
    public static class DictionaryEx
    {
        public static variable Get(this Dictionary<string,variable> dict,string key)
        {
            if (dict != null && dict.ContainsKey(key))
                return dict[key];
            else
                return null;
        }
        public static void AddKey(this Dictionary<string, variable> dict, string key,variable v)
        {
            if (dict == null) return;
            if ( !dict.ContainsKey(key))
            {
                dict.Add(key, v);
            }
            else
                dict[key] = v;
        }
    }
}