Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / tools / monkeydoc / Lucene.Net / Lucene.Net / Util / AverageGuessMemoryModel.cs
1 /* 
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  * 
9  * http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 using System;
19
20 namespace Mono.Lucene.Net.Util
21 {
22         
23         /// <summary> An average, best guess, MemoryModel that should work okay on most systems.
24         /// 
25         /// </summary>
26         public class AverageGuessMemoryModel:MemoryModel
27         {
28                 public AverageGuessMemoryModel()
29                 {
30                         InitBlock();
31                 }
32                 internal class AnonymousClassIdentityHashMap : System.Collections.Hashtable /*IdentityHashMap*/  // {{Aroush-2.9.0}} Port issue? Will this do the trick to mimic java's IdentityHashMap?
33                 {
34                         public AnonymousClassIdentityHashMap(AverageGuessMemoryModel enclosingInstance)
35                         {
36                                 InitBlock(enclosingInstance);
37                         }
38                         private void  InitBlock(AverageGuessMemoryModel enclosingInstance)
39                         {
40                                 this.enclosingInstance = enclosingInstance;
41                                 Add(typeof(bool), 1);
42                                 Add(typeof(byte), 1);
43                 Add(typeof(sbyte), 1);
44                                 Add(typeof(char), 2);
45                                 Add(typeof(short), 2);
46                                 Add(typeof(int), 4);
47                                 Add(typeof(float), 4);
48                                 Add(typeof(double), 8);
49                                 Add(typeof(long), 8);
50                         }
51                         private AverageGuessMemoryModel enclosingInstance;
52                         public AverageGuessMemoryModel Enclosing_Instance
53                         {
54                                 get
55                                 {
56                                         return enclosingInstance;
57                                 }
58                                 
59                         }
60             // {{Aroush-2.9.0}} Port issue? Will this do the trick to mimic java's IdentityHashMap?
61             /*
62              * From Java docs:
63              * This class implements the Map interface with a hash table, using 
64              * reference-equality in place of object-equality when comparing keys 
65              * (and values). In other words, in an IdentityHashMap, two keys k1 and k2 
66              * are considered equal if and only if (k1==k2). (In normal Map 
67              * implementations (like HashMap) two keys k1 and k2 are considered 
68              * equal if and only if (k1==null ? k2==null : k1.equals(k2)).) 
69              */
70             public new bool Equals(Object obj)
71             {
72                 return this.GetHashCode() == obj.GetHashCode();
73             }
74             public new static bool Equals(Object objA, Object objB)
75             {
76                 return objA.GetHashCode() == objB.GetHashCode();
77             }
78             // {{Aroush-2.9.0}} Port issue, need to mimic java's IdentityHashMap
79                 }
80                 private void  InitBlock()
81                 {
82                         sizes = new AnonymousClassIdentityHashMap(this);
83                 }
84                 // best guess primitive sizes
85                 private System.Collections.IDictionary sizes;
86                 
87                 /*
88                 * (non-Javadoc)
89                 * 
90                 * @see Mono.Lucene.Net.Util.MemoryModel#getArraySize()
91                 */
92                 public override int GetArraySize()
93                 {
94                         return 16;
95                 }
96                 
97                 /*
98                 * (non-Javadoc)
99                 * 
100                 * @see Mono.Lucene.Net.Util.MemoryModel#getClassSize()
101                 */
102                 public override int GetClassSize()
103                 {
104                         return 8;
105                 }
106                 
107                 /* (non-Javadoc)
108                 * @see Mono.Lucene.Net.Util.MemoryModel#getPrimitiveSize(java.lang.Class)
109                 */
110                 public override int GetPrimitiveSize(System.Type clazz)
111                 {
112                         return ((System.Int32) sizes[clazz]);
113                 }
114                 
115                 /* (non-Javadoc)
116                 * @see Mono.Lucene.Net.Util.MemoryModel#getReferenceSize()
117                 */
118                 public override int GetReferenceSize()
119                 {
120                         return 4;
121                 }
122         }
123 }