2using System.Collections.Generic;
6using System.Threading.Tasks;
15 Dictionary<string, object> m_rgValues =
new Dictionary<string, object>();
16 Dictionary<string, TYPE> m_rgTypes =
new Dictionary<string, TYPE>();
58 if (!m_rgTypes.ContainsKey(strName))
61 type = m_rgTypes[strName];
73 if (m_rgTypes[strName] !=
TYPE.NUMERIC)
74 throw new Exception(
"Invalid type, expected '" + m_rgTypes[strName].ToString() +
"'");
76 return (
double)m_rgValues[strName];
85 public int GetInteger(
string strName,
int? nDefault =
null)
87 if (!m_rgTypes.ContainsKey(strName))
89 if (nDefault.HasValue)
90 return nDefault.Value;
92 throw new Exception(
"The variable '" + strName +
"' is not in the dictionary.");
95 if (m_rgTypes[strName] !=
TYPE.INTEGER)
96 throw new Exception(
"Invalid type, expected '" + m_rgTypes[strName].ToString() +
"'");
98 return (
int)m_rgValues[strName];
108 if (m_rgTypes[strName] !=
TYPE.STRING)
109 throw new Exception(
"Invalid type, expected '" + m_rgTypes[strName].ToString() +
"'");
111 return (
string)m_rgValues[strName];
119 public void Add(
string strName,
string strVal)
121 m_rgValues.Add(strName, strVal);
122 m_rgTypes.Add(strName,
TYPE.STRING);
130 public void Add(
string strName,
int nVal)
132 m_rgValues.Add(strName, nVal);
133 m_rgTypes.Add(strName,
TYPE.INTEGER);
141 public void Add(
string strName,
double dfVal)
143 m_rgValues.Add(strName, dfVal);
144 m_rgTypes.Add(strName,
TYPE.NUMERIC);
151 public List<KeyValuePair<string, object>>
ToList()
153 return m_rgValues.ToList();
162 using (MemoryStream ms =
new MemoryStream())
164 BinaryWriter bw =
new BinaryWriter(ms);
166 bw.Write(m_rgValues.Count);
168 List<KeyValuePair<string, object>> rgValues = m_rgValues.ToList();
169 List<KeyValuePair<string, TYPE>> rgTypes = m_rgTypes.ToList();
171 for (
int i = 0; i < rgValues.Count; i++)
173 bw.Write(rgTypes[i].Key);
174 bw.Write((
byte)rgTypes[i].Value);
176 switch (rgTypes[i].Value)
179 bw.Write(rgValues[i].Value.ToString());
183 bw.Write((
int)rgValues[i].Value);
187 bw.Write((
double)rgValues[i].Value);
208 using (MemoryStream ms =
new MemoryStream(rg))
210 BinaryReader br =
new BinaryReader(ms);
211 int nCount = br.ReadInt32();
213 for (
int i = 0; i < nCount; i++)
215 string strName = br.ReadString();
221 string strVal = br.ReadString();
222 dictionary.
Add(strName, strVal);
226 int nVal = br.ReadInt32();
227 dictionary.
Add(strName, nVal);
231 double dfVal = br.ReadDouble();
232 dictionary.
Add(strName, dfVal);
252 Dictionary<string, double> rg =
new Dictionary<string, double>();
254 for (
int i = 0; i < nCount; i++)
256 string strKeyName = strKey + i.ToString() +
"_name";
257 string strKeyVal = strKey + i.ToString() +
"_val";
261 if (
GetType(strKeyName, out typeName) &&
GetType(strKeyVal, out typeVal))
263 if (typeName ==
TYPE.STRING && typeVal !=
TYPE.STRING)
267 rg.Add(strName, dfVal);
283 Dictionary<string, string> rg =
new Dictionary<string, string>();
285 for (
int i = 0; i < nCount; i++)
287 string strKeyName = strKey + i.ToString() +
"_name";
288 string strKeyVal = strKey + i.ToString() +
"_val";
292 if (
GetType(strKeyName, out typeName) &&
GetType(strKeyVal, out typeVal))
294 if (typeName ==
TYPE.STRING && typeVal ==
TYPE.STRING)
298 rg.Add(strName, strVal);
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.
SimpleDictionary()
The constructor.
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.
@ NONE
No training category specified.