usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespacealgorithms{ Public classlruimplementation {Internal classCachenode {InternalCachenode Next =NULL; InternalCachenode Previous =NULL; Internal Objectvalue; Internal stringkey; InternalCachenode (stringKeyObjectval) { This. Key =key; This. value=Val; } } Privatedictionary<string, cachenode> values =NULL; Private intSize =0; PrivateCachenode first; PrivateCachenode last; PublicLruimplementation (intsize) { This. Size =size; This. Values =Newdictionary<string, cachenode>(size); This. First = This. Last =NULL; } Public intValuecount {Get { return This. Values. Count; } } Public string[] Keys {Get { if( This. first! =NULL) {List<string> keylist =Newlist<string>(); Cachenode node= This. First; Do{keylist.add (Node.key); Node=node. Next; } while(Node! =NULL); returnKeylist.toarray (); } return NULL; } } Private voidRemovelrunode () {if( This. Last! =NULL) { This. values. Remove ( This. Last.key); This. Last = This. Last. Previous; This. Last. Next =NULL; } } Public voidAddValue (stringKeyObjectvalue) {cachenode node=NULL; if( This. Values. ContainsKey (key)) {node= This. Values[key]; Node.value=value; if(Node! = This. First) { if(node. Next! =NULL) {node. Next.previous=node. Previous; } if(node. Previous! =NULL) {node. Previous.next=node. Next; } } } Else{node=NewCachenode (key, value); if( This. values. Count >= This. Size) { This. Removelrunode (); } This. Values. ADD (key, node); } if( This. first! =NULL&& This. first! =node) {node. Next= This. First; This. First. Previous =node; } This. First =node; if( This. last = =NULL) { This. Last =node; } } Public ObjectTryGetValue (stringkey) { if( This. Values. ContainsKey (key)) {Cachenode node= This. Values[key]; if(Node! = This. First) { if(node. Previous! =NULL) {node. Previous.next=node. Next; } if(node. Next! =NULL) {node. Next.previous=node. Previous; } This. First. Previous =node; Node. Next= This. First; This. First =node; } return This. First.value; } return NULL; } Public stringgetbuffercontent () {return string. Join (",", This. Keys); } }}
The main use of Hashtable (Dictionary) and linked list implementation, can achieve O (1) Read and write time complexity.
Test code:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespacealgorithms{classProgram {Static voidMain (string[] args) { Do { intCacheSize =0; BOOLIsValid =true; Do{Console.WriteLine ("Input Cache Size:"); IsValid=int. TryParse (Console.ReadLine (), outcacheSize); if(CacheSize <=0) {IsValid=false; } if(!isValid) {Console.WriteLine ("Invalid value."); } } while(!isValid); Lruimplementation LRU=Newlruimplementation (cacheSize); Random Rand=NewRandom (); Do{Console.WriteLine ("Input Key:"); stringKey =Console.ReadLine (); Console.WriteLine ("Input Value:"); stringval =Console.ReadLine (); Lru. AddValue (Key, Val); Console.WriteLine ("Cache Content:"); Console.foregroundcolor=Consolecolor.green; Console.WriteLine (LRU. Getbuffercontent ()); Console.resetcolor (); intRandindex = Rand. Next (0Lru. Valuecount-1); Console.WriteLine ("Try to query the {0}th value.", Randindex); Lru. TryGetValue (LRU. Keys[randindex]); Console.WriteLine ("Cache Content:"); Console.foregroundcolor=Consolecolor.green; Console.WriteLine (LRU. Getbuffercontent ()); Console.resetcolor (); } while(true); } while(true); } }}
O (1) LRU strategy cache implementation for read-write time complexity