2using System.Collections.Generic;
21 void Save(BinaryWriter bw);
28 object Load(BinaryReader br,
bool bNewInstance =
true);
53 return nIdx + nNumAxes;
68 for (
int i = nStartIdx; i < rg.Count; i++)
83 public static int Count(List<int> rgShape,
int nStartIdx = 0,
int nEndIdx = -1)
88 nEndIdx = rgShape.Count;
90 for (
int i = nStartIdx; i < nEndIdx; i++)
105 public static int Count(
int[] rgShape,
int nStartIdx = 0,
int nEndIdx = -1)
110 nEndIdx = rgShape.Length;
112 for (
int i = nStartIdx; i < nEndIdx; i++)
114 nCount *= rgShape[i];
126 public static void Save<T>(BinaryWriter bw, List<T> rg)
128 int nCount = (rg !=
null) ? rg.Count : 0;
135 if (typeof(T) == typeof(
string))
139 bw.Write(t.ToString());
145 if (typeof(T) == typeof(
double))
149 bw.Write((
double)Convert.ChangeType(t, typeof(
double)));
155 if (typeof(T) == typeof(
float))
159 bw.Write((
float)Convert.ChangeType(t, typeof(
float)));
165 if (typeof(T) == typeof(
int))
169 bw.Write((
int)Convert.ChangeType(t, typeof(
int)));
175 if (typeof(T) == typeof(
long))
179 bw.Write((
long)Convert.ChangeType(t, typeof(
long)));
185 if (typeof(T) == typeof(uint))
189 bw.Write((uint)Convert.ChangeType(t, typeof(uint)));
195 if (typeof(T) == typeof(
bool))
199 bw.Write((
bool)Convert.ChangeType(t, typeof(
bool)));
221 throw new Exception(
"Persistance for type " + (typeof(T)).ToString() +
" is not supported!");
230 public static List<T>
Load<T>(BinaryReader br)
232 int nCount = br.ReadInt32();
233 List<T> rg =
new List<T>(nCount);
238 if (typeof(T) == typeof(
string))
240 for (
int i = 0; i < nCount; i++)
242 string val = br.ReadString();
243 rg.Add((T)Convert.ChangeType(val, typeof(T)));
249 if (typeof(T) == typeof(
double))
251 for (
int i = 0; i < nCount; i++)
253 double val = br.ReadDouble();
254 rg.Add((T)Convert.ChangeType(val, typeof(T)));
260 if (typeof(T) == typeof(
float))
262 for (
int i = 0; i < nCount; i++)
264 float val = br.ReadSingle();
265 rg.Add((T)Convert.ChangeType(val, typeof(T)));
271 if (typeof(T) == typeof(
int))
273 for (
int i = 0; i < nCount; i++)
275 int val = br.ReadInt32();
276 rg.Add((T)Convert.ChangeType(val, typeof(T)));
282 if (typeof(T) == typeof(
long))
284 for (
int i = 0; i < nCount; i++)
286 long val = br.ReadInt64();
287 rg.Add((T)Convert.ChangeType(val, typeof(T)));
293 if (typeof(T) == typeof(uint))
295 for (
int i = 0; i < nCount; i++)
297 uint val = br.ReadUInt32();
298 rg.Add((T)Convert.ChangeType(val, typeof(T)));
304 if (typeof(T) == typeof(
bool))
306 for (
int i = 0; i < nCount; i++)
308 bool val = br.ReadBoolean();
309 rg.Add((T)Convert.ChangeType(val, typeof(T)));
315 object obj = Activator.CreateInstance(typeof(T));
320 for (
int i = 0; i < nCount; i++)
322 object val = p.
Load(br);
323 rg.Add((T)Convert.ChangeType(val, typeof(T)));
329 throw new Exception(
"Persistance for type " + (typeof(T)).ToString() +
" is not supported!");
337 public static void Save(BinaryWriter bw, List<double> rg)
341 for (
int i=0; i<rg.Count; i++)
354 List<double> rg =
new List<double>();
355 int nCount = br.ReadInt32();
357 for (
int i = 0; i < nCount; i++)
359 rg.Add(br.ReadDouble());
370 public static void Save(BinaryWriter bw, List<float> rg)
374 for (
int i = 0; i < rg.Count; i++)
387 List<float> rg =
new List<float>();
388 int nCount = br.ReadInt32();
390 for (
int i = 0; i < nCount; i++)
392 rg.Add(br.ReadSingle());
403 public static void Save(BinaryWriter bw,
int? nVal)
405 bw.Write(nVal.HasValue);
408 bw.Write(nVal.Value);
418 bool bHasVal = br.ReadBoolean();
423 return br.ReadInt32();
433 if (typeof(T) == typeof(
float))
434 return sizeof(float);
436 return sizeof(double);
447 return (
double)Convert.ChangeType(fVal, typeof(
double));
458 return (
float)Convert.ChangeType(fVal, typeof(
float));
469 return (T)Convert.ChangeType(dfVal, typeof(T));
480 if (typeof(T) == typeof(
double))
481 return (
double[])Convert.ChangeType(rg, typeof(
double[]));
483 double[] rgdf =
new double[rg.Length];
484 Array.Copy(rg, rgdf, rg.Length);
498 if (typeof(T) == typeof(
float) && nStart == 0)
499 return (
float[])Convert.ChangeType(rg, typeof(
float[]));
501 float[] rgf =
new float[rg.Length - nStart];
502 Array.Copy(Array.ConvertAll(rg, p => Convert.ToSingle(p)), nStart, rgf, 0, rgf.Length);
515 if (typeof(T) == typeof(
double))
516 return (T[])Convert.ChangeType(rgdf, typeof(T[]));
518 T[] rgt =
new T[rgdf.Length];
520 if (typeof(T) == typeof(
float))
521 Array.Copy(Array.ConvertAll(rgdf, p => Convert.ToSingle(p)), rgt, rgdf.Length);
523 Array.Copy(rgdf, rgt, rgdf.Length);
536 if (typeof(T) == typeof(
float))
537 return (T[])Convert.ChangeType(rgf, typeof(T[]));
539 T[] rgt =
new T[rgf.Length];
540 Array.Copy(rgf, rgt, rgf.Length);
552 double[] rgt =
new double[rgf.Length];
553 Array.Copy(rgf, rgt, rgf.Length);
565 public static void Resize<T>(ref List<T> rg,
int nCount, T tDefault)
570 while (rg.Count < nCount)
575 while (rg.Count > nCount)
577 rg.RemoveAt(rg.Count - 1);
592 T[] rg1 =
new T[rg.Length];
593 Array.Copy(rg, rg1, rg.Length);
605 public static List<T>
Clone<T>(List<T> rg,
int nMaxCount =
int.MaxValue)
610 List<T> rg1 =
new List<T>();
612 for (
int i=0; i<rg.Count && i<nMaxCount; i++)
614 ICloneable cloneable = rg[i] as ICloneable;
616 if (cloneable !=
null)
617 rg1.Add((T)Convert.ChangeType(cloneable.Clone(), typeof(T)));
632 public static List<T>
Clone<T>(T[] rg,
int nMaxCount =
int.MaxValue)
634 List<T> rg1 =
new List<T>();
636 for (
int i = 0; i < rg.Length && i < nMaxCount; i++)
638 ICloneable cloneable = rg[i] as ICloneable;
640 if (cloneable !=
null)
641 rg1.Add((T)Convert.ChangeType(cloneable.Clone(), typeof(T)));
657 public static bool Compare<T>(List<T> rg1, List<T> rg2,
bool bExact =
true)
659 if (rg1.Count != rg2.Count)
664 if (Math.Abs(rg1.Count - rg2.Count) > 1)
667 T tOne = (T)Convert.ChangeType(1, typeof(T));
669 if (rg1.Count > rg2.Count && !rg1[rg1.Count - 1].Equals(tOne))
672 if (rg2.Count > rg1.Count && !rg2[rg2.Count - 1].Equals(tOne))
676 for (
int i = 0; i < rg1.Count && i < rg2.Count; i++)
678 IComparable compare1 = rg1[i] as IComparable;
680 if (compare1 !=
null)
682 if (compare1.CompareTo((
object)Convert.ChangeType(rg2[i], typeof(
object))) != 0)
687 if (rg1[i].ToString() != rg2[i].ToString())
704 List<T> rg =
new List<T>();
706 for (
int i = 0; i < nCount; i++)
721 public static List<int>
Create(
int nCount,
int nStart,
int nInc)
723 List<int> rg =
new List<int>();
726 for (
int i = 0; i < nCount; i++)
741 public static void Set<T>(List<T> rg, T fVal)
743 for (
int i = 0; i < rg.Count; i++)
755 public static void Set<T>(T[] rg, T fVal)
757 for (
int i = 0; i < rg.Length; i++)
773 public static string ToString<T>(List<T> rg,
int nDecimals = -1,
int nIdxHighight = -1,
string strStart =
"{",
string strEnd =
"}")
775 string strOut = strStart;
777 for (
int i = 0; i < rg.Count; i++)
779 if (nIdxHighight >= 0 && i == nIdxHighight)
784 if (typeof(T) == typeof(
float))
786 float f = (float)Convert.ChangeType(rg[i], typeof(
float));
787 strOut += f.ToString(
"N" + nDecimals.ToString());
789 else if (typeof(T) == typeof(
double))
791 double f = (double)Convert.ChangeType(rg[i], typeof(
double));
792 strOut += f.ToString(
"N" + nDecimals.ToString());
796 strOut += rg[i].ToString();
800 strOut += rg[i].ToString();
802 if (nIdxHighight >= 0 && i == nIdxHighight)
808 strOut = strOut.TrimEnd(
',');
821 string[] rg = str.Split(
',');
822 List<int> rgVal =
new List<int>();
824 foreach (
string str1
in rg)
826 rgVal.Add(
int.Parse(str1));
839 if (str ==
null || str.Length == 0)
842 if (!
char.IsNumber(str[str.Length - 1]))
845 for (
int i = str.Length - 1; i > 0; i--)
847 if (!
char.IsNumber(str[str.Length - 1]))
849 string strNum = str.Substring(i);
850 return int.Parse(strNum);
864 public static string Replace(
string str,
char ch1,
char ch2)
871 foreach (
char ch
in str)
889 public static string Replace(
string str,
char ch1,
string str2)
896 foreach (
char ch
in str)
914 public static string Replace(
string str,
string str1,
char ch2)
921 while (str.Length > 0)
923 int nPos = str.IndexOf(str1);
926 strOut += str.Substring(0, nPos);
928 str = str.Substring(nPos + str1.Length);
947 public static string ReplaceMacro(
string strRaw,
string strMacroName,
string strReplacement)
952 while (strRaw.Length > 0)
954 nPos = strRaw.IndexOf(strMacroName);
958 strOut += strRaw.Substring(0, nPos);
959 strOut += strReplacement;
960 strRaw = strRaw.Substring(nPos + strMacroName.Length);
978 public static string ReplaceMacros(
string strRaw, List<KeyValuePair<string, string>> rgMacros)
980 string strOut = strRaw;
982 foreach (KeyValuePair<string, string> kv
in rgMacros)
998 DateTime dt1 =
new DateTime(1980, 1, 1);
999 TimeSpan ts = dt - dt1;
1000 return ts.TotalMinutes;
1010 DateTime dt1 =
new DateTime(1980, 1, 1);
1011 TimeSpan ts = TimeSpan.FromMinutes(dfMin);
1023 if (!nSeed.HasValue)
1024 nSeed = (int)DateTime.Now.Ticks;
1026 Random random =
new Random(nSeed.Value);
1027 List<int> rg1 =
new List<int>();
1029 while (rg.Count > 0)
1038 int nIdx = random.Next(rg.Count);
1054 public static List<string>
LoadTextLines(
string strFile,
Log log =
null,
bool bPrependPath =
true)
1056 List<string> rgstr =
new List<string>();
1060 string strPath = Path.GetDirectoryName(strFile);
1062 using (StreamReader sr =
new StreamReader(strFile))
1064 string strLine = sr.ReadLine();
1066 while (strLine !=
null)
1068 if (strLine.Length > 0)
1070 if (strLine[0] ==
'.' && bPrependPath)
1072 int nPos = strLine.LastIndexOf(
'/');
1074 nPos = strLine.LastIndexOf(
'\\');
1077 strLine = strLine.Substring(nPos + 1);
1079 strLine = strPath +
"\\" +
Replace(strLine,
'/',
'\\');
1085 strLine = sr.ReadLine();
1089 catch (Exception excpt)
1092 log.FAIL(
"Failed to load '" + strFile +
"'! Error: " + excpt.Message);
The Log class provides general output in text form.
The Utility class provides general utility funtions.
static bool Compare< T >(List< T > rg1, List< T > rg2, bool bExact=true)
Compares one List to another.
static List< int > ParseListToInt(string str)
Parses a comma delimited string into an array of int.
static string Replace(string str, char ch1, string str2)
Replaces each instance of one character with another string in a given string.
static double[] ConvertVec< T >(T[] rg)
Convert an array of generics to an array of double.
static void Save(BinaryWriter bw, List< double > rg)
Save a list of double to a binary writer.
static string Replace(string str, char ch1, char ch2)
Replaces each instance of one character with another character in a given string.
static double ConvertVal< T >(T fVal)
Convert a generic to a double.
Utility()
The Utility constructor.
static string ReplaceMacro(string strRaw, string strMacroName, string strReplacement)
The ConvertMacro method is used to replace a set of macros in a given string.
static List< double > LoadDouble(BinaryReader br)
Loads a list of double from a binary reader.
static int CanonicalAxisIndex(int nIdx, int nNumAxes)
Returns the 'canonical' version of a (usually) user-specified axis, allowing for negative indexing (e...
static List< T > Create< T >(int nCount, T fDefault)
Create a new List and fill it with default values up to a given count.
static List< int > Create(int nCount, int nStart, int nInc)
Create a new List and fill it with values starting with start and incrementing by inc.
static void Save(BinaryWriter bw, int? nVal)
Saves a nullable int to a binary writer.
static void Save< T >(BinaryWriter bw, List< T > rg)
Save a list of items to a binary writer.
static float ConvertValF< T >(T fVal)
Convert a generic to a float.
static string Replace(string str, string str1, char ch2)
Replaces each instance of one character with another string in a given string.
static float[] ConvertVecF< T >(T[] rg, int nStart=0)
Convert an array of generics to an array of float.
static List< int > RandomShuffle(List< int > rg, int? nSeed=null)
Randomly shuffle the entries in the specified list.
static int GetNumber(string str)
Parses a string into a number, or if the string does not contain a number returns 0.
static DateTime ConvertTimeFromMinutes(double dfMin)
Convert a number of minutes into the date time equivalent to 1/1/1980 + the minutes.
static T[] Clone< T >(T[] rg)
Copy an array.
static int Count(int[] rgShape, int nStartIdx=0, int nEndIdx=-1)
Return the count of items given the shape.
static void Resize< T >(ref List< T > rg, int nCount, T tDefault)
Resize a List and fill the new elements with the default value.
static List< float > LoadFloat(BinaryReader br)
Loads a list of float from a binary reader.
static string ReplaceMacros(string strRaw, List< KeyValuePair< string, string > > rgMacros)
The ReplaceMacros method is used to replace a set of macros in a given string.
static List< string > LoadTextLines(string strFile, Log log=null, bool bPrependPath=true)
Load each line of a text file and return the contents as a list.
static void Set< T >(List< T > rg, T fVal)
Set all values of a List with a given value.
static int GetSpatialDim(List< int > rg, int nStartIdx=0)
Calculate the spatial dimension of an array starting at a given axis.
static int BaseTypeSize< T >()
Returns the base type size, where double = 8, float = 4.
static string ToString< T >(List< T > rg, int nDecimals=-1, int nIdxHighight=-1, string strStart="{", string strEnd="}")
Convert an array to a string.
static int Count(List< int > rgShape, int nStartIdx=0, int nEndIdx=-1)
Return the count of items given the shape.
static void Save(BinaryWriter bw, List< float > rg)
Save a list of float to a binary writer.
static double[] ConvertVec(float[] rgf)
Convert an array of float to an array of generics.
static List< T > Load< T >(BinaryReader br)
Loads a list of items from a binary reader.
static double ConvertTimeToMinutes(DateTime dt)
Convert a date time into minutes since 1/1/1980
static ? int LoadInt(BinaryReader br)
Loads a nullable int from a binary reader.
The IBinaryPersist interface provides generic save and load functionality.
void Save(BinaryWriter bw)
Save to a binary writer.
object Load(BinaryReader br, bool bNewInstance=true)
Load from a binary reader.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.