General cleanup and performance review changes

- move to transducer for walking the tagged range boundary items
- remove use of core.match, after getting logic correct to reduce
  loading of unneeded library
- added docstrings to function
- added type hints and removed reflection warnings
This commit is contained in:
2026-01-15 20:34:01 -06:00
parent 9ffa5d1b97
commit 3f03936cfb
4 changed files with 361 additions and 335 deletions

View File

@@ -6,13 +6,17 @@
(defrecord IntInclusiveDiscreteValueRange
[^int start ^int end]
range/DiscreteValueRange
(abuts [_this other]
(or (= 1 (abs (- start (.end other))))
(= 1 (abs (- end (.start other))))))
(range/abuts? [_this other]
(or (= 1 (abs (- start (range/end other))))
(= 1 (abs (- end (range/start other))))))
(value-before [__this]
(dec start))
(if (= start Integer/MIN_VALUE)
Integer/MIN_VALUE
(dec start)))
(value-after [_this]
(inc end))
(if (= end Integer/MAX_VALUE)
Integer/MAX_VALUE
(inc end)))
(start [_this]
start)
(end [_this]
@@ -20,12 +24,14 @@
(range-type [_this]
:int-range-inclusive)
(union [this other]
(when-not (range/abuts this other)
(when-not (or (range/abuts? this other)
(or (<= start (range/start other) end)
(<= (range/start other) start (range/end other))))
(throw (ex-info "Cannot union non-abutting ranges" {})))
(range/->discrete-value-range (.range-type this)
(min start (.start other))
(max end (.end other))))
(range/->discrete-value-range (range/range-type this)
(min start (range/start other))
(max end (range/end other))))
Object
(toString [_this]
@@ -45,15 +51,15 @@
(is (= (int-range-inclusive 0 1)
(int-range-inclusive 0 1))))
(testing "abuts"
(is (= true (range/abuts (int-range-inclusive 0 1)
(int-range-inclusive 2 3))))
(is (= true (range/abuts (int-range-inclusive 1 1)
(int-range-inclusive 2 3))))
(is (= true (range/abuts (int-range-inclusive 4 7)
(int-range-inclusive 2 3))))
(is (= false (range/abuts (int-range-inclusive 4 7)
(int-range-inclusive 1 2)))))
(testing "abuts?"
(is (= true (range/abuts? (int-range-inclusive 0 1)
(int-range-inclusive 2 3))))
(is (= true (range/abuts? (int-range-inclusive 1 1)
(int-range-inclusive 2 3))))
(is (= true (range/abuts? (int-range-inclusive 4 7)
(int-range-inclusive 2 3))))
(is (= false (range/abuts? (int-range-inclusive 4 7)
(int-range-inclusive 1 2)))))
(testing "value-before"
(is (= 0 (range/value-before (int-range-inclusive 1 8)))))
(testing "value-after"
@@ -134,260 +140,260 @@
(deftest walk-range-boundaries-test
(testing "only filter-ranges"
(is (= #{}
(range/walk-range-boundaries [{:boundary-type :start
:range-source-type :filter-range
:value 1
:prev-value 0
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :filter-range
:value 4
:next-value 5
:type :int-range-inclusive}]))))
(into #{} (#'range/walk-tagged-range-boundaries-xf) [{:boundary-type :start
:range-source-type :filter-range
:value 1
:prev-value 0
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :filter-range
:value 4
:next-value 5
:type :int-range-inclusive}]))))
(testing "only source-ranges"
(is (= #{(int-range-inclusive 1 5)}
(range/walk-range-boundaries [{:boundary-type :start
:range-source-type :source-range
:value 1
:prev-value 0
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :source-range
:value 5
:next-value 6
:type :int-range-inclusive}])))
(into #{} (#'range/walk-tagged-range-boundaries-xf) [{:boundary-type :start
:range-source-type :source-range
:value 1
:prev-value 0
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :source-range
:value 5
:next-value 6
:type :int-range-inclusive}])))
(is (= #{(int-range-inclusive 1 5)
(int-range-inclusive 11 15)}
(range/walk-range-boundaries [{:boundary-type :start
:range-source-type :source-range
:value 1
:next-value 2
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :source-range
:value 5
:prev-value 4
:type :int-range-inclusive}
{:boundary-type :start
:range-source-type :source-range
:value 11
:next-value 12
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :source-range
:value 15
:prev-value 14
:type :int-range-inclusive}]))))
(into #{} (#'range/walk-tagged-range-boundaries-xf) [{:boundary-type :start
:range-source-type :source-range
:value 1
:next-value 2
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :source-range
:value 5
:prev-value 4
:type :int-range-inclusive}
{:boundary-type :start
:range-source-type :source-range
:value 11
:next-value 12
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :source-range
:value 15
:prev-value 14
:type :int-range-inclusive}]))))
(testing "filter-ranges and source-ranges are the same"
(is (= #{}
(range/walk-range-boundaries [{:boundary-type :start
:range-source-type :filter-range
:value 1
:prev-value 0
:type :int-range-inclusive}
{:boundary-type :start
:range-source-type :source-range
:value 1
:prev-value 0
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :source-range
:value 5
:next-value 6
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :filter-range
:value 5
:next-value 6
:type :int-range-inclusive}]))))
(into #{} (#'range/walk-tagged-range-boundaries-xf) [{:boundary-type :start
:range-source-type :filter-range
:value 1
:prev-value 0
:type :int-range-inclusive}
{:boundary-type :start
:range-source-type :source-range
:value 1
:prev-value 0
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :source-range
:value 5
:next-value 6
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :filter-range
:value 5
:next-value 6
:type :int-range-inclusive}]))))
(testing "filter-ranges before source-ranges"
(is (= #{(int-range-inclusive 11 15)}
(range/walk-range-boundaries [{:boundary-type :start
:range-source-type :filter-range
:value 1
:prev-value 0
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :filter-range
:value 5
:next-value 6
:type :int-range-inclusive}
{:boundary-type :start
:range-source-type :source-range
:value 11
:prev-value 10
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :source-range
:value 15
:next-value 16
:type :int-range-inclusive}]))))
(into #{} (#'range/walk-tagged-range-boundaries-xf) [{:boundary-type :start
:range-source-type :filter-range
:value 1
:prev-value 0
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :filter-range
:value 5
:next-value 6
:type :int-range-inclusive}
{:boundary-type :start
:range-source-type :source-range
:value 11
:prev-value 10
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :source-range
:value 15
:next-value 16
:type :int-range-inclusive}]))))
(testing "filter-ranges after source-ranges"
(is (= #{(int-range-inclusive 1 5)}
(range/walk-range-boundaries [{:boundary-type :start
:range-source-type :source-range
:value 1
:prev-value 0
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :source-range
:value 5
:next-value 6
:type :int-range-inclusive}
{:boundary-type :start
:range-source-type :filter-range
:prev-value 10
:value 11
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :filter-range
:value 15
:next-value 16
:type :int-range-inclusive}]))))
(into #{} (#'range/walk-tagged-range-boundaries-xf) [{:boundary-type :start
:range-source-type :source-range
:value 1
:prev-value 0
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :source-range
:value 5
:next-value 6
:type :int-range-inclusive}
{:boundary-type :start
:range-source-type :filter-range
:prev-value 10
:value 11
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :filter-range
:value 15
:next-value 16
:type :int-range-inclusive}]))))
(testing "single filter-range between source-range"
(is (= #{(int-range-inclusive 1 5)
(int-range-inclusive 11 20)}
(range/walk-range-boundaries [{:boundary-type :start
:range-source-type :source-range
:value 1
:prev-value 0
:type :int-range-inclusive}
{:boundary-type :start
:range-source-type :filter-range
:prev-value 5
:value 6
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :filter-range
:value 10
:next-value 11
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :source-range
:value 20
:next-value 21
:type :int-range-inclusive}]))))
(into #{} (#'range/walk-tagged-range-boundaries-xf) [{:boundary-type :start
:range-source-type :source-range
:value 1
:prev-value 0
:type :int-range-inclusive}
{:boundary-type :start
:range-source-type :filter-range
:prev-value 5
:value 6
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :filter-range
:value 10
:next-value 11
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :source-range
:value 20
:next-value 21
:type :int-range-inclusive}]))))
(testing "single filter-range between source-range but ends align"
(is (= #{(int-range-inclusive 1 5)}
(range/walk-range-boundaries [{:boundary-type :start
:range-source-type :source-range
:value 1
:prev-value 0
:type :int-range-inclusive}
{:boundary-type :start
:range-source-type :filter-range
:prev-value 5
:value 6
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :source-range
:value 10
:next-value 11
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :filter-range
:value 10
:next-value 11
:type :int-range-inclusive}]))))
(into #{} (#'range/walk-tagged-range-boundaries-xf) [{:boundary-type :start
:range-source-type :source-range
:value 1
:prev-value 0
:type :int-range-inclusive}
{:boundary-type :start
:range-source-type :filter-range
:prev-value 5
:value 6
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :source-range
:value 10
:next-value 11
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :filter-range
:value 10
:next-value 11
:type :int-range-inclusive}]))))
(testing "multiple filter-ranges between source-range"
(is (= #{(int-range-inclusive 1 5)
(int-range-inclusive 11 12)
(int-range-inclusive 20 20)}
(range/walk-range-boundaries [{:boundary-type :start
:range-source-type :source-range
:value 1
:prev-value 0
:type :int-range-inclusive}
{:boundary-type :start
:range-source-type :filter-range
:prev-value 5
:value 6
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :filter-range
:value 10
:next-value 11
:type :int-range-inclusive}
{:boundary-type :start
:range-source-type :filter-range
:prev-value 12
:value 13
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :filter-range
:value 19
:next-value 20
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :source-range
:value 20
:prev-value 19
:type :int-range-inclusive}]))))
(into #{} (#'range/walk-tagged-range-boundaries-xf) [{:boundary-type :start
:range-source-type :source-range
:value 1
:prev-value 0
:type :int-range-inclusive}
{:boundary-type :start
:range-source-type :filter-range
:prev-value 5
:value 6
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :filter-range
:value 10
:next-value 11
:type :int-range-inclusive}
{:boundary-type :start
:range-source-type :filter-range
:prev-value 12
:value 13
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :filter-range
:value 19
:next-value 20
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :source-range
:value 20
:prev-value 19
:type :int-range-inclusive}]))))
(testing "source range between filter-range"
(is (= #{}
(range/walk-range-boundaries [{:boundary-type :start
:range-source-type :filter-range
:prev-value 1
:value 1
:type :int-range-inclusive}
{:boundary-type :start
:range-source-type :source-range
:value 6
:prev-value 5
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :source-range
:value 10
:next-value 11
:type :int-range-inclusive}
{:boundary-type :start
:range-source-type :source-range
:value 13
:prev-value 12
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :source-range
:value 19
:next-value 20
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :filter-range
:value 20
:next-value 21
:type :int-range-inclusive}]))))
(into #{} (#'range/walk-tagged-range-boundaries-xf) [{:boundary-type :start
:range-source-type :filter-range
:prev-value 1
:value 1
:type :int-range-inclusive}
{:boundary-type :start
:range-source-type :source-range
:value 6
:prev-value 5
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :source-range
:value 10
:next-value 11
:type :int-range-inclusive}
{:boundary-type :start
:range-source-type :source-range
:value 13
:prev-value 12
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :source-range
:value 19
:next-value 20
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :filter-range
:value 20
:next-value 21
:type :int-range-inclusive}]))))
(testing "filter range overlaps source ranges"
(is (= #{(int-range-inclusive 1 4)
(int-range-inclusive 11 13)}
(range/walk-range-boundaries [{:boundary-type :start
:range-source-type :source-range
:value 1
:prev-value 0
:type :int-range-inclusive}
{:boundary-type :start
:range-source-type :filter-range
:prev-value 4
:value 5
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :source-range
:value 6
:next-value 7
:type :int-range-inclusive}
{:boundary-type :start
:range-source-type :source-range
:value 8
:prev-value 7
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :filter-range
:value 10
:next-value 11
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :source-range
:value 13
:next-value 14
:type :int-range-inclusive}])))))
(into #{} (#'range/walk-tagged-range-boundaries-xf) [{:boundary-type :start
:range-source-type :source-range
:value 1
:prev-value 0
:type :int-range-inclusive}
{:boundary-type :start
:range-source-type :filter-range
:prev-value 4
:value 5
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :source-range
:value 6
:next-value 7
:type :int-range-inclusive}
{:boundary-type :start
:range-source-type :source-range
:value 8
:prev-value 7
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :filter-range
:value 10
:next-value 11
:type :int-range-inclusive}
{:boundary-type :end
:range-source-type :source-range
:value 13
:next-value 14
:type :int-range-inclusive}])))))
(deftest difference-test
(testing "unary"