-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLRUCacheMissCount.cs
129 lines (111 loc) · 3.07 KB
/
LRUCacheMissCount.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LRUCacheMissCount
{
/*
* code reference:
* http://blog.csdn.net/lycorislqy/article/details/49218977
* problem statement:
*
*
*
* February 2, 2016
* Java HashMap C# analog: Dictionary
* http://stackoverflow.com/questions/687942/java-map-equivalent-in-c-sharp
*
* Java List interface - C# analog
* http://stackoverflow.com/questions/10115769/something-similar-to-c-sharp-net-generic-list-in-java
*
* First use - Dictionary(int capacity) - Good practice!
*/
public class LRUcache
{
static IDictionary<int, int> map;
static LinkedList<int> list;
int capacity;
/*
* C# double linked list - no capacity constructor
*/
public LRUcache(int capacity_val)
{
capacity = capacity_val;
map = new Dictionary<int, int>(capacity);
list = new LinkedList<int>();
}
private static void outputlist()
{
string s = "[ ";
System.Console.Write("[ ");
for(int j=0; j < list.Count;j++)
s += list.ElementAt(j)+" ";
s +="]\n";
System.Console.Write(s);
}
/*
* Java HashMap.Contains()
* C# Dictionary.ContainsKey() - more expressive
*
* Java double linked list
* C# analog:
* LinkedList
*/
private int set(int key, int i2)
{
if (map.Count < capacity)
{
if (map.ContainsKey(key))
{
map[key] = i2;
list.Remove(key);
// list.AddBefore(key); ?
}
else
{/*
map[key] = i2;
list.Add(key);
*/
}
}
else
{ /*
int lastkey = list[0];
list.remove(0);
map.remove(lastkey);
list.add(key);
map.put(key, i2);
*/
}
return key;
}
}
class Solution
{
static void Main(string[] args)
{
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array = {4,3,4,2,3,1,4,2};
int max = 3;
LRUcache MemoA = new LRUcache(max);
for(int i = 0; i < array.Length; i++)
{
//MemoA.set(array[i], i);
//outputlist();
}
/*
int findx = MemoA.get(4);
if(findx == -1)
System.out.println("Do not have now");
else
{
System.out.println("result is: "+findx);
outputlist();
}
* */
}
}
}