MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
SimpleDictionary.cs
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5using System.Text;
6using System.Threading.Tasks;
7
8namespace MyCaffe.basecode
9{
13 public class SimpleDictionary
14 {
15 Dictionary<string, object> m_rgValues = new Dictionary<string, object>();
16 Dictionary<string, TYPE> m_rgTypes = new Dictionary<string, TYPE>();
17
21 public enum TYPE
22 {
26 NONE = -1,
30 STRING = 0,
34 NUMERIC = 1,
38 INTEGER = 2
39 }
40
45 {
46 }
47
54 public bool GetType(string strName, out TYPE type)
55 {
56 type = TYPE.NONE;
57
58 if (!m_rgTypes.ContainsKey(strName))
59 return false;
60
61 type = m_rgTypes[strName];
62
63 return true;
64 }
65
71 public double GetNumeric(string strName)
72 {
73 if (m_rgTypes[strName] != TYPE.NUMERIC)
74 throw new Exception("Invalid type, expected '" + m_rgTypes[strName].ToString() + "'");
75
76 return (double)m_rgValues[strName];
77 }
78
85 public int GetInteger(string strName, int? nDefault = null)
86 {
87 if (!m_rgTypes.ContainsKey(strName))
88 {
89 if (nDefault.HasValue)
90 return nDefault.Value;
91
92 throw new Exception("The variable '" + strName + "' is not in the dictionary.");
93 }
94
95 if (m_rgTypes[strName] != TYPE.INTEGER)
96 throw new Exception("Invalid type, expected '" + m_rgTypes[strName].ToString() + "'");
97
98 return (int)m_rgValues[strName];
99 }
100
106 public string GetString(string strName)
107 {
108 if (m_rgTypes[strName] != TYPE.STRING)
109 throw new Exception("Invalid type, expected '" + m_rgTypes[strName].ToString() + "'");
110
111 return (string)m_rgValues[strName];
112 }
113
119 public void Add(string strName, string strVal)
120 {
121 m_rgValues.Add(strName, strVal);
122 m_rgTypes.Add(strName, TYPE.STRING);
123 }
124
130 public void Add(string strName, int nVal)
131 {
132 m_rgValues.Add(strName, nVal);
133 m_rgTypes.Add(strName, TYPE.INTEGER);
134 }
135
141 public void Add(string strName, double dfVal)
142 {
143 m_rgValues.Add(strName, dfVal);
144 m_rgTypes.Add(strName, TYPE.NUMERIC);
145 }
146
151 public List<KeyValuePair<string, object>> ToList()
152 {
153 return m_rgValues.ToList();
154 }
155
160 public byte[] ToByteArray()
161 {
162 using (MemoryStream ms = new MemoryStream())
163 {
164 BinaryWriter bw = new BinaryWriter(ms);
165
166 bw.Write(m_rgValues.Count);
167
168 List<KeyValuePair<string, object>> rgValues = m_rgValues.ToList();
169 List<KeyValuePair<string, TYPE>> rgTypes = m_rgTypes.ToList();
170
171 for (int i = 0; i < rgValues.Count; i++)
172 {
173 bw.Write(rgTypes[i].Key);
174 bw.Write((byte)rgTypes[i].Value);
175
176 switch (rgTypes[i].Value)
177 {
178 case TYPE.STRING:
179 bw.Write(rgValues[i].Value.ToString());
180 break;
181
182 case TYPE.INTEGER:
183 bw.Write((int)rgValues[i].Value);
184 break;
185
186 case TYPE.NUMERIC:
187 bw.Write((double)rgValues[i].Value);
188 break;
189
190 default:
191 break;
192 }
193 }
194
195 return ms.ToArray();
196 }
197 }
198
204 public static SimpleDictionary FromByteArray(byte[] rg)
205 {
206 SimpleDictionary dictionary = new basecode.SimpleDictionary();
207
208 using (MemoryStream ms = new MemoryStream(rg))
209 {
210 BinaryReader br = new BinaryReader(ms);
211 int nCount = br.ReadInt32();
212
213 for (int i = 0; i < nCount; i++)
214 {
215 string strName = br.ReadString();
216 TYPE type = (TYPE)br.ReadByte();
217
218 switch (type)
219 {
220 case TYPE.STRING:
221 string strVal = br.ReadString();
222 dictionary.Add(strName, strVal);
223 break;
224
225 case TYPE.INTEGER:
226 int nVal = br.ReadInt32();
227 dictionary.Add(strName, nVal);
228 break;
229
230 case TYPE.NUMERIC:
231 double dfVal = br.ReadDouble();
232 dictionary.Add(strName, dfVal);
233 break;
234
235 default:
236 break;
237 }
238 }
239 }
240
241 return dictionary;
242 }
243
250 public Dictionary<string, double> ToNumericValues(int nCount, string strKey)
251 {
252 Dictionary<string, double> rg = new Dictionary<string, double>();
253
254 for (int i = 0; i < nCount; i++)
255 {
256 string strKeyName = strKey + i.ToString() + "_name";
257 string strKeyVal = strKey + i.ToString() + "_val";
258 TYPE typeName;
259 TYPE typeVal;
260
261 if (GetType(strKeyName, out typeName) && GetType(strKeyVal, out typeVal))
262 {
263 if (typeName == TYPE.STRING && typeVal != TYPE.STRING)
264 {
265 string strName = GetString(strKeyName);
266 double dfVal = GetNumeric(strKeyVal);
267 rg.Add(strName, dfVal);
268 }
269 }
270 }
271
272 return rg;
273 }
274
281 public Dictionary<string, string> ToStringValues(int nCount, string strKey)
282 {
283 Dictionary<string, string> rg = new Dictionary<string, string>();
284
285 for (int i = 0; i < nCount; i++)
286 {
287 string strKeyName = strKey + i.ToString() + "_name";
288 string strKeyVal = strKey + i.ToString() + "_val";
289 TYPE typeName;
290 TYPE typeVal;
291
292 if (GetType(strKeyName, out typeName) && GetType(strKeyVal, out typeVal))
293 {
294 if (typeName == TYPE.STRING && typeVal == TYPE.STRING)
295 {
296 string strName = GetString(strKeyName);
297 string strVal = GetString(strKeyVal);
298 rg.Add(strName, strVal);
299 }
300 }
301 }
302
303 return rg;
304 }
305 }
306}
The SimpleDictionary is a dictionary used to store a set of key/value pairs, primarily as the DICTION...
static SimpleDictionary FromByteArray(byte[] rg)
Creates a new dictionary from a byte array.
void Add(string strName, string strVal)
Add a new string item to the dictionary.
void Add(string strName, double dfVal)
Add a new numeric item to the dictionary.
void Add(string strName, int nVal)
Add a new integer item to the dictionary.
List< KeyValuePair< string, object > > ToList()
Get the list of values in the dictionary.
bool GetType(string strName, out TYPE type)
Returns the type of a given item.
Dictionary< string, string > ToStringValues(int nCount, string strKey)
Convert all string values into a standard Dictionary.
double GetNumeric(string strName)
Returns the numeric value of an item.
int GetInteger(string strName, int? nDefault=null)
Returns the integer value of an item.
string GetString(string strName)
Returns the string value of an item.
byte[] ToByteArray()
Converts the dictionary to a byte array.
TYPE
Defines the value type of each element.
Dictionary< string, double > ToNumericValues(int nCount, string strKey)
Convert all numeric values into a standard Dictionary.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
@ NONE
No training category specified.