cairo-book / cairo-book

The Cairo Programming Language Book, a comprehensive documentation of the Cairo 1 programming language.
https://book.cairo-lang.org/
MIT License
226 stars 216 forks source link

Advanced Cairo: Using arrays inside dicts #421

Closed enitrat closed 3 months ago

enitrat commented 10 months ago

This will now be possible following https://github.com/starkware-libs/cairo/pull/4340 . However, it requires a specific way of interacting with the dictionary: instead of calling get, we will have to call entry.

Nonnyjoe commented 10 months ago

@enitrat I'll Like to handle this.

enitrat commented 10 months ago

It's not possible yet as mentioned

Nonnyjoe commented 10 months ago

Ohhhh.... Sorry, I just noticed the PR was still being reviewed.

Enenche23 commented 5 months ago

@enitrat very interested in this, would love to work on this

TAdev0 commented 5 months ago

@Enenche23 Hi, thanks for the comment. How would you tackle this issue?

enitrat commented 4 months ago

To get started

use core::nullable::NullableTrait;
use core::dict::Felt252DictEntryTrait;
fn main() {
    let temperatures = array![20, 19, 26];
    let mut pixels: Felt252Dict<Nullable<Array<u8>>> = Default::default();
    pixels.insert(0, NullableTrait::new(temperatures));
    println!("Before insertion: {:?}", get_array_entry(ref pixels, 0));

    append_value(ref pixels, 0, 30);

    println!("After insertion: {:?}", get_array_entry(ref pixels, 0));
}

fn append_value(ref pixels: Felt252Dict<Nullable<Array<u8>>>, index: felt252, value: u8) {
    let (entry, temperatures) = pixels.entry(0);
    let mut unboxed_val = temperatures.deref_or(array![]);
    unboxed_val.append(value);
    pixels = entry.finalize(NullableTrait::new(unboxed_val));
}

fn get_array_entry(ref pixels: Felt252Dict<Nullable<Array<u8>>>, index: felt252) -> Span<u8> {
    let (entry, _temperatures) = pixels.entry(0);
    let mut temperatures = _temperatures.deref_or(array![]);
    let temps = temperatures.span();
    pixels = entry.finalize(NullableTrait::new(temperatures));
    temps
}
okhaimie-dev commented 4 months ago

can y'all assign this issue to me after the PR on randomness gets merged

@TAdev0 @enitrat

0xNeshi commented 3 months ago

Just a small correction for the "get started" code (see https://github.com/cairo-book/cairo-book/issues/421#issuecomment-2093305126) - pixels.entry should be called with index for the function to work correctly:

fn append_value(ref pixels: Felt252Dict<Nullable<Array<u8>>>, index: felt252, value: u8) {
    let (entry, temperatures) = pixels.entry(index); // was .entry(0)
// ...
fn get_array_entry(ref pixels: Felt252Dict<Nullable<Array<u8>>>, index: felt252) -> Span<u8> {
    let (entry, _temperatures) = pixels.entry(index); // was .entry(0)
enitrat commented 3 months ago

@misicnenad do you want to work on this btw?

0xNeshi commented 3 months ago

@enitrat that would be awesome!

enitrat commented 3 months ago

@okhaimie-dev removing you from this one as you already have another one active

enitrat commented 3 months ago

@misicnenad I think we can have this sub-section in https://book.cairo-lang.org/ch03-02-dictionaries.html

0xNeshi commented 3 months ago

@enitrat would that mean that we should then remove the unimplemented sub-section 11.2? image

enitrat commented 3 months ago

@misicnenad yes, we would have everything under 3.2 Dictionaries