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

Vector and String need Splice functions #17

Open
Chrinkus opened this issue Dec 6, 2022 · 1 comment
Open

Vector and String need Splice functions #17

Chrinkus opened this issue Dec 6, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@Chrinkus
Copy link
Owner

Chrinkus commented Dec 6, 2022

My initial thought is that you provide a read-only source, a start index, an end index, and a writable destination. The indexes will be [start, end) as per most uses of indexes. The user can use -1 for "to the end". Here's the signature:

void*
cgs_vector_slice(const struct cgs_vector* src, size_t beg, size_t end, struct cgs_vector* dst);

This will return 'dst' on success and NULL on any kind of failure. And a typical use:

struct cgs_vector v1 = cgs_vector_new(sizeof(int));
// fill vector with 10 goodies
struct cgs_vector v2 = cgs_vector_new(sizeof(int));
assert(cgs_vector_slice(&v1, 5, -1, &v2);

Usability concerns:

  • negative one can be used as an end index to indicate "to the end"
  • elements are copied, source container is unchanged
  • the destination is 'pushed' to, it may already have elements in it
  • implementation should check that both vectors are initialized to use the same size of elements
  • if the range is outside the bounds of the source then NULL is returned and 'dst' is unchanged
@Chrinkus
Copy link
Owner Author

Chrinkus commented Dec 6, 2022

Slice vs Splice vs Sub

I have struct cgs_strsub for "slices" of a string, perhaps the equivalent for vectors should be a struct vecsub? A non-owning view into a vector. Maybe.

As for my initial need, that being AoC 2022 day 5, it seems a splice function is more appropriate. This would allow me to remove and replace multiple elements if I wanted.

void*
cgs_vector_splice(struct cgs_vector* v, size_t beg, size_t end, struct cgs_vector* reps);

The replacements parameter could be NULL to just cut out the range and be done.

@Chrinkus Chrinkus added the enhancement New feature or request label Dec 6, 2022
@Chrinkus Chrinkus changed the title Vector and String need Slice functions Vector and String need ~Slice~ Splice functions Dec 6, 2022
@Chrinkus Chrinkus changed the title Vector and String need ~Slice~ Splice functions Vector and String need Splice functions Dec 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant