public LinkMap() { this.head = new LinkMapNode <K, V>(); this.tail = new LinkMapNode <K, V>(); this.head.prev = this.head.next = tail; this.tail.prev = this.tail.next = head; }
public Collection <V> values() { Collection <V> vs = new Collection <V>(); LinkMapNode <K, V> temp = head.next; for (; ;) { if (temp == tail) { return(vs); } vs.Add(temp.getV()); temp = temp.next; } }
public Collection <K> keys() { Collection <K> ks = new Collection <K>(); LinkMapNode <K, V> temp = head.next; for (; ;) { if (temp == tail) { return(ks); } ks.Add(temp.getK()); temp = temp.next; } }
public IEnumerable <LinkMapNode <K, V> > getNodeOneByOne() { LinkMapNode <K, V> temp = tail.prev; for (; ;) { if (temp == head) { break; } yield return(temp); temp = temp.prev; } }
public LinkMapNode <K, V> getNode(K key) { LinkMapNode <K, V> temp = tail.prev; for (; ;) { if (temp == head) { return(null); } if (key.Equals(temp.getK())) { return(temp); } temp = temp.prev; } }
public V get(K key) { LinkMapNode <K, V> temp = head.next; for (; ;) { if (temp == tail) { return(default(V)); } if (key.Equals(temp.getK()) && temp != tail && temp != head) { return(temp.getV()); } temp = temp.next; } }
public bool containsValue(V value) { LinkMapNode <K, V> temp = head.next; for (; ;) { if (temp == tail) { return(false); } if (value.Equals(temp.getV())) { return(true); } temp = temp.next; } }
public bool containsKey(K key) { LinkMapNode <K, V> temp = head.next; for (; ;) { if (temp == tail) { return(false); } if (key.Equals(temp.getK())) { return(true); } temp = temp.next; } }
public V remove(K key) { V v = default(V); if (containsKey(key)) { lock (tail) { LinkMapNode <K, V> temp = getNode(key); if (null != temp) { temp.prev.next = temp.next; temp.next.prev = temp.prev; //lock (sizeLock) //{ size--; //} } } } return(v); }
public V put(K key, V value) { V v = default(V); LinkMapNode <K, V> temp = getNode(key); if (null == temp) { LinkMapNode <K, V> node = new LinkMapNode <K, V>(key, value); lock (tail.prev) { temp = getNode(key); if (null == temp) { node.next = tail; tail.prev.next = node; node.prev = tail.prev; tail.prev = node; //lock (sizeLock) //{ size++; //} } else { v = temp.setV(value); } } } else { ///保证后赢者赢 lock (temp) { v = temp.setV(value); } } return(v); }