Comments (4)
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.
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.
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.
This has been already fixed in the dev branch by b2bef4e
from c-macro-collections.
Related Issues (20)
- When removing elements from a collection, check if ftable->free exists HOT 1
- CMC_IMPL_XXXX
- Tracking: CMC
- Tracking: SAC
- Tracking: DEV
- Tracking: EXT
- Tracking: feature stabilization
- Do not include code by default
- Move everything out of src/cmc folder
- list.h should have a sort function HOT 4
- Use const when possible
- Remove ext/ folder, use macros instead
- CMC_EXT_INIT should be there by default HOT 1
- If PFX not set, assume PFX=SNAME HOT 2
- cmc_collection base data type
- add function which returns pointer to added element
- Compiling without CMC_CALLBACKS support generates warning
- Export symbols for windows
- Look into __VA_OPT__ for C23
- Pre-hashed key for hashed data structures
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from c-macro-collections.