Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add run-time version of Readable #50

Merged
merged 5 commits into from Apr 29, 2024
Merged

Conversation

statiolake
Copy link
Owner

@statiolake statiolake commented Apr 29, 2024

Currently, you cannot define the readable which uses the prior inputs or run-time values to determine how to parse the input. That means you can't define user-defined version of built-in [T] or [T; n], which uses the prior input as its length.

This feature, RuntimeReadable, allows you to define such a readable. For example, you can define a readable for your directed graph:

struct DirectedGraphReadable(usize, usize);

impl RuntimeReadable for DirectedGraphReadable {
    type Output = Vec<Vec<usize>>;

    fn read<R: BufRead, S: Source<R>>(self, source: &mut S) -> Self::Output {
        let DirectedGraphReadable(n, m) = self;

        input! {
            from source,
            edges: [(Usize1, Usize1); m],
        };

        let mut g = vec![vec![]; n];
        for e in edges {
            g[e.0].push(e.1);
        }
        g
    }
}

let mut source = AutoSource::from("2 3\n1 1\n1 2\n2 2\n");
input! {
  from source,
    n: usize,
    m: usize,
    g: with DirectedGraphReadable(n, m),
}

As you can see in the above snippet, you need to specify the runtime readable after with for the RuntimeReadable.

Closes #48

@statiolake statiolake changed the title Add dynamic version of Readable Add run-time version of Readable Apr 29, 2024
@statiolake statiolake merged commit cedb4ec into master Apr 29, 2024
32 checks passed
@statiolake statiolake deleted the feature/dynamic-readable branch April 29, 2024 09:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow values for marker types
1 participant