MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
PropertySet.cs
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.IO;
5using System.Linq;
6using System.Text;
7using System.Threading.Tasks;
8
9namespace MyCaffe.basecode
10{
14 [Serializable]
15 public class PropertySet : IEnumerable<KeyValuePair<string, string>>
16 {
17 Dictionary<string, string> m_rgProperties = new Dictionary<string, string>();
18 Dictionary<string, int> m_rgPropertiesInt = new Dictionary<string, int>();
19 Dictionary<string, byte[]> m_rgBlobs = new Dictionary<string, byte[]>();
20
25 public PropertySet(Dictionary<string, string> rgProp)
26 {
27 m_rgProperties = rgProp;
28 }
29
34 public PropertySet(string strProp)
35 {
36 string[] rgstr = strProp.Split(';');
37
38 m_rgProperties = new Dictionary<string, string>();
39
40 foreach (string strP in rgstr)
41 {
42 if (strP.Length > 0)
43 {
44 int nPos = strP.IndexOf('=');
45 if (nPos > 0)
46 {
47 string strKey = strP.Substring(0, nPos);
48 string strVal = strP.Substring(nPos + 1);
49
50 if (!m_rgProperties.ContainsKey(strKey))
51 m_rgProperties.Add(strKey, strVal);
52 }
53 }
54 }
55 }
56
60 public PropertySet()
61 {
62 }
63
67 public List<string> PropertyNames
68 {
69 get { return m_rgProperties.Keys.ToList(); }
70 }
71
75 public List<string> PropertyBlobNames
76 {
77 get { return m_rgBlobs.Keys.ToList(); }
78 }
79
83 public List<string> PropertyIntNames
84 {
85 get { return m_rgPropertiesInt.Keys.ToList(); }
86 }
87
93 public bool MovePropertyToBlob(string strName)
94 {
95 if (!m_rgProperties.ContainsKey(strName))
96 return false;
97
98 string strVal = m_rgProperties[strName];
99 m_rgProperties.Remove(strName);
100
101 using (MemoryStream ms = new MemoryStream())
102 using (BinaryWriter bw = new BinaryWriter(ms))
103 {
104 foreach (char ch in strVal)
105 {
106 bw.Write((byte)ch);
107 }
108
109 bw.Write((byte)0);
110
111 m_rgBlobs.Add(strName, ms.ToArray());
112 }
113
114 return true;
115 }
116
121 public void Merge(PropertySet prop)
122 {
123 foreach (KeyValuePair<string, string> kv in prop.m_rgProperties)
124 {
125 if (!m_rgProperties.ContainsKey(kv.Key))
126 m_rgProperties.Add(kv.Key, kv.Value);
127 else
128 m_rgProperties[kv.Key] = kv.Value;
129 }
130
131 foreach (KeyValuePair<string, byte[]> kv in prop.m_rgBlobs)
132 {
133 if (!m_rgBlobs.ContainsKey(kv.Key))
134 m_rgBlobs.Add(kv.Key, kv.Value);
135 else
136 m_rgBlobs[kv.Key] = kv.Value;
137 }
138 }
139
146 public string GetProperty(string strName, bool bThrowExceptions = true)
147 {
148 if (!m_rgProperties.ContainsKey(strName))
149 {
150 if (bThrowExceptions)
151 throw new Exception("The property '" + strName + "' was not found!");
152
153 return null;
154 }
155
156 return m_rgProperties[strName];
157 }
158
165 public int GetPropertyInt(string strName, bool bThrowExceptions = true)
166 {
167 if (!m_rgPropertiesInt.ContainsKey(strName))
168 {
169 if (bThrowExceptions)
170 throw new Exception("The property '" + strName + "' was not found!");
171
172 return 0;
173 }
174
175 return m_rgPropertiesInt[strName];
176 }
177
184 public byte[] GetPropertyBlob(string strName, bool bThrowExceptions = true)
185 {
186 if (!m_rgBlobs.ContainsKey(strName))
187 {
188 if (bThrowExceptions)
189 throw new Exception("The property '" + strName + "' was not found!");
190
191 return null;
192 }
193
194 return m_rgBlobs[strName];
195 }
196
201 public void DeleteProperty(string strName)
202 {
203 m_rgProperties.Remove(strName);
204 }
205
211 public void SetProperty(string strName, string strVal)
212 {
213 if (!m_rgProperties.ContainsKey(strName))
214 m_rgProperties.Add(strName, strVal);
215 else
216 m_rgProperties[strName] = strVal;
217 }
218
224 public void SetPropertyInt(string strName, int nVal)
225 {
226 if (!m_rgProperties.ContainsKey(strName))
227 m_rgPropertiesInt.Add(strName, nVal);
228 else
229 m_rgPropertiesInt[strName] = nVal;
230 }
231
237 public void SetPropertyBlob(string strName, byte[] rg)
238 {
239 if (!m_rgBlobs.ContainsKey(strName))
240 m_rgBlobs.Add(strName, rg);
241 else
242 m_rgBlobs[strName] = rg;
243 }
244
250 public DateTime GetPropertyAsDateTime(string strName)
251 {
252 string strVal = GetProperty(strName);
253 DateTime dt;
254
255 if (!DateTime.TryParse(strVal, out dt))
256 throw new Exception("Failed to parse '" + strName + "' as a DateTime. The value = '" + strVal + "'");
257
258 return dt;
259 }
260
267 public bool GetPropertyAsBool(string strName, bool bDefault = false)
268 {
269 string strVal = GetProperty(strName, false);
270 if (strVal == null)
271 return bDefault;
272
273 bool bVal;
274
275 if (!bool.TryParse(strVal, out bVal))
276 throw new Exception("Failed to parse '" + strName + "' as an Boolean. The value = '" + strVal + "'");
277
278 return bVal;
279 }
280
287 public int GetPropertyAsInt(string strName, int nDefault = 0)
288 {
289 string strVal = GetProperty(strName, false);
290 if (strVal == null)
291 return nDefault;
292
293 int nVal;
294
295 if (!int.TryParse(strVal, out nVal))
296 throw new Exception("Failed to parse '" + strName + "' as an Integer. The value = '" + strVal + "'");
297
298 return nVal;
299 }
300
307 public double GetPropertyAsDouble(string strName, double dfDefault = 0)
308 {
309 string strVal = GetProperty(strName, false);
310 if (strVal == null)
311 return dfDefault;
312
313 double dfVal;
314
315 if (!BaseParameter.TryParse(strVal, out dfVal))
316 throw new Exception("Failed to parse '" + strName + "' as a Double. The value = '" + strVal + "'");
317
318 return dfVal;
319 }
320
325 public override string ToString()
326 {
327 string str = "";
328
329 foreach (KeyValuePair<string, string> kv in m_rgProperties)
330 {
331 str += kv.Key + "=" + kv.Value;
332 str += ";";
333 }
334
335 return str;
336 }
337
342 public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
343 {
344 return m_rgProperties.GetEnumerator();
345 }
346
351 IEnumerator IEnumerable.GetEnumerator()
352 {
353 return m_rgProperties.GetEnumerator();
354 }
355 }
356}
The BaseParameter class is the base class for all other parameter classes.
static bool TryParse(string strVal, out double df)
Parse double values using the US culture if the decimal separator = '.', then using the native cultur...
Specifies a key-value pair of properties.
Definition: PropertySet.cs:16
string GetProperty(string strName, bool bThrowExceptions=true)
Returns a property as a string value.
Definition: PropertySet.cs:146
List< string > PropertyIntNames
Returns the list of integer property names contained in the property set.
Definition: PropertySet.cs:84
int GetPropertyInt(string strName, bool bThrowExceptions=true)
Returns an int property as a int value.
Definition: PropertySet.cs:165
DateTime GetPropertyAsDateTime(string strName)
Returns a property as a DateTime value.
Definition: PropertySet.cs:250
List< string > PropertyBlobNames
Returns the list of blob names contained in the property set.
Definition: PropertySet.cs:76
void Merge(PropertySet prop)
Merge a given property set into this one.
Definition: PropertySet.cs:121
PropertySet(Dictionary< string, string > rgProp)
The constructor, initialized with a dictionary of properties.
Definition: PropertySet.cs:25
void DeleteProperty(string strName)
Delete a property by name.
Definition: PropertySet.cs:201
void SetPropertyBlob(string strName, byte[] rg)
Sets a property in the blob set to a byte array if it exists, otherwise it adds the new blob.
Definition: PropertySet.cs:237
byte[] GetPropertyBlob(string strName, bool bThrowExceptions=true)
Returns a property blob as a byte array value.
Definition: PropertySet.cs:184
void SetPropertyInt(string strName, int nVal)
Sets an int property in the property set to a value if it exists, otherwise it adds the new property.
Definition: PropertySet.cs:224
int GetPropertyAsInt(string strName, int nDefault=0)
Returns a property as an integer value.
Definition: PropertySet.cs:287
IEnumerator< KeyValuePair< string, string > > GetEnumerator()
Returns an enumerator of the key/value pairs.
Definition: PropertySet.cs:342
PropertySet()
The constructor.
Definition: PropertySet.cs:60
bool GetPropertyAsBool(string strName, bool bDefault=false)
Returns a property as a boolean value.
Definition: PropertySet.cs:267
bool MovePropertyToBlob(string strName)
Move the property from the list of properties to the list of blobs, storing the blob as a 0 terminate...
Definition: PropertySet.cs:93
double GetPropertyAsDouble(string strName, double dfDefault=0)
Returns a property as an double value.
Definition: PropertySet.cs:307
PropertySet(string strProp)
The constructor, initialized with a string containing a set of ';' separated key-value pairs.
Definition: PropertySet.cs:34
List< string > PropertyNames
Returns the list of property names contained in the property set.
Definition: PropertySet.cs:68
override string ToString()
Returns the string representation of the properties.
Definition: PropertySet.cs:325
void SetProperty(string strName, string strVal)
Sets a property in the property set to a value if it exists, otherwise it adds the new property.
Definition: PropertySet.cs:211
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12