/// <summary> /// FindIntersection - Finds nodes common between two binary trees. Uses Breadth-First traversal. /// </summary> /// <param name="rootOne"> Node - Root of first binary tree </param> /// <param name="rootTwo"> Node - Root of second binary tree </param> /// <returns> integer array conataining the values of all common nodes </returns> public static int[] FindIntersection(Node rootOne, Node rootTwo) { Hashset hs = new Hashset(); List <int> ans = new List <int>(); Queue <Node> breadth = new Queue <Node>(); breadth.Enqueue(rootOne); breadth.Enqueue(rootTwo); while (breadth.TryPeek(out rootOne)) { Node front = breadth.Dequeue(); if (!hs.Add(front)) { ans.Add(front.Value); } if (front.LeftChild != null) { breadth.Enqueue(front.LeftChild); } if (front.RightChild != null) { breadth.Enqueue(front.RightChild); } } return(ans.ToArray()); }
private Hashset Eclosure(TransitionState stateStart) { Hashset setProcessed = new Hashset(); Hashset setUnprocessed = new Hashset(); setUnprocessed.AddElement(stateStart); while (setUnprocessed.Count > 0) { TransitionState state = (TransitionState)setUnprocessed[0]; TransitionState[] arrTrans = state.GetTransitions(MetaSymbol.EPSILON); setProcessed.AddElement(state); setUnprocessed.RemoveElement(state); if (arrTrans != null) { foreach (TransitionState stateEpsilon in arrTrans) { if (!setProcessed.ElementExist(stateEpsilon)) { setUnprocessed.AddElement(stateEpsilon); } } } } return(setProcessed); }
/// <exclude /> public static string EncodeUrlInvalidCharacters(string value) { const char separator = '|'; const char spaceReplacement = '-'; var symbolsToEncode = new Hashset <char>(new[] { '<', '>', '*', '%', '&', '\\', '?', '/' }); symbolsToEncode.Add(separator); symbolsToEncode.Add(spaceReplacement); var sb = new StringBuilder(value.Length); foreach (var ch in value) { if (!symbolsToEncode.Contains(ch)) { sb.Append(ch); continue; } int code = (int)ch; Verify.That(code <= 256, "1 byte ASCII code expected"); sb.Append(separator).Append(code.ToString("X2")); } return(sb.Replace(' ', spaceReplacement).ToString()); }
static void GetAllStateAndInput(TransitionState stateStart, Hashset setAllState, Hashset setInputSymbols) { Hashset setUnprocessed = new Hashset(); setUnprocessed.AddElement(stateStart); while (setUnprocessed.Count > 0) { TransitionState state = (TransitionState)setUnprocessed[0]; setAllState.AddElement(state); setUnprocessed.RemoveElement(state); foreach (object objToken in state.GetAllKeys()) { string sSymbol = (string)objToken; setInputSymbols.AddElement(sSymbol); TransitionState[] arrTrans = state.GetTransitions(sSymbol); if (arrTrans != null) { foreach (TransitionState stateEpsilon in arrTrans) { if (!setAllState.ElementExist(stateEpsilon)) { setUnprocessed.AddElement(stateEpsilon); } } } } } }
public void Method1() { var names = new HashSet <string>(); names.Add("abhi"); names.Add("swathi"); names.Add("nish"); names.Add("ashu"); names.Remove("nish"); //creating another hash set Hashset <string> names2 = new Hashset <string>(); names2.Add("abc"); names2.Add("swathi"); names2.Add("ashu"); names2.Add("def"); names2.Add("ghi"); names1.UnionWith(names2); //names1.IntersectWith(names2); // names1.ExceptWith(names2); foreach (var name in names) { Console.WriteLine(name); } }
static int GetSerializedFsa(TransitionState stateStart, StringBuilder sb) { Hashset setAllState = new Hashset(); Hashset setAllInput = new Hashset(); GetAllStateAndInput(stateStart, setAllState, setAllInput); return(GetSerializedFsa(stateStart, setAllState, setAllInput, sb)); }
protected ConcurrentHashSet(SerializationInfo info, StreamingContext context) { hashset = new Hashset <T>(); // not sure about this one really... var iSerializable = hashset as ISerializable; iSerializable.GetObjectData(info, context); }
public void Test_Clear_ShouldEmptyAndSetCountToZero() { data = new Hashset<int>(4); data.Add(key1); data.Clear(); var expectedCount = 0; var actualCount = data.Count; Assert.AreEqual(expectedCount, actualCount, "Hashset should increase capacity when range is met!"); }
public void Test_Fine_ShouldKeyWhenContained() { data = new Hashset<int>(4); data.Add(key1); data.Add(key2); data.Add(key3); var keyFound = data.Find(key2); Assert.AreEqual(key2, keyFound, "Hashset Find should return key."); }
public void Test_Containes_ShouldReturnTrueWhenContained() { data = new Hashset<int>(4); data.Add(key1); data.Add(key2); data.Add(key3); var isContained = data.Containes(key2); Assert.IsTrue(isContained, "Hashset Containes should true when contained."); }
public void Test_Fine_ShouldKeyWhenContained() { data = new Hashset <int>(4); data.Add(key1); data.Add(key2); data.Add(key3); var keyFound = data.Find(key2); Assert.AreEqual(key2, keyFound, "Hashset Find should return key."); }
public void Test_Clear_ShouldEmptyAndSetCountToZero() { data = new Hashset <int>(4); data.Add(key1); data.Clear(); var expectedCount = 0; var actualCount = data.Count; Assert.AreEqual(expectedCount, actualCount, "Hashset should increase capacity when range is met!"); }
public void Test_Containes_ShouldReturnTrueWhenContained() { data = new Hashset <int>(4); data.Add(key1); data.Add(key2); data.Add(key3); var isContained = data.Containes(key2); Assert.IsTrue(isContained, "Hashset Containes should true when contained."); }
public void Test_Add_ShouldIncreaseCapacityWhenRangeIsMet() { data = new Hashset<int>(4); data.Add(key1); data.Add(key2); data.Add(key3); data.Add(key4); var expectedCapacity = 8; var actualCapacity = data.Capacity; Assert.AreEqual(expectedCapacity, actualCapacity, "Hashset should increase capacity when range is met!"); }
public void Test_Remove_ShouldRemoveAndDecreaseCount() { data = new Hashset <int>(4); data.Add(key1); data.Add(key2); data.Add(key3); data.Remove(key2); var expectedCount = 2; var actualCount = data.Count; Assert.AreEqual(expectedCount, actualCount, "Hashset should decrease count when removed!"); }
public void Test_Add_ShouldIncreaseCapacityWhenRangeIsMet() { data = new Hashset <int>(4); data.Add(key1); data.Add(key2); data.Add(key3); data.Add(key4); var expectedCapacity = 8; var actualCapacity = data.Capacity; Assert.AreEqual(expectedCapacity, actualCapacity, "Hashset should increase capacity when range is met!"); }
private Hashset Move(TransitionState state, string sInputSymbol) { var set = new Hashset(); var arrTrans = state.GetTransitions(sInputSymbol); if (arrTrans != null) { set.AddElementRange(arrTrans); } return(set); }
private Hashset Move(Hashset setState, string sInputSymbol) { var set = new Hashset(); TransitionState state; foreach (object obj in setState) { state = obj as TransitionState; Hashset setMove = Move(state, sInputSymbol); set.Union(setMove); } return(set); }
private Hashset Eclosure(Hashset setState) { var setAllEclosure = new Hashset(); TransitionState state; foreach (object obj in setState) { state = obj as TransitionState; var setEclosure = Eclosure(state); setAllEclosure.Union(setEclosure); } return(setAllEclosure); }
/// <exclude /> public static void RegisterUsage(string encryptedValue) { string value; DateTime dateTime; Decrypt(encryptedValue, out dateTime, out value); int minute = GetMinute(dateTime); Hashset <string> usedRecords = _alreadyUsedRecords[minute]; if (usedRecords == null) { lock (_alreadyUsedRecords) { usedRecords = _alreadyUsedRecords[minute]; if (usedRecords == null) { usedRecords = new Hashset <string>(); _alreadyUsedRecords.Add(minute, usedRecords); } } } lock (usedRecords) { if (!usedRecords.Contains(encryptedValue)) { usedRecords.Add(encryptedValue); } } // Periodic clean-up _counter++; if (_counter % 1000 == 0) { lock (_alreadyUsedRecords) { int currentMinute = GetMinute(DateTime.Now); ICollection <int> keys = _alreadyUsedRecords.GetKeys(); foreach (int key in keys) { if (key < currentMinute - CaptchaExpiration || key > currentMinute + 10) { _alreadyUsedRecords.Remove(key); } } } } }
private Hashset FindGroup(ArrayList arrGroup, TransitionState state) { foreach (object objSet in arrGroup) { Hashset set = objSet as Hashset; if (set.ElementExist(state) == true) { return(set); } } return(null); }
public static string RepeatedWord(string input) { string[] allwords = input.ToLower().Split(" "); Hashset wordSet = new Hashset(); foreach (string word in allwords) { if (!wordSet.Add(word)) { return(word); } } return("no duplicates"); }
private bool IsAcceptingGroup(Hashset setGroup) { TransitionState state; foreach (object objState in setGroup) { state = (TransitionState)objState; if (state.AcceptingState == true) { return(true); } } return(false); }
private string SetToString(Hashset set) { string s = ""; foreach (object objState in set) { TransitionState state = (TransitionState)objState; s += state.Id.ToString() + ", "; } s = s.TrimEnd(new char[] { ' ', ',' }); if (s.Length == 0) { s = "Empty"; } return("{" + s + "}"); }
public void Test_Intersect_ShouldAddOnlyKeysContainedInBothSets() { data = new Hashset <int>(4); data.Add(key1); data.Add(key2); var secondHashSet = new Hashset <int>(); secondHashSet.Add(key2); secondHashSet.Add(key3); var intersectSet = data.Intersect(secondHashSet); var expectedCount = 1; var actualCount = intersectSet.Count; Assert.IsTrue( expectedCount == actualCount && intersectSet.Containes(key2), "Hashset intersect should add only keys contained in both hasjsets."); }
public static IEnumerable <string> ReplaceFirstOccurrencesWithEmpty(this IEnumerable <string> @this, IEnumerable <string> a) { // prepare a HashSet<string> to know how many A elements there still exist var set = new Hashset <string>(a); // iterate and apply the rule you asked about // virtually forever (if needed) foreach (var value in @this) { if (set.Remove(value)) { yield return(""); } else { yield return(value); } } }
private static void SubscribeToTransactionRollbackEvent() { var transaction = Transaction.Current; if (transaction == null) { return; } var currentThreadData = ThreadDataManager.GetCurrentNotNull(); Hashset <string> transactions; const string tlsKey = "XmlDataProvider enlisted transactions"; if (!currentThreadData.HasValue(tlsKey)) { transactions = new Hashset <string>(); currentThreadData.SetValue(tlsKey, transactions); } else { transactions = (Hashset <string>)currentThreadData[tlsKey]; } string transactionId = transaction.TransactionInformation.LocalIdentifier; if (transactions.Contains(transactionId)) { return; } transactions.Add(transactionId); ThreadStart logging = () => { var exception = new TransactionException("XML data provider does not support transaction's API, changes were not rolled back."); Log.LogWarning(LogTitle, exception); }; transaction.EnlistVolatile(new TransactionRollbackHandler(logging), EnlistmentOptions.None); }
public void Test_Union_ShouldHaveUniqueKeys() { data = new Hashset <int>(4); data.Add(key1); data.Add(key2); var secondHashSet = new Hashset <int>(); secondHashSet.Add(key2); secondHashSet.Add(key3); var unionSet = data.Union(secondHashSet); var expectedCount = 3; var actualCount = unionSet.Count; Assert.IsTrue( expectedCount == actualCount && unionSet.Containes(key1) && unionSet.Containes(key2) && unionSet.Containes(key3), "Hashset union should have uniqie keys and correct count."); }
public void CopyTo(ArraySlice <T> array, int arrayIndex, int count) { _lock.EnterWriteLock(); _lock.EnterReadLock(); try { //CopyTo<T>(Hashset<T> src, ArraySlice<T> array, int arrayIndex, int count) Hashset <T> .CopyTo(hashset, array, arrayIndex, count); } finally { if (_lock.IsWriteLockHeld) { _lock.ExitWriteLock(); } if (_lock.IsReadLockHeld) { _lock.ExitReadLock(); } } }
public void Test_Intersect_ShouldAddOnlyKeysContainedInBothSets() { data = new Hashset<int>(4); data.Add(key1); data.Add(key2); var secondHashSet = new Hashset<int>(); secondHashSet.Add(key2); secondHashSet.Add(key3); var intersectSet = data.Intersect(secondHashSet); var expectedCount = 1; var actualCount = intersectSet.Count; Assert.IsTrue( expectedCount == actualCount && intersectSet.Containes(key2), "Hashset intersect should add only keys contained in both hasjsets."); }
/// <exclude /> public static void RegisterUsage(string encryptedValue) { string value; DateTime dateTime; Decrypt(encryptedValue, out dateTime, out value); int minute = GetMinute(dateTime); Hashset<string> usedRecords = _alreadyUsedRecords[minute]; if (usedRecords == null) { lock(_alreadyUsedRecords) { usedRecords = _alreadyUsedRecords[minute]; if (usedRecords == null) { usedRecords = new Hashset<string>(); _alreadyUsedRecords.Add(minute, usedRecords); } } } lock(usedRecords) { if(!usedRecords.Contains(encryptedValue)) { usedRecords.Add(encryptedValue); } } // Periodic clean-up _counter++; if(_counter % 1000 == 0) { lock(_alreadyUsedRecords) { int currentMinute = GetMinute(DateTime.Now); ICollection<int> keys = _alreadyUsedRecords.GetKeys(); foreach(int key in keys) { if (key < currentMinute - CaptchaExpiration || key > currentMinute + 10) { _alreadyUsedRecords.Remove(key); } } } } }
/// <exclude /> public static string EncodeUrlInvalidCharacters(string value) { const char separator = '|'; const char spaceReplacement = '-'; var symbolsToEncode = new Hashset<char>(new[] { '<', '>', '*', '%', '&', '\\', '?' }); symbolsToEncode.Add(separator); symbolsToEncode.Add(spaceReplacement); var sb = new StringBuilder(value.Length); foreach (var ch in value) { if (!symbolsToEncode.Contains(ch)) { sb.Append(ch); continue; } int code = (int)ch; Verify.That(code <= 256, "1 byte ASCII code expected"); sb.Append(separator).Append(code.ToString("X2")); } return sb.Replace(' ', spaceReplacement).ToString(); }
public void TestInit() { data = new Hashset <int>(); }
private static void OnFlushEvent(FlushEventArgs args) { Interlocked.Increment(ref _flushCounter); _subscribedTo = new Hashset<Type>(); }
private static void OnFlushEvent(FlushEventArgs args) { Interlocked.Increment(ref _flushCounter); _subscribedTo = new Hashset <Type>(); }
static int GetSerializedFsa(TransitionState stateStart, Hashset setAllState, Hashset setAllSymbols, StringBuilder sb) { int lineLength = 0; int minWidth = 6; string line = String.Empty; string format = String.Empty; setAllSymbols.RemoveElement(MetaSymbol.EPSILON); setAllSymbols.AddElement(MetaSymbol.EPSILON); object[] symbolObjects = new object[setAllSymbols.Count + 1]; symbolObjects[0] = "State"; format = "{0,-8}"; for (int i = 0; i < setAllSymbols.Count; i++) { string sSymbol = setAllSymbols[i].ToString(); symbolObjects[i + 1] = sSymbol; format += " | "; format += "{" + (i + 1).ToString() + ",-" + Math.Max(Math.Max(sSymbol.Length, minWidth), sSymbol.ToString().Length) + "}"; } line = String.Format(format, symbolObjects); lineLength = Math.Max(lineLength, line.Length); sb.AppendLine(("").PadRight(lineLength, '-')); sb.AppendLine(line); sb.AppendLine(("").PadRight(lineLength, '-')); int nTransCount = 0; foreach (object objState in setAllState) { TransitionState state = (TransitionState)objState; symbolObjects[0] = (state.Equals(stateStart) ? ">" + state.ToString() : state.ToString()); for (int i = 0; i < setAllSymbols.Count; i++) { string sSymbol = setAllSymbols[i].ToString(); TransitionState[] arrStateTo = state.GetTransitions(sSymbol); string sTo = String.Empty; if (arrStateTo != null) { nTransCount += arrStateTo.Length; sTo = arrStateTo[0].ToString(); for (int j = 1; j < arrStateTo.Length; j++) { sTo += ", " + arrStateTo[j].ToString(); } } else { sTo = "--"; } symbolObjects[i + 1] = sTo; } line = String.Format(format, symbolObjects); sb.AppendLine(line); lineLength = Math.Max(lineLength, line.Length); } format = "State Count: {0}, Input Symbol Count: {1}, Transition Count: {2}"; line = String.Format(format, setAllState.Count, setAllSymbols.Count, nTransCount); lineLength = Math.Max(lineLength, line.Length); sb.AppendLine(("").PadRight(lineLength, '-')); sb.AppendLine(line); lineLength = Math.Max(lineLength, line.Length); setAllSymbols.RemoveElement(MetaSymbol.EPSILON); return(lineLength); }
public void Test_Remove_ShouldRemoveAndDecreaseCount() { data = new Hashset<int>(4); data.Add(key1); data.Add(key2); data.Add(key3); data.Remove(key2); var expectedCount = 2; var actualCount = data.Count; Assert.AreEqual(expectedCount, actualCount, "Hashset should decrease count when removed!"); }
public void TestInit() { data = new Hashset<int>(); }
public void Test_Union_ShouldHaveUniqueKeys() { data = new Hashset<int>(4); data.Add(key1); data.Add(key2); var secondHashSet = new Hashset<int>(); secondHashSet.Add(key2); secondHashSet.Add(key3); var unionSet = data.Union(secondHashSet); var expectedCount = 3; var actualCount = unionSet.Count; Assert.IsTrue( expectedCount == actualCount && unionSet.Containes(key1) && unionSet.Containes(key2) && unionSet.Containes(key3), "Hashset union should have uniqie keys and correct count."); }
public static void Main(string[] args) { LinkedList<KeyValuePair<string, int>> list = new LinkedList<KeyValuePair<string, int>>(); Person person1 = new Person("Pesho", "Peshov", 21); Person person2 = new Person("Gosho", "Peshov", 22); Person person3 = new Person("Tosho", "Peshov", 23); // Console.WriteLine(person1.GetHashCode()); // Console.WriteLine(person2.GetHashCode()); // Console.WriteLine(person3.GetHashCode()); // Console.WriteLine(person1.CompareTo(person2)); // Console.WriteLine(person1.CompareTo(person3)); var hashTable = new HashTable<Person, int>(); hashTable.Add(person1, 1); hashTable.Add(person2, 1); hashTable.Add(person3, 2); foreach (KeyValuePair<Person, int> person in hashTable) { Console.WriteLine("Key\n{0}Value: {1}", person.Key, person.Value); Console.WriteLine(); } Console.WriteLine("=====FIND====="); Console.WriteLine(hashTable.Find(person1)); Console.WriteLine("=====REMOVE====="); Console.WriteLine(hashTable.Remove(person1)); Console.WriteLine(hashTable.Remove(person1)); Console.WriteLine("=====FIND====="); Console.WriteLine(hashTable.Find(person1)); var keys = hashTable.Keys(); foreach (var item in keys) { Console.WriteLine(item); } Console.WriteLine("=====INDEXER====="); Person person4 = new Person("Rosho", "Peshov", 55); // set hashTable[person4] = 777; // get Console.WriteLine(hashTable[person4]); Console.WriteLine("=====CLEAR====="); hashTable.Clear(); Person person5 = new Person("Josh", "Goshov", 33); hashTable.Add(person5, 1234); Person person6 = new Person("Losh", "Goshov", 66); hashTable.Add(person6, 126); foreach (KeyValuePair<Person, int> person in hashTable) { Console.WriteLine("Key\n{0}Value: {1}", person.Key, person.Value); Console.WriteLine(); } Console.WriteLine(hashTable.ContainesKey(person6)); Console.WriteLine("================================================="); Console.WriteLine("=====HashSet====="); Hashset<Person> hashSet = new Hashset<Person>(); hashSet.Add(person1); hashSet.Add(person2); Console.WriteLine(hashSet.Find(person1)); Console.WriteLine(hashSet.Remove(person1)); Console.WriteLine(hashSet.Find(person1)); Console.WriteLine(hashSet.Find(person2)); Console.WriteLine(hashSet.Remove(person2)); Console.WriteLine("=====FOREACH====="); hashSet.Clear(); hashSet.Add(person1); hashSet.Add(person2); foreach (var item in hashSet) { Console.WriteLine(item); } Console.WriteLine("=====UNION====="); var secondHashSet = new Hashset<Person>(); secondHashSet.Add(person2); secondHashSet.Add(person3); var unionHashset = hashSet.Union(secondHashSet); foreach (var item in unionHashset) { Console.WriteLine(item); } Console.WriteLine("=====INTERSECT ===="); var intersectionHashset = hashSet.Intersect(secondHashSet); foreach (var item in intersectionHashset) { Console.WriteLine(item); } }
private TransitionState ReduceDfa(TransitionState stateStartDfa) { Hashset setInputSymbol = new Hashset(); Hashset setAllDfaState = new Hashset(); GetAllStateAndInput(stateStartDfa, setAllDfaState, setInputSymbol); TransitionState stateStartReducedDfa = null; ArrayList arrGroup = null; arrGroup = PartitionDfaGroups(setAllDfaState, setInputSymbol); foreach (object objGroup in arrGroup) { Hashset setGroup = (Hashset)objGroup; bool bAcceptingGroup = IsAcceptingGroup(setGroup); bool bStartingGroup = setGroup.ElementExist(stateStartDfa); TransitionState stateRepresentative = (TransitionState)setGroup[0]; if (bStartingGroup == true) { stateStartReducedDfa = stateRepresentative; } if (bAcceptingGroup == true) { stateRepresentative.AcceptingState = true; } if (setGroup.GetCardinality() == 1) { continue; } setGroup.RemoveElement(stateRepresentative); TransitionState stateToBeReplaced = null; int nReplecementCount = 0; foreach (object objStateToReplaced in setGroup) { stateToBeReplaced = (TransitionState)objStateToReplaced; setAllDfaState.RemoveElement(stateToBeReplaced); foreach (object objState in setAllDfaState) { TransitionState state = (TransitionState)objState; nReplecementCount += state.ReplaceTransitionState(stateToBeReplaced, stateRepresentative); } } } int nIndex = 0; while (nIndex < setAllDfaState.Count) { TransitionState state = (TransitionState)setAllDfaState[nIndex]; if (state.IsDeadState()) { setAllDfaState.RemoveAt(nIndex); continue; } nIndex++; } return(stateStartReducedDfa); }
private ArrayList PartitionDfaGroups(Hashset setMasterDfa, Hashset setInputSymbol) { ArrayList arrGroup = new ArrayList(); HashMap hashMap = new HashMap(); Hashset emptySet = new Hashset(); Hashset acceptingSet = new Hashset(); Hashset nonAcceptingSet = new Hashset(); foreach (object objState in setMasterDfa) { TransitionState state = (TransitionState)objState; if (state.AcceptingState == true) { acceptingSet.AddElement(state); } else { nonAcceptingSet.AddElement(state); } } if (nonAcceptingSet.GetCardinality() > 0) { arrGroup.Add(nonAcceptingSet); } arrGroup.Add(acceptingSet); IEnumerator iterInput = setInputSymbol.GetEnumerator(); iterInput.Reset(); while (iterInput.MoveNext()) { string sInputSymbol = iterInput.Current.ToString(); int nPartionIndex = 0; while (nPartionIndex < arrGroup.Count) { Hashset setToBePartitioned = (Hashset)arrGroup[nPartionIndex]; nPartionIndex++; if (setToBePartitioned.IsEmpty() || setToBePartitioned.GetCardinality() == 1) { continue; } foreach (object objState in setToBePartitioned) { TransitionState state = (TransitionState)objState; TransitionState[] arrState = state.GetTransitions(sInputSymbol.ToString()); if (arrState != null) { Debug.Assert(arrState.Length == 1); TransitionState stateTransionTo = arrState[0]; Hashset setFound = FindGroup(arrGroup, stateTransionTo); hashMap.Add(setFound, state); } else { hashMap.Add(emptySet, state); } } if (hashMap.Count > 1) { arrGroup.Remove(setToBePartitioned); foreach (DictionaryEntry de in hashMap) { Hashset setValue = (Hashset)de.Value; arrGroup.Add(setValue); } nPartionIndex = 0; iterInput.Reset(); } hashMap.Clear(); } } return(arrGroup); }