3 // namespace: System.Text.RegularExpressions
\r
6 // author: Dan Lewis (dlewis@gmx.co.uk)
\r
10 using System.Collections;
\r
12 namespace System.Text.RegularExpressions {
\r
14 class FactoryCache {
\r
15 public FactoryCache (int capacity) {
\r
16 this.capacity = capacity;
\r
17 this.factories = new Hashtable (capacity);
\r
18 this.mru_list = new MRUList ();
\r
21 public void Add (string pattern, RegexOptions options, IMachineFactory factory) {
\r
23 Key k = new Key (pattern, options);
\r
25 while (factories.Count >= capacity) {
\r
26 object victim = mru_list.Evict ();
\r
28 factories.Remove ((Key)victim);
\r
31 factories[k] = factory;
\r
36 public IMachineFactory Lookup (string pattern, RegexOptions options) {
\r
38 Key k = new Key (pattern, options);
\r
39 if (factories.Contains (k)) {
\r
41 return (IMachineFactory)factories[k];
\r
48 private int capacity;
\r
49 private Hashtable factories;
\r
50 private MRUList mru_list;
\r
53 public string pattern;
\r
54 public RegexOptions options;
\r
56 public Key (string pattern, RegexOptions options) {
\r
57 this.pattern = pattern;
\r
58 this.options = options;
\r
61 public override int GetHashCode () {
\r
62 return pattern.GetHashCode () ^ (int)options;
\r
65 public override bool Equals (object o) {
\r
66 if (o == null || !(o is Key))
\r
70 return options == k.options && pattern.Equals (k.pattern);
\r
73 public override string ToString () {
\r
74 return "('" + pattern + "', [" + options + "])";
\r
84 public void Use (object o) {
\r
88 node = new Node (o);
\r
94 while (node != null && !o.Equals (node.value))
\r
95 node = node.previous;
\r
98 node = new Node (o);
\r
106 node.previous.next = node.next;
\r
108 node.next.previous = node.previous;
\r
112 node.previous = head;
\r
117 public object Evict () {
\r
121 object o = tail.value;
\r
127 tail.previous = null;
\r
132 private Node head, tail;
\r
134 private class Node {
\r
135 public object value;
\r
136 public Node previous, next;
\r
138 public Node (object value) {
\r
139 this.value = value;
\r