1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
//! Code for dyadic search functions
use std::{
cmp::Ordering,
collections::{HashMap, HashSet},
};
use ecow::{EcoVec, eco_vec};
use crate::{
Shape, Uiua, UiuaResult,
algorithm::{max_shape, validate_size},
array::*,
cowslice::cowslice,
fill::FillValue,
value::Value,
};
use super::{ArrayCmpSlice, FillContext};
impl Value {
/// Check which rows of another value are `member`s of this one
pub fn memberof(&self, elems: &Self, env: &Uiua) -> UiuaResult<Self> {
self.generic_bin_ref(
elems,
|a, b| a.memberof(b, env).map(Into::into),
|a, b| a.memberof(b, env).map(Into::into),
|a, b| a.memberof(b, env).map(Into::into),
|a, b| a.memberof(b, env).map(Into::into),
|a, b| a.memberof(b, env).map(Into::into),
|a, b| {
env.error(format!(
"Cannot look for members of {} array in {} array",
a.type_name(),
b.type_name(),
))
},
)
}
}
impl<T: ArrayValue> Array<T> {
/// Check which rows of another array are `member`s of this one
pub fn memberof(&self, elems: &Self, env: &Uiua) -> UiuaResult<Array<u8>> {
let of = self;
let mut arr = match elems.rank().cmp(&of.rank()) {
Ordering::Equal => {
let has_wildcard =
elems.data.iter().any(T::has_wildcard) || of.data.iter().any(T::has_wildcard);
let mut result_data = EcoVec::with_capacity(elems.row_count());
if has_wildcard {
for elem in elems.row_slices() {
let is_member = of
.row_slices()
.any(|row| ArrayCmpSlice(row) == ArrayCmpSlice(elem));
result_data.push(is_member as u8);
}
} else {
let mut members = HashSet::with_capacity(of.row_count());
for of in of.row_slices() {
members.insert(ArrayCmpSlice(of));
}
for elem in elems.row_slices() {
let is_member = members.contains(&ArrayCmpSlice(elem));
result_data.push(is_member as u8);
}
}
let shape: Shape = elems.shape.iter().cloned().take(1).collect();
Array::new(shape, result_data)
}
Ordering::Greater => {
if elems.row_count() == 0 {
return Ok(Array::new(elems.shape.clone(), EcoVec::new()));
}
let mut rows = Vec::with_capacity(elems.row_count());
for elem in elems.rows() {
rows.push(of.memberof(&elem, env)?);
}
Array::from_row_arrays(rows, env)?
}
Ordering::Less => {
if !of.shape.ends_with(&elems.shape) {
let shape = Shape::from(&of.shape[..of.shape.len() - elems.shape.len() - 1]);
let data = eco_vec![0; shape.elements()];
return Ok(Array::new(shape, data));
}
if of.rank() - elems.rank() == 1 {
if of.meta.is_sorted_up() {
// Binary search
if of.row_count() == 0 {
return Ok(false.into());
}
let elems_slice = ArrayCmpSlice(elems.data.as_slice());
let mut l = 0;
let mut r = of.row_count().saturating_sub(1);
let mut found = false;
while l <= r {
let mid = l + (r - l) / 2;
match ArrayCmpSlice(of.row_slice(mid)).cmp(&elems_slice) {
Ordering::Equal => {
found = true;
break;
}
Ordering::Less => l = mid + 1,
Ordering::Greater if mid == 0 => break,
Ordering::Greater => r = mid - 1,
}
}
found.into()
} else {
// Linear search
of.rows().any(|r| *elems == r).into()
}
} else {
let mut rows = Vec::with_capacity(of.row_count());
for of in of.rows() {
rows.push(of.memberof(elems, env)?);
}
Array::from_row_arrays(rows, env)?
}
}
};
arr.meta.flags.insert(ArrayFlags::BOOLEAN);
Ok(arr)
}
}
impl Value {
/// Get the `index of` the rows of this value in another
pub fn index_of(&self, haystack: &Value, env: &Uiua) -> UiuaResult<Value> {
self.generic_bin_ref(
haystack,
|a, b| a.index_of(b, env).map(Into::into),
|a, b| a.index_of(b, env).map(Into::into),
|a, b| a.index_of(b, env).map(Into::into),
|a, b| a.index_of(b, env).map(Into::into),
|a, b| a.index_of(b, env).map(Into::into),
|a, b| {
env.error(format!(
"Cannot look for indices of {} array in {} array",
a.type_name(),
b.type_name(),
))
},
)
}
}
impl<T: ArrayValue> Array<T> {
/// Get the `index of` the rows of this array in another
#[allow(clippy::mut_range_bound)]
pub fn index_of(&self, haystack: &Array<T>, env: &Uiua) -> UiuaResult<Array<f64>> {
let needle = self;
let default = env
.scalar_fill::<f64>()
.map(|fv| fv.value)
.unwrap_or(haystack.row_count() as f64);
Ok(match needle.rank().cmp(&haystack.rank()) {
Ordering::Equal => {
let has_wildcard = needle.data.iter().any(T::has_wildcard)
|| haystack.data.iter().any(T::has_wildcard);
let mut result_data = EcoVec::with_capacity(needle.row_count());
if has_wildcard {
for elem in needle.row_slices() {
let index = (haystack.row_slices())
.position(|row| ArrayCmpSlice(row) == ArrayCmpSlice(elem))
.map(|i| i as f64)
.unwrap_or(default);
result_data.push(index);
}
} else {
let mut cache = HashMap::with_capacity(haystack.row_count());
let mut next = 0;
'needle: for elem in needle.row_slices() {
let elem_key = ArrayCmpSlice(elem);
if let Some(i) = cache.get(&elem_key) {
result_data.push(*i as f64);
} else {
for i in next..haystack.row_count() {
let of_key = ArrayCmpSlice(haystack.row_slice(i));
cache.entry(of_key).or_insert(i);
if of_key == elem_key {
result_data.push(i as f64);
next = i + 1;
continue 'needle;
}
}
result_data.push(default);
}
}
}
let shape: Shape = needle.shape.iter().take(1).copied().collect();
Array::new(shape, result_data)
}
Ordering::Greater => {
if needle.row_count() == 0 {
return Ok(Array::new(needle.shape.clone(), EcoVec::new()));
}
let mut rows = Vec::with_capacity(needle.row_count());
for elem in needle.rows() {
rows.push(elem.index_of(haystack, env)?);
}
Array::from_row_arrays(rows, env)?
}
Ordering::Less => {
if !haystack.shape.ends_with(&needle.shape) {
let shape = Shape::from(
&haystack.shape[..haystack.shape.len() - needle.shape.len() - 1],
);
let elem = haystack.shape.row_count() as f64;
let data = eco_vec![elem; shape.elements()];
return Ok(Array::new(shape, data));
}
if haystack.rank() - needle.rank() == 1 {
if haystack.meta.is_sorted_up() {
// Binary search
let needle_slice = ArrayCmpSlice(needle.data.as_slice());
let mut l = 0;
let mut r = haystack.row_count();
while l < r {
let mid = l + (r - l) / 2;
if ArrayCmpSlice(haystack.row_slice(mid)) < needle_slice {
l = mid + 1;
} else {
r = mid;
}
}
let found = l < haystack.row_count()
&& ArrayCmpSlice(haystack.row_slice(l)) == needle_slice;
if found { l as f64 } else { default }.into()
} else {
// Linear search
(haystack
.row_slices()
.position(|r| {
r.len() == needle.data.len()
&& r.iter().zip(&needle.data).all(|(a, b)| a.array_eq(b))
})
.map(|i| i as f64)
.unwrap_or(default))
.into()
}
} else {
let mut rows = Vec::with_capacity(haystack.row_count());
for of in haystack.rows() {
rows.push(needle.index_of(&of, env)?);
}
Array::from_row_arrays(rows, env)?
}
}
})
}
}
impl Value {
/// Try to `find` this value in another
pub fn find(&self, searched: &Self, env: &Uiua) -> UiuaResult<Self> {
self.generic_bin_ref(
searched,
|a, b| a.find(b, env).map(Into::into),
|a, b| a.find(b, env).map(Into::into),
|a, b| a.find(b, env).map(Into::into),
|a, b| a.find(b, env).map(Into::into),
|a, b| a.find(b, env).map(Into::into),
|a, b| {
env.error(format!(
"Cannot find {} in {} array",
a.type_name(),
b.type_name()
))
},
)
}
/// Try to `mask` this value in another
pub fn mask(&self, searched: &Self, env: &Uiua) -> UiuaResult<Self> {
self.generic_bin_ref(
searched,
|a, b| a.mask(b, env),
|a, b| a.mask(b, env),
|a, b| a.mask(b, env),
|a, b| a.mask(b, env),
|a, b| a.mask(b, env),
|a, b| {
env.error(format!(
"Cannot mask {} in {} array",
a.type_name(),
b.type_name()
))
},
)
}
}
impl<T: ArrayValue> Array<T> {
/// Try to `find` this array in another
pub fn find(&self, haystack: &Self, env: &Uiua) -> UiuaResult<Array<u8>> {
let needle = self;
let mut haystack = haystack;
let mut local_searched: Self;
let any_dim_greater = (needle.shape.iter().rev())
.zip(haystack.shape.iter().rev())
.any(|(a, b)| a > b);
if needle.rank() > haystack.rank() || any_dim_greater {
// Fill
match env.scalar_fill() {
Ok(fill) => {
let target_shape = max_shape(&haystack.shape, &needle.shape);
local_searched = haystack.clone();
local_searched.fill_to_shape(&target_shape, fill);
haystack = &local_searched;
}
Err(_) => {
let data = cowslice![0; haystack.element_count()];
let mut arr = Array::new(haystack.shape.clone(), data);
arr.meta.flags.insert(ArrayFlags::BOOLEAN);
return Ok(arr);
}
}
}
// Pad the shape of the searched-for array
let mut searched_for_shape = needle.shape.clone();
while searched_for_shape.len() < haystack.shape.len() {
searched_for_shape.prepend(1);
}
// Calculate the pre-padded output shape
let temp_output_shape: Shape = haystack
.shape
.iter()
.zip(&searched_for_shape)
.map(|(s, f)| s + 1 - f)
.collect();
let elem_count = validate_size::<T>(temp_output_shape.iter().copied(), env)?;
let mut data = EcoVec::from_elem(0, elem_count);
let data_slice = data.make_mut();
if haystack.rank() == 1 {
// Fast path for rank-1 arrays
if let Some(first) = needle.data.first() {
// The jump is the number of elements to skip forward on a failed partial match
let jump = (needle.data.iter())
.skip(1)
.position(|d| d.array_eq(first))
.map_or(needle.data.len(), |i| i + 1);
let mut i = 0;
while i < haystack.data.len() + 1 - needle.data.len() {
if haystack.data[i].array_eq(first) {
if needle.data[1..]
.iter()
.zip(&haystack.data[i + 1..])
.all(|(a, b)| a.array_eq(b))
{
// All elements match
data_slice[i] = 1;
i += jump;
} else {
// Mismatch
i += 1;
}
} else {
// Mismatch
i += 1;
}
}
}
} else {
let mut corner = vec![0; haystack.shape.len()];
let mut curr = vec![0; haystack.shape.len()];
let mut k = 0;
if haystack.shape.iter().all(|&d| d > 0) {
'windows: loop {
// Reset curr
for i in curr.iter_mut() {
*i = 0;
}
// Search the window whose top-left is the current corner
'items: loop {
// Get index for the current item in the haystack
let mut searched_index = 0;
let mut stride = 1;
for ((c, i), s) in corner.iter().zip(&curr).zip(&haystack.shape).rev() {
searched_index += (*c + *i) * stride;
stride *= s;
}
// Get index for the current item in the searched-for array
let mut search_for_index = 0;
let mut stride = 1;
for (i, s) in curr.iter().zip(&searched_for_shape).rev() {
search_for_index += *i * stride;
stride *= s;
}
// Compare the current items in the two arrays
let same = if let Some(searched_for) = needle.data.get(search_for_index) {
haystack.data[searched_index].array_eq(searched_for)
} else {
false
};
if !same {
data_slice[k] = 0;
k += 1;
break;
}
// Go to the next item
for i in (0..curr.len()).rev() {
if curr[i] == searched_for_shape[i] - 1 {
curr[i] = 0;
} else {
curr[i] += 1;
continue 'items;
}
}
data_slice[k] = 1;
k += 1;
break;
}
// Go to the next corner
for i in (0..corner.len()).rev() {
if corner[i] == haystack.shape[i] - searched_for_shape[i] {
corner[i] = 0;
} else {
corner[i] += 1;
continue 'windows;
}
}
break;
}
}
}
let mut arr = Array::new(temp_output_shape, data);
arr.fill_to_shape(
&haystack.shape[..searched_for_shape.len()],
FillValue::new(0, None),
);
arr.validate();
arr.meta.flags.insert(ArrayFlags::BOOLEAN);
Ok(arr)
}
/// Try to `mask` this array in another
pub fn mask(&self, haystack: &Self, env: &Uiua) -> UiuaResult<Value> {
let needle = self;
if needle.rank() > haystack.rank() {
return Err(env.error(format!(
"Cannot look for rank {} array in rank {} array",
needle.rank(),
haystack.rank()
)));
}
if (needle.shape.iter().rev())
.zip(haystack.shape.iter().rev())
.any(|(n, h)| n > h)
{
return Ok(Array::new(
haystack.shape.clone(),
eco_vec![0u8; haystack.element_count()],
)
.into());
}
let mut result_data = eco_vec![0.0; haystack.element_count()];
let res = result_data.make_mut();
if haystack.rank() == 1 {
// Fast path for rank-1 arrays
if !needle.data.is_empty() {
let mut curr = 0;
let mut i = 0;
while i < haystack.data.len() + 1 - needle.data.len() {
if (needle.data.iter())
.zip(&haystack.data[i..])
.all(|(a, b)| a.array_eq(b))
{
curr += 1;
for j in i..i + needle.data.len() {
res[j] = curr as f64;
}
i += needle.data.len();
} else {
i += 1;
}
}
}
} else {
let needle_data = needle.data.as_slice();
let mut needle_shape = needle.shape.clone();
while needle_shape.len() < haystack.shape.len() {
needle_shape.prepend(1);
}
let needle_elems = needle.element_count();
let mut curr = Vec::new();
let mut offset = Vec::new();
let mut sum = vec![0; needle_shape.len()];
let mut match_num = 0u64;
for i in 0..res.len() {
// Check if the needle matches the haystack at the current index
haystack.shape.flat_to_dims(i, &mut curr);
let mut matches = true;
for j in 0..needle_elems {
needle_shape.flat_to_dims(j, &mut offset);
for ((c, o), s) in curr.iter().zip(&offset).zip(&mut sum) {
*s = *c + *o;
}
if (haystack.shape.dims_to_flat(&sum))
.is_none_or(|k| res[k] > 0.0 || !needle_data[j].array_eq(&haystack.data[k]))
{
matches = false;
break;
}
}
// Fill matches
if matches {
match_num += 1;
for j in 0..needle_elems {
needle_shape.flat_to_dims(j, &mut offset);
for ((c, o), s) in curr.iter().zip(&offset).zip(&mut sum) {
*s = *c + *o;
}
let k = haystack.shape.dims_to_flat(&sum).unwrap();
res[k] = match_num as f64;
}
}
}
}
let mut val: Value = Array::new(haystack.shape.clone(), result_data).into();
val.try_shrink();
Ok(val)
}
}