private static void Initialize() { var init = new ResourceTestSystemInitiation(); var res = init.CreateResources(ResourceSpaceDescriptions.Count); ResourceSpaceDescriptions.ResourceAssembly = res; AppDomain.CurrentDomain.AssemblyResolve += (e, a) => { if (a.Name.Contains("TestResources")) return res; throw new Exception(); }; var store = new Store(); _upnode1 = new CacheNode("Client1",res,store); _upnode2 = new CacheNode("Client2",res,store); _upnode3 = new CacheNode("Client3", res, store); _upnode4 = new CacheNode("Client4", res, store); _centnode = new CacheNode("Central", res, store); _upnode1.ConnectToDownNode(_centnode,TimeSpan.FromMilliseconds(30)); _upnode2.ConnectToDownNode(_centnode, TimeSpan.FromMilliseconds(30)); _upnode3.ConnectToDownNode(_centnode, TimeSpan.FromMilliseconds(30)); _upnode4.ConnectToDownNode(_centnode, TimeSpan.FromMilliseconds(30)); _upnode1.Start(); _upnode2.Start(); _upnode3.Start(); _upnode4.Start(); _centnode.Start(); }

public void Add2Test() { var node = new CacheNode<int, int>(10, 10, 0, Comparer<int>.Default); int depth = 0; node.AddNode(9, 9, 0, 0); Assert.AreEqual(9, node.FindNode(9, 0, out depth)); Assert.AreEqual(depth, 1); }

public void DumbTest() { var node = new CacheNode<int, int>(10, 10,0, Comparer<int>.Default); int depth = 0; Assert.AreEqual(10, node.FindNode(10, 0, out depth)); Assert.AreEqual(0, depth); Assert.AreEqual(0, node.FindNode(9, 0, out depth)); Assert.AreEqual(0, node.FindNode(11, 0, out depth)); }

private CacheNode<int, double> BuildAndBalanceTree(int[] frequences, out float expectedFrequency, int maxFixedBranchDepth = 0) { var root = BuildTree(frequences, maxFixedBranchDepth); var balancer = new CacheNode<int, double>.HeavyRebalancer(root, frequences.Length, 0, 1); var watch = Stopwatch.StartNew(); expectedFrequency = balancer.Rebalance(); watch.Stop(); Debug.Print("{0} balancing was", watch.Elapsed); root = balancer.ConstructNewTreeAfterCalculation(); return root; }

private static void Administration(CacheNode node,int amount) { for (var i = 0; i < amount; i++) { node.PostResourceFromScratch(); node.PostResourceFromScratch("Client1"); node.PostResourceFromScratch("Client2"); node.PostResourceFromScratch("Client3"); node.PostResourceFromScratch("Client4"); node.QueryResourceByOtherResource(); node.GetQueriedResource(); node.PostResourceFromScratch(); } node.ClearMemory(0); }

private CacheNode<int, double> BuildTree(int[] frequences, int maxFixedBranchDepth = 0) { var freqSum = frequences.Sum(); var root = new CacheNode<int, double>(0, (double)frequences[0] / freqSum, maxFixedBranchDepth, Comparer<int>.Default, probabilityCalc: k => (float)k); var frs = Enumerable.Range(0, frequences.Length).ToList(); var rnd = new Random(); for (int i = 1; i < frequences.Length; i++) { var t = rnd.Next(frs.Count - 1) + 1; var key = frs[t]; root.AddNode(key, (double)frequences[key] / freqSum, maxFixedBranchDepth, 0); frs.RemoveAt(t); } return root; }

private static void Work(CacheNode node,string rootResName) { node.QueryResourceByNode(rootResName); node.GetAllQueriedResource(); node.GetResource(); node.PostResourceFromMemory(); node.QueryResourceByNodeAndOtherResource(); node.GetAllQueriedResource(); for (int i = 0; i < 20;i++ ) node.PostResourceFromMemory(node.Name); node.QueryResourceByOtherResource(); node.QueryResourceByOtherResource(); node.QueryResourceByOtherResource(); node.GetAllQueriedResource(); node.PostResourceFromScratch(); node.GetResource(); node.ClearMemory(300); }

public void Set(string key, string value) { if ((!_memCache.ContainsKey(key)) && (_memCache.Count == _maxNumElements)) { Evict(); } PQNode pNode = null; if (!_memCache.ContainsKey(key)) { pNode = _priorityQueue.Insert(key); } else { pNode = _memCache[key].PriorityNode; _priorityQueue.IncreasePriority(pNode); } _memCache[key] = new CacheNode { Value = value, PriorityNode = pNode }; }

public void AddWithCollectingTest() { var node = new CacheNode<int, int>(10, 10, 0, Comparer<int>.Default); node.AddNode(11, 11, 0, 0); node.AddNode(9, 9, 0, 0); int depth; Assert.AreEqual(9, node.FindNode(9, 0, out depth)); Assert.AreEqual(1, depth);; Assert.AreEqual(11, node.FindNode(11, 0, out depth)); Assert.AreEqual(1, depth); GC.Collect(2,GCCollectionMode.Forced); GC.Collect(2, GCCollectionMode.Forced); GC.Collect(2, GCCollectionMode.Forced); GC.WaitForPendingFinalizers(); Assert.AreEqual(0, node.FindNode(9, 0, out depth)); Assert.AreEqual(0, node.FindNode(11, 0, out depth)); }

private static void MaintanceWork(CacheNode node) { node.QueryAllOfSomeResource(); node.GetAllQueriedResource(); node.GetResource(); node.PostResourceFromMemory(); node.QueryResourceByOtherResource(); node.GetAllQueriedResource(); node.PostResourceFromMemory(); node.PostResourceFromMemory(); node.PostResourceFromMemory(); node.PostResourceFromMemory(); node.PostResourceFromScratch(); node.GetResource(); node.QueryResourceByOtherResource(); node.GetAllQueriedResource(); node.PostResourceFromMemory(); node.PostResourceFromMemory(); node.ClearMemory(500); }

public override void Push(int item) { this.stack.Push(item); if(!this.cache.IsEmpty) { CacheNode node = cache.Peek(); if (node.Value == item) { node.Count++; return; } else if (node.Value > item) return; } CacheNode newNode = new CacheNode { Count = 1, Value = item }; this.cache.Push(newNode); }

public override void Push(int item) { CacheNode node = new CacheNode { Value = item }; if (this.stack.IsEmpty) node.Max = item; else { CacheNode tail = this.stack.Peek(); node.Max = Math.Max(item, tail.Max); } stack.Push(node); }

/// <summary> Adds an entry to the table.</summary> public virtual void add(DNSEntry entry) { lock (this) { //logger.log("DNSCache.add("+entry.getName()+")"); CacheNode newValue = new CacheNode(entry); CacheNode node = (CacheNode) hashtable[entry.Name]; if (node == null) { hashtable[entry.Name] = newValue; } else { newValue.Next = node.Next; node.Next = newValue; } size++; } }

public void DeepAddWithCollectingWithNonZeroFixedBranchLengthTest() { var node = new CacheNode<int, int>(10, 10,1, Comparer<int>.Default); #region Prepare node.AddNode(8, 8, 1, 0); node.AddNode(7, 7, 1, 0); node.AddNode(9, 9, 1, 0); node.AddNode(12, 12, 1, 0); node.AddNode(11, 11, 1, 0); node.AddNode(13, 13, 1, 0); int depth = 0; Assert.AreEqual(7, node.FindNode(7, 0, out depth)); Assert.AreEqual(2, depth); depth = 0; Assert.AreEqual(8, node.FindNode(8, 0, out depth)); Assert.AreEqual(1, depth); depth = 0; Assert.AreEqual(9, node.FindNode(9, 0, out depth)); Assert.AreEqual(2, depth); depth = 0; Assert.AreEqual(10, node.FindNode(10, 0, out depth)); Assert.AreEqual(0, depth); depth = 0; Assert.AreEqual(11, node.FindNode(11, 0, out depth)); Assert.AreEqual(2, depth); depth = 0; Assert.AreEqual(12, node.FindNode(12, 0, out depth)); Assert.AreEqual(1, depth); depth = 0; Assert.AreEqual(13, node.FindNode(13, 0, out depth)); Assert.AreEqual(2, depth); depth = 0; #endregion #region First collection GC.Collect(2, GCCollectionMode.Forced); GC.WaitForPendingFinalizers(); Assert.AreEqual(0, node.FindNode(7, 0, out depth)); depth = 0; Assert.AreEqual(8, node.FindNode(8, 0, out depth)); Assert.AreEqual(1, depth); depth = 0; Assert.AreEqual(0, node.FindNode(9, 0, out depth)); depth = 0; Assert.AreEqual(10, node.FindNode(10, 0, out depth)); Assert.AreEqual(0, depth); depth = 0; Assert.AreEqual(0, node.FindNode(11, 0, out depth)); depth = 0; Assert.AreEqual(12, node.FindNode(12, 0, out depth)); Assert.AreEqual(1, depth); depth = 0; Assert.AreEqual(0, node.FindNode(13, 0, out depth)); #endregion GC.Collect(2, GCCollectionMode.Forced); GC.WaitForPendingFinalizers(); Assert.AreEqual(0, node.FindNode(7, 0, out depth)); Assert.AreEqual(8, node.FindNode(8, 0, out depth)); Assert.AreEqual(0, node.FindNode(9, 0, out depth)); depth = 0; Assert.AreEqual(10, node.FindNode(10, 0, out depth)); Assert.AreEqual(0, depth); Assert.AreEqual(0, node.FindNode(11, 0, out depth)); Assert.AreEqual(12, node.FindNode(12, 0, out depth)); Assert.AreEqual(0, node.FindNode(13, 0, out depth)); }

private void Add(CacheNode entry) { // Avoid name collisions by incrementing suffix while (Nodes.Contains(entry.Name)) entry.SuffixCounter++; Nodes.Add(entry); }

public BigInteger Pow(BigInteger[] exponents) { if (bases.Length != exponents.Length) throw new ArithmeticException("Same number of bases and exponents expected"); int[][] exps; int maxExpLen; ExtractAligned(exponents, out exps, out maxExpLen); var accum = new int[modulusMagnitude.Length + 1]; var a = new int[modulusMagnitude.Length]; var gi = new int[modulusMagnitude.Length]; bool foundFirst = false; for (int i = 0; i < maxExpLen; ++i) { for (int bit = 31; bit >= 0; --bit) { bool nonZero = false; var mask = 1 << bit; var node = rootNode; for(int e = 0; e < exponents.Length; ++e) if ((exps[e][i] & mask) != 0) { CacheNode next; if (!node.next.TryGetValue(e, out next)) { next = new CacheNode { length = node.length + 1 }; next.number = (int[])node.number.Clone(); MultiplyMonty(accum, next.number, bases[e]); node.next[e] = next; } node = next; if (node.length == MaxChainLength) { if (nonZero) MultiplyMonty(accum, gi, node.number); else { Buffer.BlockCopy(node.number, 0, gi, 0, modulusMagnitude.Length * 4); nonZero = true; } node = rootNode; } } if(node != rootNode) if (nonZero) MultiplyMonty(accum, gi, node.number); else { Buffer.BlockCopy(node.number, 0, gi, 0, modulusMagnitude.Length * 4); nonZero = true; } if (foundFirst) MultiplyMonty(accum, a, a); if (nonZero) { if (!foundFirst) { Buffer.BlockCopy(gi, 0, a, 0, modulusMagnitude.Length * 4); foundFirst = true; } else MultiplyMonty(accum, a, gi); } } } Array.Clear(gi, 0, gi.Length); gi[gi.Length - 1] = 1; MultiplyMonty(accum, a, gi); BigInteger result = FromData(a); return result; }