public void UpdateElement() { var cache = new LFUCache(2); cache.Put(3, 1); cache.Put(2, 1); cache.Put(2, 2); cache.Put(4, 4); Assert.Equal(2, cache.Get(2)); }
public void RunProblem() { var lfu = new LFUCache(2); lfu.Put(1, 1); lfu.Put(2, 2); var v = lfu.Get(1); lfu.Put(3, 3); v = lfu.Get(2); }
public void ShouldNotEvict() { var cache = new LFUCache(2); Assert.Equal(-1, cache.Get(2)); cache.Put(2, 6); Assert.Equal(-1, cache.Get(1)); cache.Put(1, 5); cache.Put(1, 2); Assert.Equal(2, cache.Get(1)); Assert.Equal(6, cache.Get(2)); }
public void Acceptance() { var lfu = new LFUCache <int, int>(2); lfu.Put(3, 1); lfu.Put(2, 1); lfu.Put(2, 2); // replace key 2 lfu.Put(4, 4); // evicts key 3 Assert.False(lfu.TryGet(3, out var _)); Assert.Equal(4, lfu.Get(4)); Assert.Equal(2, lfu.Get(2)); }
private void LoadAudioClipPath(string path, int type, float volume, Vector3 position, float delay, float time = 0, int priority = 128, bool loop = false, float minPitch = 0, float maxPitch = 3, float maxDistance = 25, float spatialBlend = 0) { if (string.IsNullOrEmpty(path)) { return; } if (sounds[path] != null) { var audioData = sounds[path] as AudioData; if (audioData != null && audioData.audio != null) { audioData.audio.volume = _closeSound ? 0f : volume; audioData.go.transform.position = position; audioData.Play(delay); } else { sounds.RemoveKey(path); } } else { var data = new AudioData(path, (SoundType)type, loop, SoundParent.transform, position, priority, minPitch, maxPitch, volume, maxDistance, spatialBlend); string extension = Path.GetExtension(path); string fileName = path.Replace(extension, ""); extension = extension.Substring(1); ResourceManager.LoadAudioClipBundleAsync(fileName, extension) .Then((audioClip) => { if (audioClip == null) { throw new Exception(string.Format("Cant Load AudioClip! Path :{0}", path)); } if (data.audio != null) { data.audio.clip = audioClip; data.audio.volume = _closeSound ? 0f : volume; data.audio.time = time; data.Play(delay); sounds.Put(path, data); } }) .Catch(e => Debug.LogException(e)); } }
public void GivenLFUCache_Put_ShouldPutKeyValuePairIntoTheCache() { var cache = new LFUCache(2); cache.Put(1, 1); cache.Put(2, 2); Assert.IsTrue(cache.Get(1) == 1); // returns 1 cache.Put(3, 3); // evicts key 2 Assert.IsTrue(cache.Get(2) == -1); // returns -1 (not found) Assert.IsTrue(cache.Get(3) == 3); // returns 3. cache.Put(4, 4); // evicts key 1. Assert.IsTrue(cache.Get(1) == -1); // returns -1 (not found) Assert.IsTrue(cache.Get(3) == 3); // returns 3 Assert.IsTrue(cache.Get(4) == 4); // returns 4 }
public void Run() { LFUCache cache = new LFUCache(2 /* capacity (缓存容量) */); cache.Put(1, 1); cache.Put(2, 2); cache.Get(1); // 返回 1 cache.Put(3, 3); // 去除 key 2 cache.Get(2); // 返回 -1 (未找到key 2) cache.Get(3); // 返回 3 cache.Put(4, 4); // 去除 key 1 cache.Get(1); // 返回 -1 (未找到 key 1) cache.Get(3); // 返回 3 cache.Get(4); // 返回 4 }
private static void Run() { var cache = new LFUCache(2); // capacity cache.Put(1, 1); cache.Put(2, 2); cache.Get(1); // returns 1 cache.Put(3, 3); // evicts key 2 cache.Get(2); // returns -1 (not found) cache.Get(3); // returns 3. cache.Put(4, 4); // evicts key 1. cache.Get(1); // returns -1 (not found) cache.Get(3); // returns 3 cache.Get(4); // returns 4 }
public void TestCase() { var cache = new LFUCache(2); cache.Put(1, 1); cache.Put(2, 2); Assert.AreEqual(1, cache.Get(1)); cache.Put(3, 3); Assert.AreEqual(-1, cache.Get(2)); Assert.AreEqual(3, cache.Get(3)); cache.Put(4, 4); Assert.AreEqual(-1, cache.Get(1)); Assert.AreEqual(3, cache.Get(3)); Assert.AreEqual(4, cache.Get(4)); }
public static void Main() { var cache = new LFUCache(2); cache.Put(1, 1); cache.Put(2, 2); var value = cache.Get(1); cache.Put(3, 3); value = cache.Get(2); value = cache.Get(3); cache.Put(4, 4); value = cache.Get(1); value = cache.Get(3); value = cache.Get(4); }
public void GotTimeout() { //["LFUCache","put","put","put","get","put","put","get","put","put","get","put","get","get","get","put","put","get","put","get"] //[[10],[7,28],[7,1],[8,15],[6],[10,27],[8,10],[8],[6,29],[1,9],[6],[10,7],[1],[2],[13],[8,30],[1,5],[1],[13,2],[12]] var cache = new LFUCache(10); var commands = new [] { "put", "put", "put", "get", "put", "put", "get", "put", "put", "get", "put", "get", "get", "get", "put", "put", "get", "put", "get" }; var parameters = new int[, ] { { 7, 28 }, { 7, 1 }, { 8, 15 }, { 6, -1 }, { 10, 27 }, { 8, 10 }, { 8, -1 }, { 6, 29 }, { 1, 9 }, { 6, -1 }, { 10, 7 }, { 1, -1 }, { 2, -1 }, { 13, -1 }, { 8, 30 }, { 1, 5 }, { 1, -1 }, { 13, 2 }, { 12, -1 } }; for (int i = 0; i < commands.Length; i++) { var command = commands[i]; switch (command) { case "put": cache.Put(parameters[i, 0], parameters[i, 1]); break; case "get": cache.Get(parameters[i, 0]); break; } } }
public void Basic() { var cache = new LFUCache(2); cache.Put(1, 1); cache.Put(2, 2); Assert.Equal(1, cache.Get(1)); cache.Put(3, 3); // evicts key 2 Assert.Equal(-1, cache.Get(2)); // returns -1 (not found) Assert.Equal(3, cache.Get(3)); // returns 3. cache.Put(4, 4); // evicts key 1. Assert.Equal(-1, cache.Get(1)); // returns -1 (not found) Assert.Equal(3, cache.Get(3)); // returns 3 Assert.Equal(4, cache.Get(4)); // returns 4 }
public void Example3() { var lfu = new LFUCache <int, int>(0, -1); lfu.Put(0, 0); Assert.AreEqual(-1, lfu.Get(0)); }
public void Example2() { var lfu = new LFUCache <int, int>(3, -1); lfu.Put(2, 2); lfu.Put(1, 1); Assert.AreEqual(2, lfu.Get(2)); Assert.AreEqual(1, lfu.Get(1)); Assert.AreEqual(2, lfu.Get(2)); lfu.Put(3, 3); lfu.Put(4, 4); Assert.AreEqual(-1, lfu.Get(3)); Assert.AreEqual(2, lfu.Get(2)); Assert.AreEqual(1, lfu.Get(1)); Assert.AreEqual(4, lfu.Get(4)); }
/// <summary> /// 添加到镜像LFU缓存 /// </summary> /// <param name="node"></param> private void addGhostLFUCache(Node <K, V> node) { LFUCache <K, V> .LFUNode <K, V> fuNode = node as LFUCache <K, V> .LFUNode <K, V>; fuGhostCache.Put(node.Key, node.Value, fuNode.RefCount); //只会销毁缓存数据,而会保留缓存引用记录 if (DestroyCallback != null) { DestroyCallback.Invoke(node); } }
/// <summary> /// 创建面板,请求资源管理器 /// </summary> /// <param name="type"></param> public void CreatePanel(string path, int weight, LuaFunction func = null) { string name = Path.GetFileNameWithoutExtension(path); if (uiList.GetValue(path) != null) { return; } AssetWidget uiWidget = new AssetWidget(path, weight, (ao) => { var prefab = ao.GetAsset <GameObject>(); if (prefab == null) { Debug.LogError(string.Format("Panel load error: no panel founded! {0}, {1}", ao.assetPath, name)); return; } GameObject go = Instantiate(prefab) as GameObject; go.name = name; go.layer = LayerMask.NameToLayer("UI"); go.transform.SetParent(FindParent().transform, false); go.transform.localScale = Vector3.one; go.transform.localPosition = Vector3.zero; LuaBehaviour behaviour = go.transform.GetOrAddComponent <LuaBehaviour>(); behaviour.OnInit(); PanelData panelData; try { panelData = poolManager.Get <PanelData>(); } catch { panelData = new PanelData(); } panelData.Name = name; panelData.Weight = weight; panelData.PanelObject = go; panelData.behaviour = behaviour; uiList.Put(name, panelData); uiShowList.Add(name); if (func != null) { func.Call(go); } _curShowPanel = name; _curDepth++; panelData.AddOrder(_curDepth); Debug.LogWarning("CreatePanel::>> " + name + " " + prefab); }); panelLoader.LoadAsset(uiWidget); }
public V Get(K key) { //从LFU缓存中进行命中 Node <K, V> node = fuCache.GetNode(key); if (node != null) { return(node.Value); } //从LRU缓存中进行命中 node = ruCache.GetNode(key); if (node != null) { //lru缓存链中被命中,则添加到LFU中 ruCache.RemoveKey(key); fuCache.Put(key, node.Value); return(node.Value); } return(default(V)); }
public void LFUCacheTest() { // ["LFUCache","put","put","get","put","get","get","put","get","get","get"] // [[2],[1,1],[2,2],[1],[3,3],[2],[3],[4,4],[1],[3],[4]] int result; LFUCache lfuCache = new LFUCache(2); lfuCache.Put(1, 1); lfuCache.Put(2, 2); result = lfuCache.Get(1); lfuCache.Put(3, 3); result = lfuCache.Get(2); result = lfuCache.Get(3); lfuCache.Put(4, 4); result = lfuCache.Get(1); result = lfuCache.Get(3); result = lfuCache.Get(4); //result = lfuCache.Get(3); // Test case 2: // ["LFUCache","put","put","put","put","get"] //[[2],[3,1],[2,1],[2,2],[4,4],[2]] lfuCache = new LFUCache(2); lfuCache.Put(3, 1); lfuCache.Put(2, 1); lfuCache.Put(2, 2); lfuCache.Put(2, 4); result = lfuCache.Get(2); }
static void Main(string[] args) { LFUCache cache = new LFUCache(3); cache.Put(1, 1); int rs = cache.Get(1); rs = cache.Get(1); rs = cache.Get(1); Console.WriteLine($"Result: {rs} - Expect: {1}"); cache.Put(2, 2); cache.Put(3, 3); cache.Put(4, 4); rs = cache.Get(2); Console.WriteLine($"Result: {rs} - Expect: {-1}"); rs = cache.Get(4); Console.WriteLine($"Result: {rs} - Expect: {4}"); rs = cache.Get(4); rs = cache.Get(4); rs = cache.Get(3); Console.WriteLine($"Result: {rs} - Expect: {3}"); cache.Put(5, 5); rs = cache.Get(2); Console.WriteLine($"Result: {rs} - Expect: {-1}"); rs = cache.Get(5); Console.WriteLine($"Result: {rs} - Expect: {5}"); // phase 2 cache = new LFUCache(1); cache.Put(1, 1); rs = cache.Get(1); Console.WriteLine($"Result: {rs} - Expect: {1}"); cache.Put(1, 3); rs = cache.Get(1); Console.WriteLine($"Result: {rs} - Expect: {3}"); cache.Put(2, 4); rs = cache.Get(2); Console.WriteLine($"Result: {rs} - Expect: {4}"); rs = cache.Get(1); Console.WriteLine($"Result: {rs} - Expect: {-1}"); // phase 3 cache = new LFUCache(2); cache.Put(1, 1); cache.Put(2, 2); rs = cache.Get(1); Console.WriteLine($"Result: {rs} - Expect: {1}"); cache.Put(3, 3); rs = cache.Get(2); Console.WriteLine($"Result: {rs} - Expect: {-1}"); rs = cache.Get(3); Console.WriteLine($"Result: {rs} - Expect: {3}"); cache.Put(4, 4); rs = cache.Get(1); Console.WriteLine($"Result: {rs} - Expect: {-1}"); rs = cache.Get(-1); Console.WriteLine($"Result: {rs} - Expect: {-1}"); rs = cache.Get(3); Console.WriteLine($"Result: {rs} - Expect: {3}"); cache = new LFUCache(0); cache.Put(0, 0); cache.Get(0); }