using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BCG { public class BlueRowHasher { private int _n_keys; private int _hash_counter = 0; private bool _thread_safe = false; private IDictionary _root_map = new Dictionary(); public BlueRowHasher(int n_keys, bool thread_safe = false) { _n_keys = n_keys; _thread_safe = thread_safe; } public string get_string_hash(IBlueRow row, int[] key_cols) { return get_hash(row, key_cols).ToString(); } /// /// gets the hash for a row with given keys, /// if key does not exist is adds it adds it /// and returns the hash /// public int get_hash(IBlueRow row, int[] key_cols) { IDictionary d = _root_map; for (int i = 0; ; i++) { object v = row[key_cols[i]]; if (i < _n_keys - 1) { object inner_d = null; if (!d.TryGetValue(v, out inner_d)) { inner_d = _get_new_dictionary(); d.Add(v, inner_d); } d = (IDictionary)inner_d; } else { object rv; if (!d.TryGetValue (v, out rv)) { rv = _get_next_hash(); d.Add(v, rv); } return (int)rv; } } } public int get_hash(params object[] key_vals) { IDictionary d = _root_map; for (int i = 0; ; i++) { object v = key_vals[i]; if (i < _n_keys - 1) { object inner_d = null; if (!d.TryGetValue(v, out inner_d)) { inner_d = _get_new_dictionary(); d.Add(v, inner_d); } d = (IDictionary)inner_d; } else { object rv; if (!d.TryGetValue(v, out rv)) { rv = _get_next_hash(); d.Add(v, rv); } return (int)rv; } } } private int _get_next_hash() { return _thread_safe ? System.Threading.Interlocked.Increment(ref _hash_counter) : ++_hash_counter; } private IDictionary _get_new_dictionary() { if (_thread_safe) return new System.Collections.Concurrent.ConcurrentDictionary(); else return new Dictionary(); } } }