Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-05-13· OpenAIAi2 WildChat-4.8M (AI2)

Rewrite this function in rust, zig and nim: import numpy as np import os for f in…

  1. User

    Rewrite this function in rust, zig and nim:

    import numpy as np import os

    for f in os.listdir("/tmp/big000"): if 'sizes' not in f: continue arr = np.frombuffer(open("/tmp/big000/"+f,"rb").read(), dtype=np.uint8) arr = arr.reshape((-1,5)) arr = np.pad(arr, ((0, 0), (0, 3))) arr = arr.flatten().view(np.uint64) print(np.max(arr))

  2. Assistant

    Certainly! Below you'll find the equivalent implementations of the given Python script in Rust, Zig, and Nim.

    Firstly, here is the implementation in Rust:

    use std::fs;
    use std::io::Read;
    use std::path::Path;
    
    fn main() {
        let dir = "/tmp/big000";
        for entry in fs::read_dir(dir).expect("Failed to read directory") {
            let entry = entry.expect("Failed to read entry");
            let path = entry.path();
            let file_name = entry.file_name().into_string().expect("Failed to convert OsString to String");
            
            if !file_name.contains("sizes") {
                continue;
            }
    
            let mut file = fs::File::open(&path).expect("Failed to open file");
            let mut buffer = Vec::new();
            file.read_to_end(&mut buffer).expect("Failed to read file");
    
            let length = buffer.len() / 5;
            let mut arr = vec![0u8; length * 8];
            
            for i in 0..length {
                for j in 0..5 {
                    arr[i * 8 + j] = buffer[i * 5 + j];
                }
            }
    
            let arr_u64: Vec<u64> = arr.chunks(8).map(|chunk| {
                let mut byte_array = [0u8; 8];
                byte_array.copy_from_slice(chunk);
                u64::from_le_bytes(byte_array)
            }).collect();
    
            let max_value = arr_u64.iter().copied().max().expect("No values to find max");
            println!("{}", max_value);
        }
    }
    

    Here is the implementation in Zig:

    const std = @import("std");
    
    pub fn main() void {
        const allocator = std.heap.page_allocator;
        const dir = "/tmp/big000";
        
        const entries = std.fs.cwd().openIterator(dir).getNextDirents(allocator, std.fs.DirentType.Other).catch(|err| {
            std.log.err("Cannot read directory: {}", .{err});
            return;
        });
        
        if (!entries) return;
    
        for (entries) |entry| {
            const file_name = entry.name;
            
            if (!std.mem.containsAtLeast(file_name, "sizes")) continue;
    
            defer {
                if (entry.free != null) {
                    entry.free();
                }
            }
    
            var reader = std.fs.cwd().openFile(file_name, .{ .read = true }).getReader(allocator).catch(return);
            const file_data = reader.readAllAlloc(allocator).catch(return);
            if (file_data.len % 5 != 0) return;
    
            const length = file_data.len / 5;
            var padded_data = std.mem.Buffer(u8).initCapacity(length * 8, allocator);
    
            for (file_data) |data, i| {
                padded_data.appendSlice(data, i * 5, min(i * 5 + 5, file_data.len));
                padded_data.appendRepeat(u8{0}, i * 5 + 5, 8 - (5));
            }
    
            var max_value: u64 = 0;
            for (padded_data.items, 0, .x10) |*chunk, i| {
                const val = chunk;
                if (val > max_value) { max_value = val; }
            }
            std.log.info("Max value: {}", .{max_value});
        }
    }
    

    Finally, here is the implementation in Nim:

    import os, sequtils
    
    proc main() =
        let dir = "/tmp/big000"
        for f in walkDir(dir):
            if not "sizes" in f.name:
                continue
    
            let data = readFile(f.fullPath).toSeq
            var paddedData: seq[uint64] = @[]
            var temp: seq[byte] = @[]
            
            for i in 0 ..< data.len:
                temp.add data[i]
                if temp.len == 5:
                    paddedData.add uint64(temp & @[0, 0, 0]) # padding with three zeros
                    temp.setLen 0
    
            echo max(paddedData)
    
    when isMainModule:
        main()
    

    These implementations read the files from the specified directory, process the byte data similarly to the Python script, reshape it, pad it, convert it to a different datatype, and print out the maximum value found. Adjustments may need to be done depending on the exact requirements and file structure.