NEWTON

NEWTON


Popular tags

    Is there any simple way to concat two arrays with known sizes in Cairo?

    Asked

    4 months ago

    165

    views


    0

    Hey, I want to concatenate two arrays with known sizes in Cairo lang. Is there any simple way how to do it? Can you share some of your code?

      cairoconcat

    Newton

    asked

    4 months ago


    1 answers

    1

    Accepted answer

    Hey, here is an example of concatenating two arrays of known length with the memcpy function and printing the result.

    // Use the output builtin.
    %builtins output
    from starkware.cairo.common.alloc import alloc
    from starkware.cairo.common.memcpy import memcpy
    
    func main{output_ptr: felt*}() {
        alloc_locals;
        let (local arr_one: felt*) = alloc();
        let (local arr_two: felt*) = alloc();
    
        assert [arr_one] = 1;
        assert [arr_one + 1] = 2;
        assert [arr_one + 2] = 3;
        assert [arr_two] = 4;
        assert [arr_two + 1] = 5;
        assert [arr_two + 2] = 6;
    
        let (local concat: felt*) = alloc();
        memcpy(concat, arr_one, 3);
        memcpy(concat + 3, arr_two, 3);
        assert [output_ptr] = [concat];
        assert [output_ptr + 1] = [concat + 1];
        assert [output_ptr + 2] = [concat + 2];
        assert [output_ptr + 3] = [concat + 3];
        assert [output_ptr + 4] = [concat + 4];
        assert [output_ptr + 5] = [concat + 5];
        let output_ptr = output_ptr + 6;
        return ();
    }
    

    In case you want to concat an array of felts and an array of Points, you just have to remember that structure in Cairo is syntaxing sugar. The compiler will compute the offset of each member of the structure and allow you to access the value in memory (see here).

    You can thus do the below if you want to concat your two arrays:

    // Use the output builtin.
    %builtins output
    from starkware.cairo.common.alloc import alloc
    from starkware.cairo.common.memcpy import memcpy
    
    struct Point {
        x: felt,
        y: felt,
    }
    
    func main{output_ptr: felt*}() {
        alloc_locals;
        let (local arr_one: felt*) = alloc();
        let (local arr_two: Point*) = alloc();
    
        assert [arr_one] = 1;
        assert [arr_one + 1] = 2;
        assert [arr_one + 2] = 3;
        assert [arr_two] = Point(3, 4);
        assert [arr_two + 2] = Point(5, 6);
        assert [arr_two + 4] = Point(7, 8);
    
        let (local concat: felt*) = alloc();
        memcpy(concat, arr_one, 3);
        memcpy(concat + 3, arr_two, 6);
        assert [output_ptr] = [concat];
        assert [output_ptr + 1] = [concat + 1];
        assert [output_ptr + 2] = [concat + 2];
        assert [output_ptr + 3] = [concat + 3];
        assert [output_ptr + 4] = [concat + 4];
        assert [output_ptr + 5] = [concat + 5];
        assert [output_ptr + 6] = [concat + 6];
        assert [output_ptr + 7] = [concat + 7];
        assert [output_ptr + 8] = [concat + 8];
        let output_ptr = output_ptr + 9;
        return ();
    }
    

    answered

    4 months ago

    Your answer

    NEWTON

    NEWTON