Normand Briere
2018-07-07 09ddd38fd4a8a7100c834a5e976f4796fae53541
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
package timeflow.util;
 
import java.util.*;
 
public class DoubleBag<T> implements Iterable<T> {
   private HashMap<T, Count> table;
   private double max;
   
   public DoubleBag()
   {
       table=new HashMap<T, Count>();
   }
 
   public double getMax()
   {
       return max;
   }
   
   public List<T> listTop(int n, boolean useSum)
   {
       int count=0;
       Iterator<T> i=list(useSum).iterator();
       List<T> top=new ArrayList<T>();
       while (count<n && i.hasNext())
       {
           top.add(i.next());
           count++;
       }
       return top;
   }
   
   public List<T> list(final boolean useSum)
   {
       List<T> result=new ArrayList<T>();
       result.addAll(table.keySet());
       
       Collections.sort(result, new Comparator<T>()
               {
                   public int compare(T x, T y)
                   {
                       double d= useSum ? num(y)-num(x) : average(y)-average(x);
                       return d>0 ? 1 : (d<0 ? -1 : 0);
                   }
               });
       return result;
   }
   
   public double num(T x)
   {
       Count c=table.get(x);
       if (c!=null)
           return c.num;
       else
           return 0;
   }
   
   public double average(T x)
   {
       Count c=table.get(x);
       return c.num/c.vals;
   }
   
   public void add(T x, double z)
   {
       if (Double.isNaN(z))
           return;
       Count c=table.get(x);
       double sum=z;
       if (c!=null)
       {
            c.add(z);
            sum=c.num;
       }
       else
       {
           table.put(x, new Count(z));
       }
       max=Math.max(sum, max);
   }
   
   class Count
   {
       double num;
       int vals;
       
       public Count(double num)
       {
           this.num=num;
           vals=1;
       }
       
       public double add(double x)
       {
           vals++;
           return num+=x;
       }
   }
   
   public int size()
   {
       return table.size();
   }
   
   
   public List<T> unordered()
   {
       List<T> result=new ArrayList<T>();
       result.addAll(table.keySet());
       return result;
   }
 
   
   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)
   {
       DoubleBag<String> b=new DoubleBag<String>();
       b.add("a",1);
       b.add("b",2);
       b.add("a",3);
       System.out.println(b.num("a"));
       System.out.println(b.num("b"));
       System.out.println(b.num("c"));
       List<String> s=b.list(true);
       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();
   }
}