Code Monkey home page Code Monkey logo

Comments (4)

pfeatherstone avatar pfeatherstone commented on May 20, 2024

How about this:

bool PFX##_pop_range(struct SNAME *list, size_t beg, size_t end)
{
if (PFX##_empty(list) || beg > end || end > list->count)
return false;

if (list->deallocator)
{
size_t i = beg;
for (; i < end ; i++)
list->deallocator(list->buffer[i]);
}

size_t length = end - beg;
size_t remain = list->count - end;
if (remain > 0)
memmove(&list->buffer[beg], &list->buffer[end], remain * sizeof(V));
list->count -= length;
memset(&list->buffer[list->count], 0, length * sizeof(V));

return true;
}

bool PFX##_pop_at(struct SNAME *list, size_t i)
{
return PFX##_pop_range(list, i, i + 1);
}

bool PFX##_pop_front(struct SNAME *list)
{
return PFX##_pop_at(list, 0);
}

bool PFX##_pop_back(struct SNAME *list)
{
return PFX##_pop_at(list, list->count - 1);
}

void PFX##_pop_if(struct SNAME *list, bool (condition)(V))
{
int
indices = malloc(list->count * sizeof(int));
int i;
for (i = list->count - 1; i >= 0 ; --i)
indices[i] = condition(list->buffer[i]) ? i : -1;

for (i = list->count - 1; i >= 0 ; --i)
{
if (indices[i] != -1)
PFX##_pop_at(list, indices[i]);
}
free(indices);
} \

from c-macro-collections.

pfeatherstone avatar pfeatherstone commented on May 20, 2024

In the last post I’ve realised that I don’t need to preallocate indices. I don’t know what I was thinking

from c-macro-collections.

LeoVen avatar LeoVen commented on May 20, 2024

Hi thank you for the suggestion. The indices are indeed wrong. I will get them fixed in the next release.

Regarding calling PFX##_pop_at(list, 0) inside PFX##_pop_front won't happen because I have another utility to this library cooking up. You will be able to set callback functions for each of the main functions of each collection. This means that if you set a callback function to PFX##_pop_at it will activate when PFX##_pop_front is called too. This, of course, could change.

Regarding deallocating the elements when it is popped: this was a design decision. What if the element being popped in a list is also handled by another list? O maybe another collection? So to get it freed from memory you will have to first access it with PFX##_get and then deallocate it.

from c-macro-collections.

LeoVen avatar LeoVen commented on May 20, 2024

This has been already fixed in the dev branch by b2bef4e

from c-macro-collections.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.