diff options
author | Mark Spruiell <mes@zeroc.com> | 2008-01-30 20:37:02 -0800 |
---|---|---|
committer | Mark Spruiell <mes@zeroc.com> | 2008-01-30 20:37:02 -0800 |
commit | 227943453d5c491f7e398fc1ee9b09d4885d8078 (patch) | |
tree | d7fe0db7d3856d5c182d61a3bb500759d59d7f81 | |
parent | bug 2274 - review exception catching in SL tests (diff) | |
download | ice-227943453d5c491f7e398fc1ee9b09d4885d8078.tar.bz2 ice-227943453d5c491f7e398fc1ee9b09d4885d8078.tar.xz ice-227943453d5c491f7e398fc1ee9b09d4885d8078.zip |
bug 2282 - generic evictor demo
-rw-r--r-- | cs/demo/book/evictor/EvictorBase.cs | 29 | ||||
-rw-r--r-- | cs/demo/book/evictor/LinkedList.cs | 105 | ||||
-rw-r--r-- | java/demo/book/evictor/EvictorBase.java | 16 | ||||
-rw-r--r-- | java/demo/book/evictor/LinkedList.java | 46 |
4 files changed, 138 insertions, 58 deletions
diff --git a/cs/demo/book/evictor/EvictorBase.cs b/cs/demo/book/evictor/EvictorBase.cs index 7fb6171709a..8600314806f 100644 --- a/cs/demo/book/evictor/EvictorBase.cs +++ b/cs/demo/book/evictor/EvictorBase.cs @@ -1,8 +1,9 @@ namespace Evictor { + using System.Collections.Generic; + public abstract class EvictorBase : Ice.ServantLocator { - public EvictorBase() { _size = 1000; @@ -13,18 +14,18 @@ namespace Evictor _size = size < 0 ? 1000 : size; } - protected abstract Ice.Object add(Ice.Current c, out System.Object cookie); + protected abstract Ice.Object add(Ice.Current c, out object cookie); - protected abstract void evict(Ice.Object servant, out System.Object xcookie); + protected abstract void evict(Ice.Object servant, object cookie); - public Ice.Object locate(Ice.Current c, out System.Object cookie) + public Ice.Object locate(Ice.Current c, out object cookie) { lock(this) { // // Check if we a servant in the map already. // - EvictorEntry entry = (EvictorEntry)_map[c.id]; + EvictorEntry entry = _map[c.id]; if(entry != null) { // @@ -56,7 +57,7 @@ namespace Evictor // ++(entry.useCount); _queue.AddFirst(c.id); - entry.queuePos = (LinkedList.Enumerator)_queue.GetEnumerator(); + entry.queuePos = (LinkedList<Ice.Identity>.Enumerator)_queue.GetEnumerator(); entry.queuePos.MoveNext(); cookie = entry; @@ -65,7 +66,7 @@ namespace Evictor } } - public void finished(Ice.Current c, Ice.Object o, System.Object cookie) + public void finished(Ice.Current c, Ice.Object o, object cookie) { lock(this) { @@ -92,8 +93,8 @@ namespace Evictor private class EvictorEntry { internal Ice.Object servant; - internal System.Object userCookie; - internal LinkedList.Enumerator queuePos; + internal object userCookie; + internal LinkedList<Ice.Identity>.Enumerator queuePos; internal int useCount; } @@ -104,13 +105,13 @@ namespace Evictor // look at the excess elements to see whether any of them // can be evicted. // - LinkedList.Enumerator p = (LinkedList.Enumerator)_queue.GetEnumerator(); + LinkedList<Ice.Identity>.Enumerator p = (LinkedList<Ice.Identity>.Enumerator)_queue.GetEnumerator(); int excessEntries = _map.Count - _size; for(int i = 0; i < excessEntries; ++i) { p.MovePrev(); - Ice.Identity id = (Ice.Identity)p.Current; - EvictorEntry e = (EvictorEntry)_map[id]; + Ice.Identity id = p.Current; + EvictorEntry e = _map[id]; if(e.useCount == 0) { evict(e.servant, e.userCookie); // Down-call @@ -120,8 +121,8 @@ namespace Evictor } } - private System.Collections.Hashtable _map = new System.Collections.Hashtable(); - private LinkedList _queue = new LinkedList(); + private Dictionary<Ice.Identity, EvictorEntry> _map = new Dictionary<Ice.Identity, EvictorEntry>(); + private LinkedList<Ice.Identity> _queue = new LinkedList<Ice.Identity>(); private int _size; } } diff --git a/cs/demo/book/evictor/LinkedList.cs b/cs/demo/book/evictor/LinkedList.cs index 6a9b2de725b..387ef95a3fb 100644 --- a/cs/demo/book/evictor/LinkedList.cs +++ b/cs/demo/book/evictor/LinkedList.cs @@ -2,15 +2,14 @@ namespace Evictor { using System; using System.Collections; + using System.Collections.Generic; using System.Diagnostics; - public class LinkedList : ICollection, ICloneable + public class LinkedList<T> : ICollection<T>, ICollection, ICloneable { public LinkedList() { - _head = null; - _tail = null; - _count = 0; + Clear(); } public int Count @@ -21,6 +20,14 @@ namespace Evictor } } + public bool IsReadOnly + { + get + { + return false; + } + } + public bool IsSynchronized { get @@ -37,7 +44,7 @@ namespace Evictor } } - public void CopyTo(Array array, int index) + public void CopyTo(T[] array, int index) { // // Check preconditions. @@ -69,19 +76,34 @@ namespace Evictor Node n = _head; while(n != null) { - array.SetValue(n.val, index++); + array[index++] = n.val; n = (Node)n.next; } } + void ICollection.CopyTo(Array array, int index) + { + T[] arr = array as T[]; + if(arr == null) + { + throw new ArgumentException("array"); + } + CopyTo(arr, index); + } + public IEnumerator GetEnumerator() { return new Enumerator(this); } + IEnumerator<T> IEnumerable<T>.GetEnumerator() + { + return new Enumerator(this); + } + public object Clone() { - LinkedList l = new LinkedList(); + LinkedList<T> l = new LinkedList<T>(); Node cursor = _head; while(cursor != null) { @@ -91,7 +113,7 @@ namespace Evictor return l; } - public void Add(object value) + public void Add(T value) { Node n = new Node(); n.val = value; @@ -110,7 +132,7 @@ namespace Evictor _count++; } - public void AddFirst(object value) + public void AddFirst(T value) { Node n = new Node(); n.val = value; @@ -129,6 +151,43 @@ namespace Evictor _count++; } + public void Clear() + { + _head = null; + _tail = null; + _count = 0; + } + + public bool Contains(T value) + { + return Find(value) != null; + } + + public bool Remove(T value) + { + Node n = Find(value); + if(n != null) + { + Remove(n); + return true; + } + return false; + } + + private Node Find(T value) + { + Node n = _head; + while(n != null) + { + if(Object.Equals(value, n.val)) + { + return n; + } + n = n.next; + } + return null; + } + private void Remove(Node n) { Debug.Assert(n != null); @@ -157,16 +216,16 @@ namespace Evictor { internal Node next; internal Node prev; - internal object val; + internal T val; } private Node _head; private Node _tail; private int _count; - public class Enumerator : IEnumerator + public class Enumerator : IEnumerator<T>, IEnumerator, IDisposable { - internal Enumerator(LinkedList list) + internal Enumerator(LinkedList<T> list) { _list = list; _current = null; @@ -183,7 +242,7 @@ namespace Evictor _removed = false; } - public object Current + public T Current { get { @@ -195,6 +254,14 @@ namespace Evictor } } + object IEnumerator.Current + { + get + { + return Current; + } + } + public bool MoveNext() { if(_removed) @@ -249,7 +316,17 @@ namespace Evictor _current = null; } - private LinkedList _list; // The list we are iterating over. + public void Dispose() + { + if(_list == null) + { + throw new ObjectDisposedException(null); + } + Reset(); + _list = null; + } + + private LinkedList<T> _list; // The list we are iterating over. private Node _current; // Current iterator position. private Node _moveNext; // Remembers node that followed a removed element. private Node _movePrev; // Remembers node that preceded a removed element. diff --git a/java/demo/book/evictor/EvictorBase.java b/java/demo/book/evictor/EvictorBase.java index ed471738e95..c309bd2866a 100644 --- a/java/demo/book/evictor/EvictorBase.java +++ b/java/demo/book/evictor/EvictorBase.java @@ -26,7 +26,7 @@ public abstract class EvictorBase implements Ice.ServantLocator // // Check if we have a servant in the map already. // - EvictorEntry entry = (EvictorEntry)_map.get(c.id); + EvictorEntry entry = _map.get(c.id); if(entry != null) { // @@ -90,7 +90,7 @@ public abstract class EvictorBase implements Ice.ServantLocator { Ice.Object servant; java.lang.Object userCookie; - java.util.Iterator queuePos; + java.util.Iterator<Ice.Identity> queuePos; int useCount; } @@ -101,12 +101,12 @@ public abstract class EvictorBase implements Ice.ServantLocator // look at the excess elements to see whether any of them // can be evicted. // - java.util.Iterator p = _queue.riterator(); + java.util.Iterator<Ice.Identity> p = _queue.riterator(); int excessEntries = _map.size() - _size; for(int i = 0; i < excessEntries; ++i) { - Ice.Identity id = (Ice.Identity)p.next(); - EvictorEntry e = (EvictorEntry)_map.get(id); + Ice.Identity id = p.next(); + EvictorEntry e = _map.get(id); if(e.useCount == 0) { evict(e.servant, e.userCookie); // Down-call @@ -116,7 +116,9 @@ public abstract class EvictorBase implements Ice.ServantLocator } } - private java.util.Map _map = new java.util.HashMap(); - private Evictor.LinkedList _queue = new Evictor.LinkedList(); + private java.util.Map<Ice.Identity, EvictorEntry> _map = + new java.util.HashMap<Ice.Identity, EvictorEntry>(); + private Evictor.LinkedList<Ice.Identity> _queue = + new Evictor.LinkedList<Ice.Identity>(); private int _size; } diff --git a/java/demo/book/evictor/LinkedList.java b/java/demo/book/evictor/LinkedList.java index 09cd5f13ecf..28ec1d75681 100644 --- a/java/demo/book/evictor/LinkedList.java +++ b/java/demo/book/evictor/LinkedList.java @@ -20,7 +20,7 @@ package Evictor; // retained over structural changes to the list itself (similar to an // STL list). // -public class LinkedList +public class LinkedList<T> { public LinkedList() @@ -28,7 +28,7 @@ public class LinkedList _header.next = _header.previous = _header; } - public java.lang.Object + public T getFirst() { if(_size == 0) @@ -40,7 +40,7 @@ public class LinkedList } public void - addFirst(java.lang.Object o) + addFirst(T o) { addBefore(o, _header.next); } @@ -57,19 +57,19 @@ public class LinkedList return _size; } - public java.util.Iterator + public java.util.Iterator<T> iterator() { return new ForwardIterator(); } - public java.util.Iterator + public java.util.Iterator<T> riterator() { return new ReverseIterator(); } - private class ForwardIterator implements java.util.Iterator + private class ForwardIterator implements java.util.Iterator<T> { public boolean hasNext() @@ -77,7 +77,7 @@ public class LinkedList return _next != null; } - public java.lang.Object + public T next() { if(_next == null) @@ -122,11 +122,11 @@ public class LinkedList _current = null; } - private Entry _current; - private Entry _next; + private Entry<T> _current; + private Entry<T> _next; } - private class ReverseIterator implements java.util.Iterator + private class ReverseIterator implements java.util.Iterator<T> { public boolean hasNext() @@ -134,7 +134,7 @@ public class LinkedList return _next != null; } - public java.lang.Object + public T next() { if(_next == null) @@ -179,17 +179,17 @@ public class LinkedList _current = null; } - private Entry _current; - private Entry _next; + private Entry<T> _current; + private Entry<T> _next; } - private static class Entry + private static class Entry<T> { - java.lang.Object element; - Entry next; - Entry previous; + T element; + Entry<T> next; + Entry<T> previous; - Entry(java.lang.Object element, Entry next, Entry previous) + Entry(T element, Entry<T> next, Entry<T> previous) { this.element = element; this.next = next; @@ -197,10 +197,10 @@ public class LinkedList } } - private Entry - addBefore(java.lang.Object o, Entry e) + private Entry<T> + addBefore(T o, Entry<T> e) { - Entry newEntry = new Entry(o, e, e.previous); + Entry<T> newEntry = new Entry<T>(o, e, e.previous); newEntry.previous.next = newEntry; newEntry.next.previous = newEntry; _size++; @@ -208,7 +208,7 @@ public class LinkedList } private void - remove(Entry e) + remove(Entry<T> e) { if(e == _header) { @@ -220,6 +220,6 @@ public class LinkedList _size--; } - private Entry _header = new Entry(null, null, null); + private Entry<T> _header = new Entry<T>(null, null, null); private int _size = 0; } |