near total port (window-time is missing)

This commit is contained in:
Christophe Grand 2016-12-16 14:52:59 +01:00
parent c084089681
commit cffd76298b
2 changed files with 27 additions and 17 deletions

View file

@ -7,7 +7,8 @@
:clj (:require [net.cgrand.macrovich :as macros])) :clj (:require [net.cgrand.macrovich :as macros]))
(:refer-clojure :exclude [reduce reductions into count for partition str last keys vals min max]) (:refer-clojure :exclude [reduce reductions into count for partition str last keys vals min max])
(:require [#?(:clj clojure.core :cljs cljs.core) :as core] (:require [#?(:clj clojure.core :cljs cljs.core) :as core]
[net.cgrand.xforms.rfs :as rf])) [net.cgrand.xforms.rfs :as rf])
#?(:cljs (:import [goog.structs Queue])))
(macros/deftime (macros/deftime
@ -169,7 +170,7 @@
(defn maximum (defn maximum
([comparator] ([comparator]
(maximum comparator nil)) (maximum comparator nil))
([^java.util.Comparator comparator absolute-minimum] ([comparator absolute-minimum]
(reduce (rf/maximum comparator absolute-minimum)))) (reduce (rf/maximum comparator absolute-minimum))))
(def min (reduce rf/min)) (def min (reduce rf/min))
@ -284,7 +285,7 @@
(let [xform pad-or-xform] (let [xform pad-or-xform]
(fn [rf] (fn [rf]
(let [mxrf (multiplexable rf) (let [mxrf (multiplexable rf)
dq (java.util.ArrayDeque. n) dq #?(:clj (java.util.ArrayDeque. n) :cljs (Queue.))
barrier (volatile! n) barrier (volatile! n)
xform (comp (map #(if (identical? dq %) nil %)) xform)] xform (comp (map #(if (identical? dq %) nil %)) xform)]
(fn (fn
@ -292,11 +293,11 @@
([acc] (.clear dq) (rf acc)) ([acc] (.clear dq) (rf acc))
([acc x] ([acc x]
(let [b (vswap! barrier dec)] (let [b (vswap! barrier dec)]
(when (< b n) (.add dq (if (nil? x) dq x))) (when (< b n) (#?(:clj .add :cljs .enqueue) dq (if (nil? x) dq x)))
(if (zero? b) (if (zero? b)
; this transduce may return a reduced because of mxrf wrapping reduceds coming from rf ; this transduce may return a reduced because of mxrf wrapping reduceds coming from rf
(let [acc (transduce xform mxrf acc dq)] (let [acc (transduce xform mxrf acc #?(:clj dq :cljs (.getValues dq)))]
(dotimes [_ (core/min n step)] (.poll dq)) (dotimes [_ (core/min n step)] (#?(:clj .poll :cljs .dequeue) dq))
(vswap! barrier + step) (vswap! barrier + step)
acc) acc)
acc))))))) acc)))))))
@ -304,7 +305,7 @@
([n step pad xform] ([n step pad xform]
(fn [rf] (fn [rf]
(let [mxrf (multiplexable rf) (let [mxrf (multiplexable rf)
dq (java.util.ArrayDeque. n) dq #?(:clj (java.util.ArrayDeque. n) :cljs (Queue.))
barrier (volatile! n) barrier (volatile! n)
xform (comp (map #(if (identical? dq %) nil %)) xform)] xform (comp (map #(if (identical? dq %) nil %)) xform)]
(fn (fn
@ -312,18 +313,18 @@
([acc] (if (< @barrier n) ([acc] (if (< @barrier n)
(let [xform (comp cat (take n) xform) (let [xform (comp cat (take n) xform)
; don't use mxrf for completion: we want completion and don't want reduced-wrapping ; don't use mxrf for completion: we want completion and don't want reduced-wrapping
acc (transduce xform rf acc [dq pad])] acc (transduce xform rf acc [(.getValues dq) pad])]
(vreset! @barrier n) (vreset! @barrier n)
(.clear dq) (.clear dq)
acc) acc)
acc)) acc))
([acc x] ([acc x]
(let [b (vswap! barrier dec)] (let [b (vswap! barrier dec)]
(when (< b n) (.add dq (if (nil? x) dq x))) (when (< b n) (#?(:clj .add :cljs .enqueue) dq (if (nil? x) dq x)))
(if (zero? b) (if (zero? b)
; this transduce may return a reduced because of mxrf wrapping reduceds coming from rf ; this transduce may return a reduced because of mxrf wrapping reduceds coming from rf
(let [acc (transduce xform mxrf acc dq)] (let [acc (transduce xform mxrf acc #?(:clj dq :cljs (.getValues dq)))]
(dotimes [_ (min n step)] (.poll dq)) (dotimes [_ (min n step)] (#?(:clj .poll :cljs .dequeue) dq))
(vswap! barrier + step) (vswap! barrier + step)
acc) acc)
acc)))))))) acc))))))))

View file

@ -17,28 +17,37 @@
(declare str!) (declare str!)
(macros/usetime (macros/usetime
#? (:cljs
(defn ^:private cmp [f a b]
(let [r (f a b)]
(cond
(number? r) r
r -1
(f b a) 1
:else 0))))
(defn minimum (defn minimum
([comparator] ([comparator]
(minimum comparator nil)) (minimum comparator nil))
([^java.util.Comparator comparator absolute-maximum] ([#?(:clj ^java.util.Comparator comparator :cljs comparator) absolute-maximum]
(fn (fn
([] ::+∞) ([] ::+∞)
([x] (if (identical? ::+∞ x) ([x] (if (#?(:clj identical? :cljs keyword-identical?) ::+∞ x)
absolute-maximum absolute-maximum
x)) x))
([a b] (if (or (identical? ::+∞ a) (pos? (.compare comparator a b))) b a))))) ([a b] (if (or (#?(:clj identical? :cljs keyword-identical?) ::+∞ a) (pos? (#?(:clj .compare :cljs cmp) comparator a b))) b a)))))
(defn maximum (defn maximum
([comparator] ([comparator]
(maximum comparator nil)) (maximum comparator nil))
([^java.util.Comparator comparator absolute-minimum] ([#?(:clj ^java.util.Comparator comparator :cljs comparator) absolute-minimum]
(fn (fn
([] ::-∞) ([] ::-∞)
([x] (if (identical? ::-∞ x) ([x] (if (#?(:clj identical? :cljs keyword-identical?) ::-∞ x)
absolute-minimum absolute-minimum
x)) x))
([a b] (if (or (identical? ::-∞ a) (neg? (.compare comparator a b))) b a))))) ([a b] (if (or (#?(:clj identical? :cljs keyword-identical?) ::-∞ a) (neg? (#?(:clj .compare :cljs cmp) comparator a b))) b a)))))
(def min (minimum compare)) (def min (minimum compare))