 
 
 (defn- set-positions
-  "Positions in an alternating layer are used to calculate the order of the next layer.
+  "Positions in an ordered layer are used to calculate the order of the next layer.
   ESK's description of the position algorithm is almost willfully circuitous and obtuse,
-  so here's a simplified description: An item's position in an alternating layer is the
+  so here's a simplified description: An item's position in an ordered layer is the
   sum of the size of all previous items, plus 1, where the size of a segment container
   is the number of segments it contains, and the size of a node is 1.
 
   Also, it doesn't really matter what seed you choose for the initial value of the sum. I
   chose -1, which is implied by the description in ESK."
   [layer]
-  (loop [alt (:alternating layer)
+  (loop [ordered (:ordered layer)
          current-position -1
          positions {}]
-    (if (empty? alt)
+    (if (empty? ordered)
       (assoc layer :positions positions)
-      (let [item (first alt)
-            ;; When (count alt) is odd, item is a SegmentContainer
-            c-p (if (odd? (count alt))
+      (let [item (first ordered)
+            c-p (if (instance? SegmentContainer item)
                   (+ current-position (count (:segments item)))
                   (inc current-position))]
-        (recur (rest alt) c-p (assoc positions item (inc current-position)))))))
+        (recur (rest ordered) c-p (assoc positions item (inc current-position)))))))
 
 
 (defn- set-qs-non-qs

              ordered-graph (-> (if reverse?
                                  (reverse-graph sparse-graph)
                                  sparse-graph)
                                (update-in [:layers 0] assoc :alternating alternating)
                                order-graph-once)]
          (println c (:crossings ordered-graph) (-> ordered-graph :layers peek :alternating))
          (recur (-> ordered-graph :layers peek :alternating)
                 (conj orderings (if reverse? (reverse-graph ordered-graph) ordered-graph))))))))

  equivalent to the position in the previous layer, so we just use that), and we merge
  the two lists into a single ordering based on these measures."
  [layer next-layer]
  (let [positions (:positions layer)
        measures (:measures next-layer)
        ns (sort-by #(get measures %) (:non-qs next-layer))
        ss (->> (:minus-ps layer)
                (filter #(instance? SegmentContainer %))
                (sort-by #(get positions %)))
        minus-qs (loop [nodes ns
                        segments ss
                        pos positions
                        ord []]
                   (if (or (empty? nodes) (empty? segments))
                     ;; ESK's algorithm doesn't specify what to do with leftover things
                     ;; I think this is because it doesn't take into account nodes that don't have
                     ;; parents in layers > 0. In any case, at most one of nodes or segments
                     ;; will be non-empty
                     (-> ord (into nodes) (into segments))
                     (let [node-1 (first nodes)
                           seg-1 (first segments)
                           node-measure (get measures node-1)
                           seg-position (get pos seg-1)
                           node-first (<= node-measure seg-position)
                           seg-first (>= node-measure (+ seg-position (count (:segments seg-1))))]
                       (cond node-first (recur (rest nodes) segments pos (conj ord node-1))
                             seg-first (recur nodes (rest segments) pos (conj ord seg-1))
                             :else (let [k (.ceil js/Math (- node-measure seg-position))
                                         s-1 (update-in seg-1 [:segments] rrb/subvec 0 k)
                                         s-2 (update-in seg-1 [:segments] rrb/subvec k)]
                                     (recur
                                      (rest nodes)
                                      (cons s-2 segments)
                                      (assoc pos s-2 (+ (get pos seg-1) 1))
                                      (into ord [s-1 node-1])))))))]
    (assoc next-layer :minus-qs minus-qs)))


(defn add-qs
  "Step 4 of ESK - takes the results of step 3, which doesn't include the q-nodes, and adds them,
  splitting their segment containers in the process"
  [next-layer]
  (let [qs (:qs next-layer)
        flat (->> (:minus-qs next-layer)
                  (mapcat #(if (instance? SegmentContainer %)
                             (:segments %)
                             [%]))
