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

Consider a Set type (or something similar) #44

Open
uint opened this issue Jun 12, 2024 · 3 comments
Open

Consider a Set type (or something similar) #44

uint opened this issue Jun 12, 2024 · 3 comments
Milestone

Comments

@uint
Copy link
Collaborator

uint commented Jun 12, 2024

@dakom mentioned he'd sometimes see usage of the Map where the value is (), and the last key component is the actual value.

@dakom
Copy link

dakom commented Jun 13, 2024

yeah, just to elaborate a bit - and this may be two separate issues:

one use-case is exactly as the title says, something like a Set.. it's not that bad to just use Map<Foo, ()>, it could almost work just like a type alias.

the other use-case is a bit trickier, where I want a list with a dynamic key. This is probably solved with Deque::new_dyn, but that's a relatively recent addition and I haven't had the pleasure of needing it hands-on yet :)

Specifically, for the "list with a dynamic key" problem, without Deque::new_dyn, I'd solve it using a composite key and a prefix, e.g.: Map<(Foo, Bar), ()>. Then the "bars for a given foo" can be iterated over via .prefix(foo).keys(...). It works perfectly fine, but it's a bit odd.

@uint
Copy link
Collaborator Author

uint commented Jun 13, 2024

the other use-case is a bit trickier, where I want a list with a dynamic key

Ah, right. For this one, I have a feeling we're already good. storey's version of this would be something like Map<Foo, Column<Bar>>. The Map handles dynamic keys of type Foo, and then Column is a bit like a list, only every entry is indexed with u32.

If the column isn't right, it should be fairly easy to implement some other list-like collection in storey, and then plug it into the map.

An example of a "column in a map"

@dakom
Copy link

dakom commented Jun 13, 2024

nice! so just to expand on the use-case a bit - in case this helps:

where i've needed it, it's usually just a dynamic list, and so Column with an index sounds like it covers it completely

but sometimes it's not exactly a list by index, but rather something else... for a made-up example, consider NFTs where TokenId is not numeric but some other string that's created outside of the contracts for some reason.

so TOKENS_PER_OWNER = Map<Addr, Column(TokenId)> could work, but the index isn't something that would really be used - i.e. I'd want to be able to remove an item by (Addr, TokenId) without knowing the internal index

Also, since it's a Map I assume all the usual range/keys stuff just works- but just to make sure, it's important to be able to paginate and in both directions (both Ascending and Descending)

@uint uint added this to the v0.4.0 milestone Sep 4, 2024
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

No branches or pull requests

2 participants