Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (516)
Games in Android Showcase (122)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Collection: Histogram / Frequency table  (Read 1464 times)
0 Members and 1 Guest are viewing this topic.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 823
Projects: 4
Exp: 16 years


Hand over your head.


« Posted 2011-05-09 22:13:52 »

You can use a histogram to count the number of occurences of a certain item in a collection.

Additionally you can query the histogram for the most frequently occuring items.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
import java.util.*;

public class Histogram<T>
{
   private final Map<T, Integer> map;

   public Histogram()
   {
      this.map = new HashMap<T, Integer>();
   }

   public void clear()
   {
      this.map.clear();
   }

   public int put(T t)
   {
      return this.put(t, 1);
   }

   public int put(T t, int amount)
   {
      if (amount < 0)
         throw new IllegalArgumentException();

      int count = this.get(t);
      if (amount == 0)
         return count;

      count += amount;
      this.map.put(t, Integer.valueOf(count));
      return count;
   }

   public int get(T t)
   {
      Integer count = this.map.get(t);
      if (count == null)
         return 0;
      return count.intValue();
   }

   public Set<T> keys()
   {
      return this.map.keySet();
   }

   public List<T> thresholdKeys(int atLeast)
   {
      int found = 0;
      for (Integer c : this.map.values())
         if (c.intValue() >= atLeast)
            found++;
      return topKeys(found);
   }

   public List<T> topKeys()
   {
      return this.topKeys(Integer.MAX_VALUE);
   }

   public List<T> topKeys(int amount)
   {
      List<T> kList = new ArrayList<T>();
      List<Integer> vList = new ArrayList<Integer>();

      for (Entry<T, Integer> e : this.map.entrySet())
      {
         if (vList.size() == amount + 1)
         {
            // when overflowed, remove lowest
            int index = indexOfMin(vList);
            kList.remove(index);
            vList.remove(index);
         }

         kList.add(e.getKey());
         vList.add(e.getValue());
      }

      // bubble sort, assuming 'amount' is reasonably small
      for (int i = 0; i < vList.size(); i++)
      {
         for (int k = i + 1; k < vList.size(); k++)
         {
            if (vList.get(i).intValue() >= vList.get(k).intValue())
            {
               continue;
            }

            Integer a = vList.get(i);
            Integer b = vList.get(k);
            vList.set(k, a);
            vList.set(i, b);

            T x = kList.get(i);
            T y = kList.get(k);
            kList.set(k, x);
            kList.set(i, y);
         }
      }

      if (kList.size() == amount + 1)
      {
         // drop smallest item, if overflowed
         kList.remove(kList.size() - 1);
         vList.remove(vList.size() - 1);
      }

      return kList;
   }

   public int remove(T t)
   {
      return this.remove(t, 1);
   }

   public int remove(T t, int amount)
   {
      Integer count = this.map.get(t);
      if (count == null)
         throw new NoSuchElementException(String.valueOf(t));
      if (amount < 0)
         throw new IllegalArgumentException();
      if (count.intValue() < amount)
         throw new IllegalStateException("cannot remove");

      count = Integer.valueOf(count.intValue() - amount);
      if (count.intValue() == 0)
         this.map.remove(t);
      else
         this.map.put(t, count);

      return count.intValue();
   }

   public int reset(T t)
   {
      Integer count = this.map.remove(t);
      if (count == null)
         return 0;
      return count.intValue();
   }

   public int set(T t, int val)
   {
      if (val < 0)
         throw new IllegalArgumentException();
      Integer count = this.map.get(t);

      if (count == null)
      {
         if (val != 0)
            this.map.put(t, Integer.valueOf(val));
         return 0;
      }

      if (val == 0)
         return this.map.remove(t).intValue();
      return this.map.put(t, Integer.valueOf(val)).intValue();
   }

   private static int indexOfMin(List<Integer> ids)
   {
      int minAt = 0;
      for (int i = 1; i < ids.size(); i++)
         if (ids.get(i).intValue() < ids.get(minAt).intValue())
            minAt = i;
      return minAt;
   }

   //

   public Histogram<T> asSynchronized()
   {
      final Histogram<T> backing = this;

      return new Histogram<T>()
      {
         public synchronized int put(T t)
         {
            return backing.put(t);
         }

         public synchronized int put(T t, int amount)
         {
            return backing.put(t, amount);
         }

         public synchronized int get(T t)
         {
            return backing.get(t);
         }

         public synchronized int remove(T t)
         {
            return backing.remove(t);
         }

         public synchronized int reset(T t)
         {
            return backing.reset(t);
         }

         public synchronized int set(T t, int val)
         {
            return backing.set(t, val);
         }

         @Override
         public synchronized Set<T> keys()
         {
            return backing.keys();
         }

         @Override
         public synchronized List<T> topKeys()
         {
            return backing.topKeys();
         }

         @Override
         public synchronized List<T> topKeys(int amount)
         {
            return backing.topKeys(amount);
         }

         @Override
         public boolean equals(Object obj)
         {
            return backing.equals(obj);
         }

         @Override
         public int hashCode()
         {
            return backing.hashCode();
         }

         @Override
         public String toString()
         {
            return backing.toString();
         }

         @Override
         public Histogram<T> asSynchronized()
         {
            return this;
         }
      };
   }
}

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline ra4king

JGO Kernel


Medals: 353
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-05-11 04:08:42 »

Wouldn't you want the "asSynchronized()" method return an instance of Histogram that contains all the objects in the parent Histogram?

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (31 views)
2014-10-27 03:28:38

TehJavaDev (26 views)
2014-10-27 03:27:51

DarkCart (41 views)
2014-10-26 19:37:11

Luminem (21 views)
2014-10-26 10:17:50

Luminem (26 views)
2014-10-26 10:14:04

theagentd (33 views)
2014-10-25 15:46:29

Longarmx (61 views)
2014-10-17 03:59:02

Norakomi (57 views)
2014-10-16 15:22:06

Norakomi (46 views)
2014-10-16 15:20:20

lcass (43 views)
2014-10-15 16:18:58
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!