Async Entity Component System based on the ideas of specs (https://github.com/amethyst/specs)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 line
1.3 KiB

  1. use std::cmp::{max, Ord, Ordering};
  2. use crate::{Driver, Executor, ParallelIterator};
  3. /* Max */
  4. pub struct Max<X> {
  5. iterator: X,
  6. }
  7. impl<X> Max<X> {
  8. pub fn new(iterator: X) -> Self {
  9. Self { iterator }
  10. }
  11. }
  12. impl<'a, X, T> Driver<'a, Option<T>> for Max<X>
  13. where
  14. X: ParallelIterator<'a, Item = T>,
  15. T: Send + Ord + 'a,
  16. {
  17. fn exec_with<E>(self, executor: E) -> E::Result
  18. where
  19. E: Executor<'a, Option<T>>,
  20. {
  21. self.iterator.reduce_with(max).exec_with(executor)
  22. }
  23. }
  24. /* MaxBy */
  25. pub struct MaxBy<X, O> {
  26. iterator: X,
  27. operation: O,
  28. }
  29. impl<X, O> MaxBy<X, O> {
  30. pub fn new(iterator: X, operation: O) -> Self {
  31. Self {
  32. iterator,
  33. operation,
  34. }
  35. }
  36. }
  37. impl<'a, X, O, T> Driver<'a, Option<T>> for MaxBy<X, O>
  38. where
  39. X: ParallelIterator<'a, Item = T>,
  40. O: Fn(&T, &T) -> Ordering + Clone + Send + Sync + 'a,
  41. T: Send + Ord + 'a,
  42. {
  43. fn exec_with<E>(self, executor: E) -> E::Result
  44. where
  45. E: Executor<'a, Option<T>>,
  46. {
  47. let operation = self.operation;
  48. self.iterator
  49. .reduce_with(move |a, b| match operation(&a, &b) {
  50. Ordering::Greater => b,
  51. _ => a,
  52. })
  53. .exec_with(executor)
  54. }
  55. }