Tracks
/
Cairo
Cairo
/
Exercises
/
Chrono Realms Chrono Chain
Chrono Realms Chrono Chain

Chrono Realms Chrono Chain

Work In Progress
Learning Exercise

Introduction

Smart pointers in Cairo are advanced data structures that ensure safe and efficient memory management by adding safety features to regular pointers, preventing common issues like dereferencing null pointers or accessing uninitialized memory.

What is a Smart Pointer?

A smart pointer behaves like a regular pointer but tracks ownership and ensures safe memory access, preventing issues like null or dangling pointer dereferencing.

Types of Smart Pointers

Cairo provides several smart pointer types, such as Box<T> and Nullable<T>:

  • Box<T>: Stores data in a special memory segment, ideal for large or dynamically-sized data. It allows transferring ownership without copying the data.
  • Nullable<T>: Points to either a valid value of type T or null, useful for handling optional values.

Memory Safety

Smart pointers help prevent unsafe memory access, ensuring memory is automatically deallocated when no longer needed, thus reducing the risk of memory leaks.

Example: Using Box<T> for Recursive Types

Smart pointers like Box<T> allow for safe handling of recursive types, such as in a binary tree, by allocating memory efficiently and avoiding infinite recursion.

use core::box::{BoxTrait};

#[derive(Copy, Drop)]
enum BinaryTree {
    Leaf: u32,
    Node: (u32, Box<BinaryTree>, Box<BinaryTree>),
}

fn main() {
    let leaf1 = BinaryTree::Leaf(1);
    let leaf2 = BinaryTree::Leaf(2);
    let node = BinaryTree::Node((3, BoxTrait::new(leaf1), BoxTrait::new(leaf2)));
    println!("{:?}", node);
}

Performance Benefits

Smart pointers improve performance by passing references to data instead of copying large structures, reducing memory overhead.

// `Cart` is a large struct that contains a lot of information
fn pass_pointer(cart: Box<Cart>) {
    let cart = cart.unbox();
    println!("{} is shopping today and bought {} items", cart.buyer, cart.items);
}

Instructions

In Chrono Realms, Time Keepers often deal with not just trees of timelines, but Chrono Chains-sequences of linked TimeNodes, each representing a specific moment in time. A Chrono Chain is a straight path of sequential moments, where each TimeNode connects to the next. These Chrono Chains are useful when traveling through a series of specific events, as they allow Time Keepers to follow a single timeline.

However, to handle these potentially long Chrono Chains, Time Keepers use Smart Pointers (Box<t>)</t> to safely manage and traverse these lists of moments without causing unnecessary memory duplication or overflow. Each TimeNode holds a reference to the next node, forming a recursive structure.

Your task as an apprentice is to implement a Chrono Chain as a recursive list structure using smart pointers.

In this exercise, you will:

  1. Create a recursive ChronoChain enum, representing a list of moments.
  2. Use the Box<T> smart pointer to store the recursive nodes.
  3. Implement a function to create a ChronoChain from an array of u32 values.
  4. Implement a function to traverse the ChronoChain and sum up the values stored in the list.

1. Define the Recursive ChronoChain Enum

Create a recursive enum ChronoChain with two variants:

  • End: Represents the end of the list.
  • Link: Contains a u32 value and a boxed reference to the next node in the chain.

2. Create a Function to Build a ChronoChain

Write a function ChronoChain::build that takes an array of u32 values and returns a ChronoChain, linking the values sequentially using smart pointers.

3. Implement the Sum Function

Write a function ChronoChain::sum to recursively traverse the ChronoChain and sum the values of all nodes.

Example Usage

fn main() {
    // Create a ChronoChain from an array of values
    let chrono_chain = ChronoChain::build(array![10, 20, 30]);

    // Sum the values in the ChronoChain
    let total_sum = chrono_chain.sum();

    println!("Total Time Power: {}", total_sum);
}
Edit via GitHub The link opens in a new window or tab
Cairo Exercism

Ready to start Chrono Realms Chrono Chain?

Sign up to Exercism to learn and master Cairo with 68 exercises, and real human mentoring, all for free.