You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/ref/bin.md
+79-18Lines changed: 79 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,20 +16,48 @@ x bin y bin[x;y]
16
16
x binr y binr[x;y]
17
17
```
18
18
19
+
## Lists
20
+
19
21
Where
20
22
21
23
-`x` is a sorted list
22
-
-`y` is a list or atom of exactly the same type (no type promotion)
24
+
-`y` is an atom of exactly the same type (no type promotion)
23
25
24
-
returns the index of the _last_ item in `x` which is ≤`y`. The result is `-1` for `y` less than the first item of `x`.
26
+
returns the index of the _last_ item in `x` which is ≤`y`. The result is `-1` for `y` less than the first item of `x`. If `x` is a simple list, `bin` is [atomic](../basics/atomic.md) in `y`. (For higher ranks of either argument, `bin` works the same way as [`?` (Find)](find.md/#type-specific).)
25
27
`binr`_binary search right_, introduced in V3.0 2012.07.26, gives the index of the _first_ item in `x` which is ≥`y`.
26
28
27
-
They use a binary-search algorithm, which is generally more efficient on large data than the linear-search algorithm used by `?` ([Find](find.md)).
29
+
```q
30
+
q)0 2 4 6 8 10 bin 5
31
+
2
32
+
q)0 2 4 6 8 10 bin -10 0 4 5 6 20
33
+
-1 0 2 2 3 5
34
+
35
+
q)0 1 1 2 bin 0 1 2
36
+
0 2 3
37
+
q)0 1 1 2 binr 0 1 2
38
+
0 1 3
39
+
```
40
+
41
+
`bin` uses a binary search algorithm, which is generally more efficient on large data than the linear-search algorithm used by [`?` (Find)](find.md).
28
42
29
-
The items of `x`should be sorted ascending although `bin` does not verify this property.
43
+
The items of `x`must be sorted ascending although `bin` does not verify this property.
30
44
31
45
!!! danger "If `x` is not sorted the result is undefined."
32
46
47
+
`bin` can be also used if `x` is a dictionary with its values sorted.
48
+
49
+
```q
50
+
q)(`a`b`c!0 2 4) bin -1 3
51
+
``b
52
+
```
53
+
54
+
Non-simple lists can also be used. In this case, items are lexicographically sorted.
55
+
56
+
```q
57
+
q)("apple";"banana";"coffee") bin ("anise";"berry";"curry")
58
+
-1 1 2
59
+
```
60
+
33
61
The result `r` can be interpreted as follows: for an atom `y`, `r` is an integer atom whose value is either a valid index of `x` or `-1`. In general:
34
62
35
63
```txt
@@ -44,31 +72,64 @@ and
44
72
r[j]=x bin y[j] for all j in index of y
45
73
```
46
74
47
-
Essentially `bin`gives a half-open interval on the left.
75
+
`bin`is the function used in [`aj`](aj.md) and [`lj`](lj.md).
48
76
49
-
`bin` and `binr` are right-atomic: their results have the same count as `y`.
77
+
`bin` and `binr` are [multithreaded primitives](../kb/mt-primitives.md).
50
78
51
-
`bin` also operates on tuples and table columns and is the function used in [`aj`](aj.md) and [`lj`](lj.md).
79
+
## Tables
52
80
53
-
`bin` and `binr` are [multithreaded primitives](../kb/mt-primitives.md).
81
+
Where
82
+
83
+
-`x` is a table of `n` columns
84
+
-`y` is a table row with the same schema (e.g. a list with `n` elements or a dictionary with the same keys as the columns of `x`)
85
+
86
+
returns the index of the last row of `x` for which
87
+
88
+
- the first `n-1` values each match the first `n-1` values of `y`, and
89
+
- the last value is not greater than the last value of `y`.
90
+
91
+
(For higher ranks, see the examples below as well as the documentation for [`?` (Find)](find.md/#type-specific).)
92
+
93
+
If no items match the criteria, either because there are no rows that match in the first `n-1` columns, or because the last value is smaller than the last value in the first such row, `0N` is returned.
54
94
55
95
```q
56
-
q)0 2 4 6 8 10 bin 5
57
-
2
58
-
q)0 2 4 6 8 10 bin -10 0 4 5 6 20
59
-
-1 0 2 2 3 5
96
+
q)t:([]a:`p`p`p`q`q`q;b:0 2 4 0 2 4)
97
+
q)t bin `a`b!(`p;3)
98
+
1
99
+
q)t bin ([]a:`q;b:-1 1 3 5)
100
+
0N 3 4 5
101
+
q)t bin `a`b!(`r;2)
102
+
0N
60
103
```
61
104
62
-
If the left argument items are not distinct the result is not the same as would be obtained with `?`:
105
+
To use `bin` with a table, the last column needs not be sorted overall, but it needs to be sorted within the equivalence classes defined by the first `n-1` columns (as shown in the previous example).
106
+
107
+
`bin` can also be used with keyed tables. Here, `y` needs to contain all value columns, and it is the keys that are returned (as a table).
`bin` detects the special case of three columns with the third column having a sorted attribute. The search is initially constrained by the first column, then by the sorted third column, and then by a linear search through the remaining second column. The performance difference is visible in this example:
0 commit comments