Skip to content

Commit b072fd8

Browse files
committed
Move equality functions here from GLUtilities
1 parent 1ee815e commit b072fd8

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "SignalIndices"
22
uuid = "18f53e89-494d-4131-905c-653e8ada6cd8"
33
authors = ["Galen Lynch <galen@galenlynch.com>"]
4-
version = "0.1.0"
4+
version = "0.1.1"
55

66
[deps]
77
IterTools = "c8e1da08-722c-5040-9ed9-7db0dc04731e"

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ maxima_indices = local_extrema(signal, >)
4444
minima_indices = local_extrema(signal, <)
4545

4646
# Fast histogram for regularly-spaced bins
47-
counts = glhist(values, bin_edges)
47+
counts = uniformhist(values, bin_edges)
4848
```
4949

5050
### Array Utilities
@@ -59,6 +59,12 @@ idx = find_closest(array, target)
5959
non_colliding = filter_no_collisions(events_a, events_b, min_gap)
6060
duplicates = find_not_unique(array)
6161

62+
# Equality and comparison
63+
allsame([1, 1, 1]) # true — all elements equal
64+
allsame(length, [1,2], [3,4]) # true — all have same length
65+
anyeq(3, [1, 2, 3]) # true — element found in collection
66+
absdiff(5, 3) # 2 — absolute difference (unsigned-safe)
67+
6268
# Iterator utilities
6369
result = sum(skipnothing([1, nothing, 2, nothing, 3])) # 6
6470
```
@@ -119,7 +125,7 @@ Pkg.develop(path="/path/to/SignalIndices")
119125
- `indices_above_thresh(arr, threshold)` - Contiguous regions above threshold
120126
- `local_extrema(signal, comp)` - Find local maxima/minima
121127
- `find_local_extrema(signal, start; findmax, right_on_ties)` - Hill-climb to extremum
122-
- `glhist(values, bins)` / `glhist!(counts, values, bins)` - Fast histogram
128+
- `uniformhist(values, bins)` / `uniformhist!(counts, values, bins)` - Fast histogram
123129
- `filter_no_collisions(as, bs, radius)` - Remove elements too close to reference
124130
- `window_counts(times, duration)` - Count events in sliding window
125131

@@ -138,6 +144,9 @@ Pkg.develop(path="/path/to/SignalIndices")
138144
- `trailing_zeros_idx(arr)` - Last non-zero index
139145
- `clipsize!(vec, n)` - Resize and shrink capacity
140146
- `rev_view(vec)` - Reversed view
147+
- `allsame(collection)` / `allsame(f, args...)` - Check all elements (or mapped values) are equal
148+
- `anyeq(element, collection)` - Check if element exists in collection
149+
- `absdiff(a, b)` - Absolute difference (safe for unsigned integers)
141150
- `skipoftype(T, itr)` / `skipnothing(itr)` - Skip elements by type
142151

143152
## Citing

src/SignalIndices.jl

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ export
7474
clipsize!,
7575
find_not_unique,
7676
skipoftype,
77-
skipnothing
77+
skipnothing,
78+
allsame,
79+
anyeq,
80+
absdiff
7881

7982
div_type(::Type{N}) where {N<:AbstractFloat} = N
8083
div_type(::Type{N}) where {N<:Integer} = Float64
@@ -1108,4 +1111,28 @@ function indices_above_thresh(arr, thr)
11081111
return out
11091112
end
11101113

1114+
allsame(f::Function, first) = true
1115+
1116+
function allsame(f::Function, first, second, others...)
1117+
f(first) == f(second) && allsame(f, second, others...)
1118+
end
1119+
1120+
allsame(first, args...) = allsame(identity, first, args...)
1121+
1122+
function allsame(a::AbstractArray)
1123+
isempty(a) && return true
1124+
@inbounds first = a[1]
1125+
for e in a[2:end]
1126+
if !isequal(e, first)
1127+
return false
1128+
end
1129+
end
1130+
true
1131+
end
1132+
1133+
anyeq(el, iter) = any(a -> a == el, iter)
1134+
1135+
@inline absdiff(a::Unsigned, b::Unsigned) = ifelse(a <= b, b - a, a - b)
1136+
@inline absdiff(a::Signed, b::Signed) = abs(a - b)
1137+
11111138
end

0 commit comments

Comments
 (0)