3 // namespace: System.Text.RegularExpressions
\r
6 // author: Dan Lewis (dlewis@gmx.co.uk)
\r
10 // Permission is hereby granted, free of charge, to any person obtaining
\r
11 // a copy of this software and associated documentation files (the
\r
12 // "Software"), to deal in the Software without restriction, including
\r
13 // without limitation the rights to use, copy, modify, merge, publish,
\r
14 // distribute, sublicense, and/or sell copies of the Software, and to
\r
15 // permit persons to whom the Software is furnished to do so, subject to
\r
16 // the following conditions:
\r
18 // The above copyright notice and this permission notice shall be
\r
19 // included in all copies or substantial portions of the Software.
\r
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
\r
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
\r
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
\r
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
31 using System.Collections;
\r
33 namespace System.Text.RegularExpressions {
\r
35 class FactoryCache {
\r
36 public FactoryCache (int capacity) {
\r
37 this.capacity = capacity;
\r
38 this.factories = new Hashtable (capacity);
\r
39 this.mru_list = new MRUList ();
\r
42 public void Add (string pattern, RegexOptions options, IMachineFactory factory) {
\r
44 Key k = new Key (pattern, options);
\r
46 factories[k] = factory;
\r
51 // lock must be held by the caller
\r
54 while (factories.Count >= capacity && capacity > 0) {
\r
55 object victim = mru_list.Evict ();
\r
57 factories.Remove ((Key) victim);
\r
61 public IMachineFactory Lookup (string pattern, RegexOptions options) {
\r
63 Key k = new Key (pattern, options);
\r
64 if (factories.Contains (k)) {
\r
66 return (IMachineFactory)factories[k];
\r
73 public int Capacity {
\r
74 get { return capacity; }
\r
76 // < 0 check done in the caller (Regex.CacheSize)
\r
84 private int capacity;
\r
85 private Hashtable factories;
\r
86 private MRUList mru_list;
\r
89 public string pattern;
\r
90 public RegexOptions options;
\r
92 public Key (string pattern, RegexOptions options) {
\r
93 this.pattern = pattern;
\r
94 this.options = options;
\r
97 public override int GetHashCode () {
\r
98 return pattern.GetHashCode () ^ (int)options;
\r
101 public override bool Equals (object o) {
\r
102 if (o == null || !(o is Key))
\r
106 return options == k.options && pattern.Equals (k.pattern);
\r
109 public override string ToString () {
\r
110 return "('" + pattern + "', [" + options + "])";
\r
116 public MRUList () {
\r
117 head = tail = null;
\r
120 public void Use (object o) {
\r
123 if (head == null) {
\r
124 node = new Node (o);
\r
125 head = tail = node;
\r
130 while (node != null && !o.Equals (node.value))
\r
131 node = node.previous;
\r
134 node = new Node (o);
\r
142 node.previous.next = node.next;
\r
144 node.next.previous = node.previous;
\r
148 node.previous = head;
\r
153 public object Evict () {
\r
157 object o = tail.value;
\r
163 tail.previous = null;
\r
168 private Node head, tail;
\r
170 private class Node {
\r
171 public object value;
\r
172 public Node previous, next;
\r
174 public Node (object value) {
\r
175 this.value = value;
\r