(ns de.skamphausen.fingerprint
  (:require 
   [clojure.core.reducers :as r]
   [clojure.set           :as set]))

(defn- apply-fingerprint 
  "Given a fingerprinting function F and a seq of objects XS return a
  map with fingerprints as keys and a vector of all objects with that
  fingerprint.  Uses reducers/fold under the hood because
  fingerprinting can be slow.  You can pass an optional CHUNKSIZE to
  be used as first parameter (n) to fold.  XS is converted to a vector
  first to allow for parallization." 
  ([f xs]
     (apply-fingerprint f xs 512))
  ([f xs chunksize]
     (r/fold
      chunksize
      (r/monoid (partial merge-with concat) (constantly {}))
      (fn [acc item] 
        (let [fp (f item)]
          (assoc acc fp (conj (get acc fp []) item))))
      (vec xs))))

(defn- duplicate-sorter 
  "Given a map of fingerprints to objects FPMAP return a vector with
  unambiguous objects as first item and vectors of ambigous objects
  sharing the same fingerprint as the second item.  Unambiguous
  objects are returned as a map of fingerprint to object (no more
  collection of objects) while ambigous objects are returned as a
  vector of objects without fingerprints."  
  [fpmap]
  (reduce-kv
   (fn sort-item [acc k v]
     (if (= 1 (count v))
       (assoc-in  acc [0 k] (first v))
       (update-in acc [1] #(apply conj % v))
       ))
   [{} []]
   fpmap))

(defn- pair-by-fingerprint
  "Given two maps of fingerprint to object FPX and FPY return a vector
  of pairs, where pairs are vectors of the form [obj-x obj-y]."
  [fpmapx fpmapy]
  (let [fpx    (set (keys fpmapx))
        fpy    (set (keys fpmapy))
        common (set/intersection fpx fpy)
        onlyx  (set/difference   fpx fpy)
        onlyy  (set/difference   fpy fpx)]
    [(vec
      (for [common-fp common]
        [(get fpmapx common-fp) (get fpmapy common-fp)]))
     [(vec (map val (select-keys fpmapx onlyx)))
      (vec (map val (select-keys fpmapy onlyy)))]]))

(defn pair+
  "Given a fingerprinting function F and two seqs of objects XS and YS
  return 
  - the unambiguous pairs
  - ambiguous items in XS
  - ambiguous items in YS
  - unpaired  items in XS
  - unpaired  items in YS
  as a map."
  ([f xs ys]
     (pair+ f xs ys 512))
  ([f xs ys chunksize]
     (let [fpx (apply-fingerprint f xs chunksize)
           fpy (apply-fingerprint f ys chunksize)
           [uxs ambxs] (duplicate-sorter fpx)
           [uys ambys] (duplicate-sorter fpy)
           [prs [onlx only]] (pair-by-fingerprint uxs uys)]

       [prs
        [ambxs ambys] ;; ambiguous items
        [onlx only]])))

(defn pair
  "Given a fingerprinting function F and two seqs of objects XS and YS
  just return the unambiguous pairs of objects according to the
  fingerprinting function.  See also pair+ which returns ambiguous
  items, as well."
  [f xs ys chunksize]
  (first (pair+ f xs ys chunksize)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; examples
(defn classic [fp-fn data]
  (reduce
   (fn [acc x]
     (let [fp (fp-fn x)]
       (assoc acc fp (conj (get acc fp []) x))))
   {}
   data))

(comment
  (classic first ["abc" "def" "aaa"] )
  )


(defn quersumme [zahl]
  (loop [x zahl
         summe 0]
    (if (< x 9)
      (+ summe x)
      (recur (int (/ x 10)) 
             (+ summe (mod x 10))))))

(def sample-data (doall (repeatedly 10000000 (partial rand-int 10000))))

(comment
  (time (do (classic  quersumme sample-data) nil))

  (time (do (apply-fingerprint  quersumme sample-data) nil))
)

