Normand Briere
2018-07-07 e416acb9b012b17d1efe49ad2199ea7132d874d1
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
package timeflow.util;
 
import java.util.*;
 
public class Bag<T> implements Iterable<T> {
   HashMap<T, Count> table;
   int max;
   
   public Bag()
   {
       table=new HashMap<T, Count>();
   }
   
   public Bag(Iterable<T> i)
   {
       for (T x:i)
           add(x);
   }
   
   public Bag(T[] array)
   {
       for (int i=0; i<array.length; i++)
           add(array[i]);
   }
   
   public int getMax()
   {
       return max;
   }
   
   public List<T> listTop(int n)
   {
       int count=0;
       Iterator<T> i=list().iterator();
       List<T> top=new ArrayList<T>();
       while (count<n && i.hasNext())
       {
           top.add(i.next());
           count++;
       }
       return top;
   }
   
   public List<T> unordered()
   {
       List<T> result=new ArrayList<T>();
       result.addAll(table.keySet());
       return result;
   }
   
   public List<T> list()
   {
       List<T> result=new ArrayList<T>();
       result.addAll(table.keySet());
       
       Collections.sort(result, new Comparator<T>()
               {
                   public int compare(T x, T y)
                   {
                       return num(y)-num(x);
                   }
               });
       return result;
   }
   
   public int num(T x)
   {
       Count c=table.get(x);
       if (c!=null)
           return c.num;
       else
           return 0;
   }
   
   public int add(T x)
   {        
       Count c=table.get(x);
       int n=0;
       if (c!=null)
           n=++c.num;
       else
       {
           table.put(x, new Count(1));
           n=1;
       }
       max=Math.max(n,max);
       return n;
   }
   
   class Count
   {
       int num;
       public Count(int num)
       {
           this.num=num;
       }
   }
   
   public int size()
   {
       return table.size();
   }
   
   public int removeLessThan(int cut)
   {
       
       Set<T> small=new HashSet<T>();
       for (T x: table.keySet())
       {
           if (num(x)<cut)
               small.add(x);
       }
       for (T x:small)
           table.remove(x);
       return small.size();
   }
   
   public static void main(String[] args)
   {
       Bag<String> b=new Bag<String>();
       b.add("a");
       b.add("b");
       b.add("a");
       System.out.println(b.num("a"));
       System.out.println(b.num("b"));
       System.out.println(b.num("c"));
       List<String> s=b.list();
       for (int i=0; i<s.size(); i++)
           System.out.println(s.get(i)+": "+b.num(s.get(i)));
   }
 
   @Override
   public Iterator<T> iterator() {
       return table.keySet().iterator();
   }
}