Debian Codegraph @

src/dr/util/FrequencySet.java

raw symbols SCIP
1 /*
2 * FrequencySet.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.util;
27
28 import java.util.*;
29
30 /**
31 * Stores a set of objects with frequencies
32 *
33 * @version $Id: FrequencySet.java,v 1.9 2005/06/27 21:18:40 rambaut Exp $
34 *
35 * @author Andrew Rambaut
36 */
37 public class FrequencySet<T>
38 {
39 //
40 // Public stuff
41 //
42
43 public FrequencySet() {}
44
45 /** get number of objects */
46 public int size()
47 {
48 return size;
49 }
50
51 /** get object in frequency order */
52 public T get(int i) {
53 if (!sorted) {
54 sortByFrequency();
55 }
56
57 return list.get(i).object;
58 }
59
60
61 protected int getFrequency(T b) {
62 Bin bin = binMap.get(b);
63 if( bin == null ) {
64 return 0;
65 }
66 return bin.frequency;
67 }
68
69 /** get frequency of ith object */
70 public int getFrequency(int i)
71 {
72 if (!sorted) {
73 sortByFrequency();
74 }
75
76 return list.get(i).frequency;
77 }
78
79 /** get sum of all frequencies */
80 public int getSumFrequency() {
81 int sum = 0;
82 for (int i = 0, n = size(); i < n; i++) {
83 sum += getFrequency(i);
84 }
85
86 return sum;
87 }
88
89 /** adds an object to the set */
90 public void add(T object)
91 {
92 add(object, 1);
93 }
94
95 /**
96 * adds an object to the set with an initial frequency, or if object already
97 * in frequency set then frequency is incremented by 1.
98 */
99 public void add(T object, int frequency) {
100
101 Bin bin = binMap.get(object);
102 if (bin != null) {
103 bin.frequency += 1;
104 } else {
105 bin = new Bin(object, frequency);
106 binMap.put(object, bin);
107 size += 1;
108 sorted = false;
109 }
110 }
111
112 public Set<T> getKeySet() {
113 return binMap.keySet();
114 }
115
116 /** The frequencySets are equal if their inner sets are equal */
117 @Override
118 public boolean equals(Object obj) {
119 return (obj instanceof FrequencySet) && binMap.keySet().equals(((FrequencySet)obj).binMap.keySet());
120 }
121
122 /** sort by descending frequency */
123 private void sortByFrequency() {
124
125 list.clear();
126 for (Bin bin : binMap.values()) {
127 list.add(bin);
128 }
129
130 Collections.sort(list, frequencyComparator);
131 sorted = true;
132 }
133
134 //
135 // Private stuff
136 //
137
138 private List<Bin> list = new ArrayList<Bin>();
139 private Map<T, Bin> binMap = new HashMap<T, Bin>();
140 private boolean sorted = false;
141 private int size = 0;
142
143 private class Bin {
144 T object;
145 int frequency;
146
147 public Bin(T object, int frequency) {
148 this.object = object;
149 this.frequency = frequency;
150 }
151
152 @Override
153 public boolean equals(Object obj) {
154 return object.equals(((Bin)obj).object);
155 }
156
157 public int hashCode() {
158 return object.hashCode();
159 }
160 }
161
162 private Comparator<Bin> frequencyComparator = new Comparator<Bin>() {
163 public int compare(Bin bin1, Bin bin2) {
164 return bin2.frequency - bin1.frequency;
165 }
166 };
167 }