40 Commits

Author SHA1 Message Date
yujiri8 dae516dbdf improve documentation of std.sort.*Context functions (#16145) 2023-06-27 00:51:06 -07:00
Ali Chraghi 6bd5479306 std.sort.block: add safety check for lessThan return value 2023-06-26 17:50:10 -07:00
Niles Salter 7d511d6428 [heapsort] Protect against integer overflow
(Firstly, I changed `n` to `b`, as that is less confusing. It's not a length, it's a right boundary.)

The invariant maintained is `cur < b`. In the worst case `2*cur + 1` results in a maximum of `2b`. Since `2b` is not guaranteed to be lower than `maxInt`, we have to add one overflow check to `siftDown` to make sure we avoid undefined behavior.

LLVM also seems to have a nicer time compiling this version of the function. It is about 2x faster in my tests (I think LLVM was stumped by the `child += @intFromBool` line), and adding/removing the overflow check has a negligible performance difference on my machine. Of course, we could check `2b <= maxInt` in the parent function, and dispatch to a version of the function without the overflow check in the common case, but that probably is not worth the code size just to eliminate a single instruction.
2023-06-22 17:32:28 +00:00
Eric Joldasov 50339f595a all: zig fmt and rename "@XToY" to "@YFromX"
Signed-off-by: Eric Joldasov <bratishkaerik@getgoogleoff.me>
2023-06-19 12:34:42 -07:00
Niles Salter 700ea694b2 Fix pdqSort+heapSort for ranges besides 0..len (#15982) 2023-06-13 16:55:58 -04:00
Ali Chraghi 3db3cf7790 std.sort: add pdqsort and heapsort 2023-05-23 17:55:59 -07:00
IntegratedQuantum 6e05620117 Document the sorting order in std.sort. 2023-05-17 18:26:49 -07:00
Andrew Kelley 6261c13731 update codebase to use @memset and @memcpy 2023-04-28 13:24:43 -07:00
Roman Frołow 0787b11f19 naming: mid for index and mid_item for item 2023-03-21 15:12:13 +02:00
Alexis Brodeur 98dd041d53 Relax std.sort.binarySearch requirements
Forcing the key to be of the same type as the sorted items used during
the search is a valid use case.

There, however, exists some cases where the key and the items are of
heterogeneous types, like searching for a code point in ordered ranges
of code points:

```zig
const CodePoint = u21;
const CodePointRange = [2]CodePoint;

const valid_ranges = &[_]CodePointRange{
    // an ordered array of ranges
};

fn orderCodePointAndRange(
    context: void,
    code_point: CodePoint,
    range: CodePointRange
) std.math.Order {
    _ = context;
    if (code_point < range[0]) {
        return .lt;
    }
    if (code_point > range[1]) {
        return .gt;
    }
    return .eq;
}

fn isValidCodePoint(code_point: CodePoint) bool {
    return std.sort.binarySearch(
        CodePointRange,
        code_point,
        valid_ranges,
        void,
        orderCodePointAndRange
    ) != null;
}
```

It is so expected that `std.sort.binarySearch` should therefore support
both homogeneous and heterogeneous keys.
2023-02-21 12:28:43 -05:00
Andrew Kelley aeaef8c0ff update std lib and compiler sources to new for loop syntax 2023-02-18 19:17:21 -07:00
Andrew Kelley 6e49ba77f3 std: add sort method to ArrayHashMap and MultiArrayList
This also adds `std.sort.sortContext` and
`std.sort.insertionSortContext` which are more advanced methods that
allow overriding the `swap` method. The former calls the latter for now
because reworking the main sort implementation is a big task that can be
done later without any changes to the API.
2022-03-10 13:13:17 -05:00
Ominitay c1a5ff34f3 std.rand: Refactor Random interface
These changes have been made to resolve issue #10037. The `Random`
interface was implemented in such a way that causes significant slowdown
when calling the `fill` function of the rng used.

The `Random` interface is no longer stored in a field of the rng, and is
instead returned by the child function `random()` of the rng. This
avoids the performance issues caused by the interface.
2021-10-27 16:07:48 -04:00
Andrew Kelley 6115cf2240 migrate from std.Target.current to @import("builtin").target
closes #9388
closes #9321
2021-10-04 23:48:55 -07:00
Andrew Kelley d29871977f remove redundant license headers from zig standard library
We already have a LICENSE file that covers the Zig Standard Library. We
no longer need to remind everyone that the license is MIT in every single
file.

Previously this was introduced to clarify the situation for a fork of
Zig that made Zig's LICENSE file harder to find, and replaced it with
their own license that required annual payments to their company.
However that fork now appears to be dead. So there is no need to
reinforce the copyright notice in every single file.
2021-08-24 12:25:09 -07:00
Jacob G-W 9fffffb07b fix code broken from previous commit 2021-06-21 17:03:03 -07:00
Andrew Kelley 5619ce2406 Merge remote-tracking branch 'origin/master' into stage2-whole-file-astgen
Conflicts:
 * doc/langref.html.in
 * lib/std/enums.zig
 * lib/std/fmt.zig
 * lib/std/hash/auto_hash.zig
 * lib/std/math.zig
 * lib/std/mem.zig
 * lib/std/meta.zig
 * test/behavior/alignof.zig
 * test/behavior/bitcast.zig
 * test/behavior/bugs/1421.zig
 * test/behavior/cast.zig
 * test/behavior/ptrcast.zig
 * test/behavior/type_info.zig
 * test/behavior/vector.zig

Master branch added `try` to a bunch of testing function calls, and some
lines also had changed how to refer to the native architecture and other
`@import("builtin")` stuff.
2021-05-08 14:45:21 -07:00
Veikka Tuominen fd77f2cfed std: update usage of std.testing 2021-05-08 15:15:30 +03:00
Andrew Kelley 429cd2b5dd std: change @import("builtin") to std.builtin 2021-04-15 19:06:39 -07:00
Andreas Karlsson 50af87a9e3 Fix example code in comments for asc and desc 2021-01-06 15:53:53 -08:00
Frank Denis 6c2e0c2046 Year++ 2020-12-31 15:45:24 -08:00
Andrew Kelley 4a69b11e74 add license header to all std lib files
add SPDX license identifier
copyright ownership is zig contributors
2020-08-20 16:07:04 -04:00
Vexu e85fe13e44 run zig fmt on std lib and self hosted 2020-07-11 20:41:19 +03:00
Andrew Kelley d4d954abd2 std.sort: give comparator functions a context parameter 2020-06-08 15:16:40 -04:00
Yuri Pieters f5f77089b7 sort.binarySearch: Remove unneeded edge case check 2020-04-09 09:13:47 +01:00
Yuri Pieters b7e72cc421 sort.binarySearch: test for regresson of #4980 2020-04-09 02:00:08 +01:00
Yuri Pieters 447dc2bb90 sort.binarySearch: fix integer underflow (#4980)
When the key was smaller than any value in the array, an error was
ocurring with the mid being zero and having 1 subtracted from it.
2020-04-09 01:58:57 +01:00
Andrew Kelley 9e7ae06249 std lib API deprecations for the upcoming 0.6.0 release
See #3811
2020-03-30 14:23:22 -04:00
Benjamin Feng 699c50a375 Switch a bunch of FBA to use testing.allocator 2020-02-12 17:17:56 -06:00
LemonBoy db3aea3a0b Change API for binarySearch fn 2020-02-03 21:51:03 +01:00
LemonBoy fd8d8afb24 stdlib: Add binary search function 2020-01-31 00:40:43 +01:00
Andrew Kelley 13259acbc3 std.sort.insertionSort: remove superfluous block 2020-01-28 16:22:09 -05:00
Robin Voetter 841a37ab59 Add std.sort.argMax and std.sort.argMin 2019-12-04 18:20:55 +01:00
Robin Voetter 0159fa284a Make std.sort.min and std.sort.max return ?T 2019-12-04 18:10:20 +01:00
Robin Voetter 65f57e4499 Make std.sort.max accept const slices and add tests 2019-12-04 16:42:18 +01:00
Robin Voetter 6bb0ee0bc4 Add std.sort.isSorted 2019-12-04 16:41:32 +01:00
Andrew Kelley bf3ac66150 remove type coercion from array values to references
* Implements #3768. This is a sweeping breaking change that requires
   many (trivial) edits to Zig source code. Array values no longer
   coerced to slices; however one may use `&` to obtain a reference to
   an array value, which may then be coerced to a slice.

 * Adds `IrInstruction::dump`, for debugging purposes. It's useful to
   call to inspect the instruction when debugging Zig IR.

 * Fixes bugs with result location semantics. See the new behavior test
   cases, and compile error test cases.

 * Fixes bugs with `@typeInfo` not properly resolving const values.

 * Behavior tests are passing but std lib tests are not yet. There
   is more work to do before merging this branch.
2019-11-27 03:37:50 -05:00
Benjamin Feng aca1367533 Optimize binary search algorithm 2019-11-26 13:09:58 -05:00
Andrew Kelley e0db54e89d update the codebase to use @as 2019-11-08 15:57:24 -05:00
Andrew Kelley ed36dbbd9c mv std/ lib/
that's all this commit does. further commits will fix cli flags and
such.

see #2221
2019-09-25 23:35:41 -04:00