Merging changes

This commit is contained in:
Taha committed 2023-09-04 16:25:40 -04:00
1 parent c4216d2272
commit 797816be7e
437 files changed
+54228 -28

No files matched your search

@@ -0,0 +1,141 @@
using System;
using System.Reflection;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
namespace BCG.Utilities.General
{
public class CSVParser
{
enum state
{
waitingForToken,
inQuote,
waitingForSeparator,
possibleEndOfQuote,
inToken,
}
class CSVParseException : Exception
{
public CSVParseException (string msg) : base (msg)
{}
}
public static string [] parse (string csv)
{
ArrayList tokens = new ArrayList();
StringBuilder currentToken = null;
state s = state.waitingForToken;
int n = 0;
foreach (char c in csv)
{
n++;
switch (s)
{
case state.waitingForToken:
if (c == ',')
tokens.Add("");
else if (c == '"')
{
s = state.inQuote;
currentToken = new StringBuilder();
}
else if (Char.IsWhiteSpace(c))
continue;
else
{
s = state.inToken;
currentToken = new StringBuilder();
currentToken.Append(c);
}
break;
case state.inQuote:
if (c == '"')
{
s = state.possibleEndOfQuote;
}
else
{
currentToken.Append(c);
}
break;
case state.possibleEndOfQuote:
if (c == '"')
{
s = state.inQuote;
currentToken.Append(c);
}
else if (Char.IsWhiteSpace(c))
{
tokens.Add(currentToken.ToString());
s = state.waitingForSeparator;
}
else if (c == ',')
{
tokens.Add(currentToken.ToString());
s = state.waitingForToken;
}
else
throw new Exception("Expected ',' or end of line at column " + n.ToString());
break;
case state.waitingForSeparator :
if (c == ',')
{
s = state.waitingForToken;
}
else if (Char.IsWhiteSpace(c))
continue;
else
throw new Exception("Expected ',' or end of line at column " + n.ToString());
break;
case state.inToken :
if (c == ',')
{
s = state.waitingForToken;
tokens.Add(currentToken.ToString());
}
else
{
currentToken.Append(c);
}
break;
}
}
if (s == state.inToken || s == state.possibleEndOfQuote)
tokens.Add(currentToken.ToString());
else if (s == state.waitingForToken)
tokens.Add("");
else if ( s != state.waitingForSeparator)
throw new Exception("Missing column or unterminated string at end of line");
string [] result = new string[tokens.Count];
tokens.CopyTo(result,0);
return result;
}
static void test()
{
for (;;)
{
try
{
foreach (string s in parse(Console.ReadLine()))
{
Console.WriteLine(s);
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}
}
@@ -0,0 +1,397 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace BCG
{
[Serializable]
public class CollectionHelpers
{
public delegate bool EqualDelegate(object a, object b);
public static ArrayList difference (ICollection a, ICollection b,
EqualDelegate ed)
{
ArrayList result = new ArrayList();
foreach (Object aa in a)
{
bool found = false;
foreach (Object bb in b)
{
if (ed(aa, bb))
{
found = true;
break;
}
}
if (!found)
result.Add (aa);
}
return result;
}
public static ArrayList difference (ICollection a, ICollection b)
{
ArrayList result = new ArrayList();
foreach (Object aa in a)
{
bool found = false;
foreach (Object bb in b)
{
if (aa.Equals(bb))
{
found = true;
break;
}
}
if (!found)
result.Add (aa);
}
return result;
}
public static ArrayList sorted_difference (ICollection sa, ICollection sb) {
ArrayList result = new ArrayList ();
IEnumerator ia = sa.GetEnumerator ();
IEnumerator ib = sb.GetEnumerator ();
IEnumerator iremainder = ia;
bool moreA,moreB;
for (moreA = ia.MoveNext (), moreB = ib.MoveNext (); moreA && moreB;) {
int cmp = ((IComparable)ia.Current).CompareTo (ib.Current);
if (cmp < 0) {
result.Add (ia.Current);
moreA = ia.MoveNext ();
iremainder = ia;
}
else if (cmp > 0)
moreB = ib.MoveNext ();
else {
moreA = ia.MoveNext ();
moreB = ib.MoveNext ();
}
}
for (; moreA; moreA = iremainder.MoveNext ())
result.Add (iremainder.Current);
return result;
}
public static bool typedequal<T>(ICollection<T> lhs, ICollection<T> rhs)
{
if(lhs == null || rhs == null)
{
return false;
}
T[] l = new T[lhs.Count];
T[] r = new T[rhs.Count];
lhs.CopyTo(l, 0);
rhs.CopyTo(r, 0);
return equal((IList)l, (IList)r);
}
public static bool equal(ICollection lhs, ICollection rhs)
{
if ( lhs == null || rhs == null )
{
return false;
}
Array l = new object[lhs.Count];
Array r = new object[rhs.Count];
lhs.CopyTo(l, 0);
rhs.CopyTo(r,0);
return equal(l,r);
}
public static bool equal(IList lhs, IList rhs)
{
if ( lhs == null || rhs == null )
{
return false;
}
if(lhs.Count != rhs.Count)
return false;
else
{
for(int i = 0; i < lhs.Count; i++)
{
if(!lhs[i].Equals(rhs[i]))
return false;
}
return true;
}
}
public static Array fromArrayList(ArrayList al, Type t)
{
Array result = Array.CreateInstance(t, al.Count);
al.CopyTo(result,0);
return result;
}
public static Array append (Array a1, Array a2, Type t)
{
Array result = Array.CreateInstance(t, a1.Length + a2.Length);
a1.CopyTo(result,0);
a2.CopyTo(result,a1.Length);
return result;
}
public static Array subtract (Array from, params object [] elemens)
{
return fromArrayList(
difference(from, elemens),from.GetType().
GetElementType());
}
public static bool hasDuplicates (ICollection collection)
{
Hashtable h = new Hashtable();
foreach (object o in collection)
h[o] = true;
return h.Keys.Count != collection.Count;
}
public static ICollection subtract (IDictionary d1, IDictionary d2)
{
ArrayList result = new ArrayList();
foreach (object d1k in d1.Keys)
if (!d2.Contains(d1k))
result.Add(d1k);
return result;
}
/// <summary>
/// Is c1 a subset of c2?
/// </summary>
/// <param name="c1"></param>
/// <param name="c2"></param>
/// <returns></returns>
public static bool isSubset (ICollection c1, ICollection c2)
{return difference(c1,c2).Count == 0;}
public static void orderedUniqueInsert (ArrayList coll,object item) {
int ifnd = coll.BinarySearch (item);
if (ifnd < 0)
coll.Insert (~ifnd,item);
else
coll[ifnd] = item;
}
public static void orderedRemove (ArrayList coll,object item) {
int ifnd = coll.BinarySearch (item);
if (ifnd >= 0)
coll.RemoveAt (ifnd);
}
public static bool t_equals<T>(T t1, T t2) { return (t1 != null && t2 != null && t1.Equals(t2)) || (t1 == null && t2 == null); }
public static bool dictionaries_equal<K, V>(IDictionary<K, V> new_d, IDictionary<K, V> old_d, Func<V, V, bool> comp = null)
{
IDictionary<K, V> deleted_d, added_d, changed_d;
compare_dictionaries(new_d, old_d, out deleted_d, out added_d, out changed_d, comp);
return deleted_d.Count == 0 && added_d.Count == 0 && changed_d.Count == 0;
}
public static void compare_dictionaries<K, V>(IDictionary<K, V> new_d, IDictionary<K, V> old_d,
out IDictionary<K, V> deleted_d, out IDictionary<K, V> added_d, out IDictionary<K, V> changed_d,
Func<V, V, bool> comp = null)
{
new_d = new_d ?? new Dictionary<K, V>();
old_d = old_d ?? new Dictionary<K, V>();
deleted_d = new Dictionary<K, V>();
added_d = new Dictionary<K, V>();
changed_d = new Dictionary<K, V>();
comp = comp ?? t_equals<V>;
foreach (KeyValuePair<K, V> p in new_d)
{
V old_d_val = default(V);
if (!old_d.TryGetValue(p.Key, out old_d_val))
added_d.Add(p);
else if (!comp(p.Value, old_d_val))
changed_d.Add(p);
}
foreach (KeyValuePair<K, V> p in old_d)
if (!new_d.ContainsKey(p.Key))
deleted_d.Add(p);
}
/// Compares 2 dictionaries with the same key types
/// but different data types. Takes a function to
/// compare the 2 data types
public static void compare_dictionaries<K, N, O>(IReadOnlyDictionary<K, N> new_d, IReadOnlyDictionary<K, O> old_d,
out IDictionary<K, O> deleted_d, out IDictionary<K, N> added_d, out IDictionary<K, Tuple<N,O>> changed_d,
Func<N, O, bool> comp)
{
new_d = new_d ?? new Dictionary<K, N>();
old_d = old_d ?? new Dictionary<K, O>();
deleted_d = new Dictionary<K, O>();
added_d = new Dictionary<K, N>();
changed_d = new Dictionary<K, Tuple<N,O>>();
foreach (KeyValuePair<K, N> p in new_d)
{
O old_val = default(O);
if (!old_d.TryGetValue(p.Key, out old_val))
added_d.Add(p);
else if (!comp(p.Value, old_val))
changed_d.Add(p.Key, new Tuple<N, O>(p.Value, old_val));
}
foreach (KeyValuePair<K, O> p in old_d)
if (!new_d.ContainsKey(p.Key))
deleted_d.Add(p);
}
public static void compare_dictionary_keys<K, V>(IDictionary<K, V> new_d, IDictionary<K, V> old_d,
out HashSet<K> deleted_keys, out HashSet<K> added_keys)
{
new_d = new_d ?? new Dictionary<K, V>();
old_d = old_d ?? new Dictionary<K, V>();
deleted_keys = new HashSet<K>();
added_keys = new HashSet<K>();
foreach (KeyValuePair<K, V> p in new_d)
if (!old_d.ContainsKey(p.Key))
added_keys.Add(p.Key);
foreach (KeyValuePair<K, V> p in old_d)
if (!new_d.ContainsKey(p.Key))
deleted_keys.Add(p.Key);
}
public static void compare_hashsets<T>(HashSet<T> new_d, HashSet<T> old_d,
out HashSet<T> deleted_d, out HashSet<T> added_d)
{
added_d = new HashSet<T>();
deleted_d = new HashSet<T>();
foreach (T t in new_d)
if (!old_d.Contains(t))
added_d.Add(t);
foreach (T t in old_d)
if (!new_d.Contains(t))
deleted_d.Add(t);
}
public static IEnumerable<T> append_arrays<T>(params IEnumerable<T>[] ienum_list)
{
List<T> rv = new List<T>();
foreach (IEnumerable<T> ienum in ienum_list)
rv.AddRange(ienum);
return rv;
}
public static bool hashset_equals<T>(HashSet<T> new_d, HashSet<T> old_d)
{
HashSet<T> deleted_d, added_d;
compare_hashsets(new_d, old_d, out deleted_d, out added_d);
return deleted_d.Count == 0 && added_d.Count == 0;
}
public static void find_neighbors<T>(IEnumerable<T> items, Comparison<T> comp_func, T comp, out int low_index, out int high_index)
{
low_index = high_index = -1;
for (int i = 0; i < items.Count(); i++)
{
T p = items.ElementAt (i);
if (comp_func (comp, p) <= 0)
{
high_index = i;
low_index = i - 1;
if (low_index >= 0)
{
T low_index_val = items.ElementAt (low_index);
if (comp_func(comp, low_index_val) >= 0)
break;
}
else
break;
}
else
{
low_index = i;
high_index = i + 1;
if (high_index <= items.Count() - 1)
{
T high_index_val = items.ElementAt (high_index);
if (comp_func(comp, high_index_val) <= 0)
break;
}
else
{
high_index = -1;
break;
}
}
}
}
public static void find_neighbors<T, P>(IEnumerable<T> list, P nearest_to, Func<T, P, double> diff_measure,
out T low_neighbor, out T high_neighbor)
{
low_neighbor = default(T); high_neighbor = default(T);
list = list ?? new List<T>();
double low_min_diff = double.MaxValue, high_min_diff = double.MaxValue;
foreach (T t in list)
{
double this_diff = Math.Abs(diff_measure(t, nearest_to));
if (this_diff <= 0)
{
this_diff = Math.Abs(this_diff);
if (this_diff < low_min_diff)
{
low_min_diff = this_diff;
low_neighbor = t;
}
}
else
{
if (this_diff < high_min_diff)
{
high_min_diff = this_diff;
high_neighbor = t;
}
}
}
}
public static T find_nearest<T,P>(IEnumerable<T> list, P nearest_to, Func<T, double> diff_measure)
{
list = list ?? new List<T>();
double min_diff = double.MaxValue;
T rv = default(T);
foreach (T t in list)
{
double this_diff = Math.Abs(diff_measure(t));
if (this_diff < min_diff)
{
rv = t;
min_diff = this_diff;
}
}
return rv;
}
public static bool are_maps_equal<K, T>(IDictionary<K, T> m1, IDictionary<K, T> m2, Func<T, T, bool> equality_comparer = null)
{
m1 = m1 ?? new Dictionary<K, T>();
m2 = m2 ?? new Dictionary<K, T>();
if (equality_comparer == null)
equality_comparer = (t1, t2) => { return t1.Equals(t2); };
IDictionary<K, T> deleted_d, added_d;
IDictionary<K, Tuple<T, T>> changed_d;
compare_dictionaries<K, T, T>(new Dictionary<K, T>(m1), new Dictionary<K, T>(m2),
out deleted_d, out added_d, out changed_d, equality_comparer);
return deleted_d.Count + added_d.Count + changed_d.Count == 0;
}
}
}
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BCG
{
public class TypeHelper
{
private static HashSet<TypeCode> _numeric_type_codes = new HashSet<TypeCode>
{
TypeCode.Byte, TypeCode.Decimal, TypeCode.Double, TypeCode.Int16, TypeCode.Int32,
TypeCode.Int64, TypeCode.SByte, TypeCode.Single, TypeCode.UInt16, TypeCode.UInt32,
TypeCode.UInt64
};
public static bool is_dbnull(object o) { return o == null || o == DBNull.Value; }
public static bool is_numeric_type(object o)
{
return is_dbnull(o) ? false : _numeric_type_codes.Contains(Type.GetTypeCode(o.GetType()));
}
public static T[] parse_string_to_typed_array<T>(string text, params char[] seperators)
{
if (text == null || text.Length == 0)
return new T[] { };
else
{
List<T> rv = new List<T>();
string[] toks = text.Split(seperators);
foreach (string t in toks)
{
string fin_t = t.Trim();
if (!String.IsNullOrEmpty(fin_t))
{
try
{
T ct = (T)Convert.ChangeType(fin_t, typeof(T));
if (ct != null)
rv.Add(ct);
}
catch (Exception) { }
}
}
return rv.ToArray();
}
}
}
}
@@ -0,0 +1,48 @@
using System;
using System.Reflection;
using System.Diagnostics;
namespace BCG.Utilities.General
{
public class TypeInstanceLoader
{
private TypeInstanceLoader ()
{
}
public static object loadInstance (string typename, params object[] parameters)
{
Type t = null;
try
{
t = loadType (typename);
}
catch (ApplicationException e)
{
Debug.Assert (false, e.Message);
}
return Activator.CreateInstance (t, parameters);
}
public static Type loadType (string name)
{
return loadType(name, false);
}
public static Type loadType (string name, bool throwOnError)
{
Type t = null;
Assembly[] ass = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly a in ass)
{
t = a.GetType(name, false, true);
if (t != null)
return t;
}
if(throwOnError)
throw new ApplicationException ("Can't find type '" + name + "' in all assemblies.");
else
return null;
}
}
}