]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/map/Associativity.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / map / Associativity.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.utils.datastructures.map;
13
14
15 /**
16  * Description associativity of dimensions.
17  * The maximum number of dimensions is 32.
18  *
19  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
20  */
21 public final class Associativity {
22                 
23         public static Associativity of(boolean...dimensionAssociativity)
24         {
25                 return new Associativity(dimensionAssociativity);
26         }       
27         
28         public static Associativity[] fullAssociativity(int level)
29         {               
30                 int count = (1 << level)-1;
31                 Associativity[] result = new Associativity[count];
32                 for (int i=0; i<count; i++) 
33                         result[i] = new Associativity(level, i);
34                 return result;
35         }
36         
37         public int dimensionAssociativity;
38         public int level;
39         
40         public Associativity(boolean...dimensionAssociativity)
41         {
42                 if (dimensionAssociativity==null || dimensionAssociativity.length>32)
43                         throw new IllegalArgumentException();
44                 
45                 int mask = 1;
46                 for (boolean b : dimensionAssociativity) {
47                         if (b) this.dimensionAssociativity |= mask;
48                         mask <<= 1;
49                 }
50                 
51                 level = dimensionAssociativity.length;
52         }
53         public Associativity(int level, int dimensionAssociativity)
54         {
55                 this.dimensionAssociativity = dimensionAssociativity;
56                 this.level = level;
57         }
58         
59         @Override
60         public int hashCode() {
61                 return dimensionAssociativity;
62         }
63         
64         @Override
65         public boolean equals(Object obj) {
66         if (obj == null) return false;
67         if (!(obj.getClass().equals(this.getClass()))) return false;        
68         Associativity other = (Associativity) obj;                      
69                 return dimensionAssociativity == other.dimensionAssociativity;
70         }
71         
72         public int getLevel() {
73                 return level;
74         }
75         
76 }