   (let [result (BitSet.)]
     (dobits [i bitset]
       (when (predicate i)
-	(.set result i)))
+        (.set result i)))
     result))
 
 (defn to-bitset
-  ""
+  "Converts hashset to a BitSet. Elements are associated with indices
+  as given by element-vector (i.e. the order in this vector is the
+  order of the elements in the resulting BitSet."
   [element-vector hashset]
-  (let [#^BitSet bs (BitSet. (count element-vector))]
+  (let [^BitSet bs (BitSet. (count element-vector))]
     (doseq [pair (indexed element-vector)
-	    :when (contains? hashset (second pair))]
+            :when (contains? hashset (second pair))]
       (.set bs (first pair)))
     bs))
 
 (defn to-hashset
-  ""
-  [element-vector #^BitSet bitset]
-  (loop [pos (int (.nextSetBit bitset 0))
-	 result #{}]
+  "Converts given bitset to a set, where element-vector gives the
+  actual objects to be collected, i.e. element v at position p in
+  element-vector is included in the result iff p is set in bitset."
+  [element-vector, ^BitSet bitset]
+  (loop [pos (int (.nextSetBit bitset 0)),
+         result #{}]
     (if (== -1 pos)
       result
       (recur (.nextSetBit bitset (inc pos))
-	     (conj result (nth element-vector pos))))))
+             (conj result (nth element-vector pos))))))
 
 (defn to-binary-matrix
-  ""
+  "Converts the incidence-relation to a binary matrix (in the sense of
+  Java) filled with 1 and 0."
   [object-vector attribute-vector incidence-relation]
   (let [incidence-matrix (make-array Integer/TYPE (count object-vector) (count attribute-vector))]
     (dotimes [obj-idx (count object-vector)]
-      (let [#^ints row (aget #^objects incidence-matrix obj-idx)]
-	(dotimes [att-idx (count attribute-vector)]
-	  (aset row att-idx
-		(if (contains? incidence-relation [(nth object-vector obj-idx)
-						   (nth attribute-vector att-idx)])
-		  (int 1)
-		  (int 0))))))
+      (let [^ints row (aget ^objects incidence-matrix obj-idx)]
+        (dotimes [att-idx (count attribute-vector)]
+          (aset row att-idx
+                (if (contains? incidence-relation [(nth object-vector obj-idx)
+                                                   (nth attribute-vector att-idx)])
+                  (int 1)
+                  (int 0))))))
     incidence-matrix))
 
 (defn to-binary-context

     ~@body))

(defn bitwise-object-derivation
  ""
  [incidence-matrix object-count attribute-count #^BitSet bitset]
  (let [#^BitSet derived-attributes (BitSet. attribute-count)]
    (dotimes [att (int attribute-count)]
      (if (forall-in-bitset [obj bitset]
	    (== 1 (deep-aget ints incidence-matrix obj att)))
	(.set derived-attributes att)))
    derived-attributes))

(defn bitwise-attribute-derivation
  ""
  [incidence-matrix object-count attribute-count #^BitSet bitset]
  (let [#^BitSet derived-objects (BitSet. object-count)]
    (dotimes [obj (int object-count)]
      (if (forall-in-bitset [att bitset]
	    (== 1 (deep-aget ints incidence-matrix obj att)))
	(.set derived-objects obj)))
    derived-objects))

nil

  (:use [clojure.contrib.seq-utils :only (indexed)])
  (:use [conexp.fca.contexts :only (objects attributes incidence)]))

(set! *warn-on-reflection* true)

;;; Helpers to convert to and from BitSets

