Advertisement
Guest User

performance improvements?

a guest
Mar 25th, 2011
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (defn- type-matcher-1
  2.   "Returns a matcher for elements Foo, !Foo, Foo!, !Foo!."
  3.   [g c]
  4.   (fn [x]
  5.     (let [[neg qname exact] (split-name (name c))
  6.       type (resolve-aec g qname)]
  7.       (xor (not (empty? neg))
  8.        (if (not (empty? exact))
  9.          (= type (m1class x))
  10.          (instance? type x))))))
  11.  
  12. (defn- type-matcher
  13.   "Returns a matcher for either nil, !Foo!, or [Foo Bar! !Baz]."
  14.   [g cls]
  15.   (cond
  16.    (nil? cls) identity
  17.    (qname? cls) (type-matcher-1 g cls)
  18.    (coll? cls) (let [t-matchers (map #(type-matcher-1 g %) cls)]
  19.          (fn [arg]
  20.            ;; TODO: support of arbitrary logical op
  21.            (reduce #(or %1 %2)
  22.                ((apply juxt t-matchers)
  23.                 arg))))
  24.    :else (RuntimeException. "Don't know how to create a type matcher for" cls)))
  25.  
  26. (defn- direction-matcher
  27.   [dir]
  28.   (cond
  29.    (or (nil? dir) (= dir EdgeDirection/INOUT)) identity
  30.    (= dir EdgeDirection/OUT) (fn [i] (normal-edge? i))
  31.    (= dir EdgeDirection/IN) (fn [i] (not (normal-edge? i)))
  32.    :else (throw (RuntimeException. "Unknown direction " dir))))
  33.  
  34. (defn first-inc
  35.   "Returns the first incidence in iseq of v.
  36. May be restricted by cls and dir."
  37.   ([^Vertex v]
  38.      (first-inc v nil nil))
  39.   ([^Vertex v cls]
  40.      (first-inc v cls nil))
  41.   ([^Vertex v cls dir]
  42.      (let [tm (type-matcher v cls)
  43.        dm (direction-matcher dir)]
  44.        (loop [i (.getFirstIncidence v)]
  45.      (if (or (nil? i) (and (dm i) (tm i)))
  46.        i
  47.        (recur (.getNextIncidence i)))))))
  48.  
  49. (defn next-inc
  50.   "Returns the incidence following e in the current vertex's iseq.
  51. May be restricted by cls and dir."
  52.   ([^Edge e]
  53.      (next-inc e nil nil))
  54.   ([^Edge e cls]
  55.      (next-inc e cls nil))
  56.   ([^Edge e cls dir]
  57.      (let [tm (type-matcher e cls)
  58.        dm (direction-matcher dir)]
  59.        (loop [i (.getNextIncidence e)]
  60.      (if (or (nil? i) (and (dm i) (tm i)))
  61.        i
  62.        (recur (.getNextIncidence i)))))))
  63.  
  64.  
  65. (defmulti iseq
  66.   "Returns a lazy sequence of v's incidences."
  67.   (fn [c & _ ] (class c)))
  68.  
  69. (defmethod iseq Vertex
  70.   ([v]
  71.      (iseq v nil nil))
  72.   ([v cls]
  73.      (iseq v cls nil))
  74.   ([v cls dir]
  75.      (let [f (first-inc v cls dir)]
  76.        (if f
  77.      (lazy-cat (iseq f cls dir))
  78.      []))))
  79.  
  80. (defmethod iseq Edge
  81.   ([e]
  82.      (iseq e nil nil))
  83.   ([e cls]
  84.      (iseq e cls nil))
  85.   ([e cls dir]
  86.      (let [n (next-inc e cls dir)]
  87.        (lazy-cat [e] (and n (iseq n cls dir))))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement