1 33 package net.sf.jga.algorithms; 34 35 import java.util.Collection ; 36 import java.util.Comparator ; 37 import java.util.Iterator ; 38 import net.sf.jga.fn.BinaryFunctor; 39 import net.sf.jga.fn.UnaryFunctor; 40 import net.sf.jga.fn.algorithm.Accumulate; 41 import net.sf.jga.fn.arithmetic.Arithmetic; 42 import net.sf.jga.fn.arithmetic.ArithmeticFactory; 43 import net.sf.jga.fn.arithmetic.Average; 44 import net.sf.jga.fn.arithmetic.Plus; 45 import net.sf.jga.fn.comparison.EqualTo; 46 import net.sf.jga.fn.comparison.Equality; 47 import net.sf.jga.fn.comparison.Max; 48 import net.sf.jga.fn.comparison.Min; 49 import net.sf.jga.util.ArrayUtils; 50 51 52 58 59 public class Summarize { 60 61 65 68 static public <T> long count(T[] ts, T value) { 69 return count(ArrayUtils.iterate(ts), new EqualTo<T>().bind2nd(value)); 70 } 71 72 76 static public <T> long count(T[] ts, Equality<T> eq, T value) { 77 return count(ArrayUtils.iterate(ts), eq.bind2nd(value)); 78 } 79 80 83 static public <T> long count(T[] ts, UnaryFunctor<T,Boolean > pred) { 84 return count(ArrayUtils.iterate(ts), pred); 85 } 86 87 90 static public <T> long count(Iterable <T> input) { 91 return count(input.iterator()); 92 } 93 94 95 98 static public <T> long count(Iterable <? extends T> input, T value) { 99 return count(input.iterator(), value); 100 } 101 102 103 107 static public <T> long count(Iterable <? extends T> input, Equality<T> eq, T value) { 108 return count(input.iterator(), eq, value); 109 } 110 111 112 115 static public <T> long count(Iterable <? extends T> input, UnaryFunctor<T,Boolean > pred) { 116 return count(input.iterator(), pred); 117 } 118 119 123 static public <T> long count(Iterator <? extends T> iter) { 124 long ct = 0; 125 while(iter.hasNext()) { 126 iter.next(); 127 ++ct; 128 } 129 130 return ct; 131 } 132 133 134 138 static public <T> long count(Iterator <? extends T> iter, T value) { 139 return count(Filter.filter(iter, new EqualTo<T>().bind2nd(value))); 140 } 141 142 143 147 static public <T> long count(Iterator <? extends T> iter, Equality<T> eq, T value) { 148 return count(Filter.filter(iter, eq.bind2nd(value))); 149 } 150 151 152 156 static public <T> long count(Iterator <? extends T> iter, UnaryFunctor<T,Boolean > pred) { 157 return count(Filter.filter(iter, pred)); 158 } 159 160 161 165 168 static public <T> T min(T[] ts, Comparator <? super T> comp) { 169 return min(ArrayUtils.iterate(ts), new Min<T>(comp)); 170 } 171 172 173 176 static public <T extends Comparable <? super T>> T min(T[] ts) { 177 return min(ArrayUtils.iterate(ts), new Min.Comparable<T>()); 178 } 179 180 181 185 static public <T> T min(T[] ts, BinaryFunctor<T,T,T> fn) { 186 return min(ArrayUtils.iterate(ts), fn); 187 } 188 189 190 193 static public <T> T min(Iterable <? extends T> ts, Comparator <? super T> comp) { 194 return min(ts.iterator(), new Min<T>(comp)); 195 } 196 197 198 201 static public <T extends Comparable <? super T>> T min(Iterable <? extends T> ts) { 202 return min(ts.iterator(), new Min.Comparable<T>()); 203 } 204 205 206 210 static public <T> T min(Iterable <? extends T> ts, BinaryFunctor<T,T,T> fn) { 211 return min(ts.iterator(), fn); 212 } 213 214 215 219 static public <T> T min(Iterator <? extends T> ts, Comparator <? super T> comp) { 220 return min(ts, new Min<T>(comp)); 221 } 222 223 224 227 static public <T extends Comparable <? super T>> T min(Iterator <? extends T> ts) { 228 return min(ts, new Min.Comparable<T>()); 229 } 230 231 232 237 static public <T> T min(Iterator <? extends T> ts, BinaryFunctor<T,T,T> fn) { 238 return new Accumulate<T>(fn).fn(ts); 239 } 240 241 242 246 247 250 static public <T> T max(T[] ts, Comparator <? super T> comp) { 251 return max(ArrayUtils.iterate(ts), new Max<T>(comp)); 252 } 253 254 255 258 static public <T extends Comparable <? super T>> T max(T[] ts) { 259 return max(ArrayUtils.iterate(ts), new Max.Comparable<T>()); 260 } 261 262 263 267 static public <T> T max(T[] ts, BinaryFunctor<T,T,T> fn) { 268 return max(ArrayUtils.iterate(ts), fn); 269 } 270 271 272 275 static public <T> T max(Iterable <? extends T> ts, Comparator <? super T> comp) { 276 return max(ts.iterator(), new Max<T>(comp)); 277 } 278 279 280 283 static public <T extends Comparable <? super T>> T max(Iterable <? extends T> ts) { 284 return max(ts.iterator(), new Max.Comparable<T>()); 285 } 286 287 288 292 static public <T> T max(Iterable <? extends T> ts, BinaryFunctor<T,T,T> fn) { 293 return max(ts.iterator(), fn); 294 } 295 296 297 301 static public <T> T max(Iterator <? extends T> ts, Comparator <? super T> comp) { 302 return max(ts, new Max<T>(comp)); 303 } 304 305 306 309 static public <T extends Comparable <? super T>> T max(Iterator <? extends T> ts) { 310 return max(ts, new Max.Comparable<T>()); 311 } 312 313 314 319 static public <T> T max(Iterator <? extends T> ts, BinaryFunctor<T,T,T> fn) { 320 return new Accumulate<T>(fn).fn(ts); 321 } 322 323 324 328 329 332 static public <T extends Number > T sum(T[] ts) { 333 return sum((Class <T>) ts.getClass().getComponentType(), ArrayUtils.iterate(ts)); 334 } 335 336 339 static public <T extends Number > T sum(Class <T> type, T[] ts) { 340 return sum(type, ArrayUtils.iterate(ts)); 341 } 342 343 346 static public <T extends Number > T sum(Class <T> type, Iterable <? extends T> ts) { 347 return sum(type, ts.iterator()); 348 } 349 350 351 354 static public <T extends Number > T sum(Class <T> type, Iterator <? extends T> ts) { 355 return accumulate(ts, new Plus<T>(type)); 356 } 357 358 359 363 364 static public <T> T accumulate(T[] ts, BinaryFunctor<T,T,T> fn) { 365 return accumulate(ArrayUtils.iterate(ts), fn); 366 } 367 368 369 static public <T> T accumulate(T[] ts, T initial, BinaryFunctor<T,T,T> fn) { 370 return accumulate(ArrayUtils.iterate(ts), initial, fn); 371 372 } 373 374 375 static public <T> T accumulate(Iterable <? extends T> values, BinaryFunctor<T,T,T> fn) { 376 return accumulate(values.iterator(), fn); 377 } 378 379 380 static public <T> T accumulate(Iterable <? extends T> values, T initial, BinaryFunctor<T,T,T> fn) { 381 return accumulate(values.iterator(), initial, fn); 382 } 383 384 385 static public <T> T accumulate(Iterator <? extends T> iterator, BinaryFunctor<T,T,T> fn) { 386 return new Accumulate<T>(fn).fn(iterator); 387 } 388 389 390 static public <T> T accumulate(Iterator <? extends T> iterator, T initial, BinaryFunctor<T,T,T> fn) { 391 return new Accumulate<T>(initial, fn).fn(iterator); 392 } 393 394 395 399 402 static public <T extends Number > T average(T[] ts) { 403 return average((Class <T>) ts.getClass().getComponentType(), ArrayUtils.iterate(ts)); 404 } 405 406 409 static public <T extends Number > T average(Class <T> type, T[] ts) { 410 return average(type, ArrayUtils.iterate(ts)); 411 } 412 413 416 static public <T extends Number > T average(Class <T> type, Iterable <T> ts) { 417 return average(type, ts.iterator()); 418 } 419 420 421 424 static public <T extends Number > T average(Class <T> type, Iterator <T> ts) { 425 return new Average<T>(type).fn(ts); 426 } 427 } 428 | Popular Tags |