FlagSet.java revision d8c8ec27ed2ec0b11fa37f476395ce27834471f0
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.databinding.tool.writer;
18
19import java.util.Arrays;
20import java.util.BitSet;
21
22/**
23 * Used for code generation. A BitSet can be converted into a flag set,
24 * which is basically a list of longs that can be divided into pieces.
25 */
26public class FlagSet {
27    public static final int sBucketSize = 64;// long
28    public final String type;
29    public final long[] buckets;
30    private String mLocalName;
31    private boolean mIsDynamic = false;
32
33    public FlagSet(BitSet bitSet, int bucketCount) {
34        buckets = new long[bucketCount];
35        for (int i = bitSet.nextSetBit(0);
36                i != -1; i = bitSet.nextSetBit(i + 1)) {
37            buckets[i / sBucketSize] |= 1 << (i % sBucketSize);
38        }
39        type = "long";
40    }
41
42    public FlagSet(long[] buckets) {
43        this.buckets = new long[buckets.length];
44        System.arraycopy(buckets, 0, this.buckets, 0, buckets.length);
45        type = "long";
46    }
47
48    public FlagSet(long[] buckets, int minBucketCount) {
49        this.buckets = new long[Math.max(buckets.length, minBucketCount)];
50        System.arraycopy(buckets, 0, this.buckets, 0, buckets.length);
51        type = "long";
52    }
53
54    public FlagSet(int... bits) {
55        int max = 0;
56        for (int i = 0 ; i < bits.length; i ++) {
57            max = Math.max(i, bits[i]);
58        }
59        buckets = new long[1 + (max / sBucketSize)];
60        for (int x = 0 ; x < bits.length; x ++) {
61            final int i = bits[x];
62            buckets[i / sBucketSize] |= 1 << (i % sBucketSize);
63        }
64        type = "long";
65    }
66
67    public boolean intersect(FlagSet other, int bucketIndex) {
68        return (buckets[bucketIndex] & other.buckets[bucketIndex]) != 0;
69    }
70
71    public String getLocalName() {
72        return mLocalName;
73    }
74
75    public void setLocalName(String localName) {
76        mLocalName = localName;
77    }
78
79    public boolean hasLocalName() {
80        return mLocalName != null;
81    }
82
83    public boolean isDynamic() {
84        return mIsDynamic;
85    }
86
87    public void setDynamic(boolean isDynamic) {
88        mIsDynamic = isDynamic;
89    }
90
91    public FlagSet andNot(FlagSet other) {
92        FlagSet result = new FlagSet(buckets);
93        final int min = Math.min(buckets.length, other.buckets.length);
94        for (int i = 0; i < min; i ++) {
95            result.buckets[i] &= ~(other.buckets[i]);
96        }
97        return result;
98    }
99
100    public FlagSet or(FlagSet other) {
101        final FlagSet result = new FlagSet(buckets, other.buckets.length);
102        for (int i = 0; i < other.buckets.length; i ++) {
103            result.buckets[i] |= other.buckets[i];
104        }
105        return result;
106    }
107
108    public boolean isEmpty() {
109        for (int i = 0; i < buckets.length; i ++) {
110            if (buckets[i] != 0) {
111                return false;
112            }
113        }
114        return true;
115    }
116
117    @Override
118    public String toString() {
119        StringBuilder sb = new StringBuilder();
120        for (int i = 0; i < buckets.length; i ++) {
121            sb.append(Long.toBinaryString(buckets[i])).append(" ");
122        }
123        return sb.toString();
124    }
125
126    private long getBucket(int bucketIndex) {
127        if (bucketIndex >= buckets.length) {
128            return 0;
129        }
130        return buckets[bucketIndex];
131    }
132
133    public boolean bitsEqual(FlagSet other) {
134        final int max = Math.max(buckets.length, other.buckets.length);
135        for (int i = 0; i < max; i ++) {
136            if (getBucket(i) != other.getBucket(i)) {
137                return false;
138            }
139        }
140        return true;
141    }
142}
143