Comments (7)
And the hash table has the same problem. I see little use cases where this kind of implementation could be of interest.
I'm not sure where you're coming from here. If you look at any other container implementation for the C language, you'll see that they all use the same method. I've seen plenty of programs in which storing pointers inside containers was a useful thing to do. Now, of course, sometimes you actually do want to store things directly, but it's hard to write a generic container in C that doesn't use void*
. It's not great, but it technically gives you the ability to store anything indirectly.
To actually do what you're proposing, some kind of template system would need to be devised. C++ does this automagically, but C has nothing like it. I guess maybe something could be done with macros, but I don't see any non ugly and awkward solution there.
Maybe you had something on mind?
from collections-c.
In fact, you can find some container C libraries implementing what I'm looking for, that is a true container, not a shallow one ie containing only reference to data:
- a well known example is the glib library whose implementation use the
void*
generic pointer. - the same for the qlibc library but the hash table is only string-capable.
- the same for Containers but unfortunately the hash table has the same problem your hastable has (only references to the data).
- another example is the klib library with a macro-based implementation (klib is a great library and the khash hashtable provided competes with the fastest libraries as google dense_hash_map and others).
- another macro based one is GenericDataStructure.
I was looking for a lightweight library (glib and qlibc are not) and implementing true vector and hastable containers by means of generic pointers.
Your implementation has a narrow use case because it doesn't manage the data it points to. A function cannot easily return an Array :
- this is undefined behavior if the data the array points to is allocated on the stack
- if the data is allocated on the heap, the function has to return also pointer(s) to the dynamic memory and all the burden of managing memory falls back to the user.
from collections-c.
Ah okay, I got it. You want a container that copies the data into it and I agree it would be a nice addition. I might add it eventually. However, you are always welcome to send a pull request if you feel like contributing!
from collections-c.
Hi @Pascal-Ortiz I see you linked to my Containers library.
In my implementation, you can either copy the data or copy the pointer, up to you. If you pass in a pointer to your data, it copies the data, but if you pass in a pointer to a pointer, it copies the pointer. I'm optimizing the library to be more efficient, and am about half done with that, and afterwards I'll add some more documentation on how to use the functions, etc.
from collections-c.
Hi Bailey!
Hi @Pascal-Ortiz I see you linked to my Containers library.
Indeed, your vector holds the data and is 100% OK for me.
I was talking about the hashtable implementation and to me more precise i was referring to this line of code from the unordered_map_add_item
function:
me->buckets[index] = add;
where add
is a pointer.
In my implementation, you can either copy the data
Ah, ok, perhaps are you referring to the unordered_map_put
function?
from collections-c.
@Pascal-Ortiz that function is used internally (it is possible to put the pointer into the bucket because prior to that in the put function, a copy is made). The put function is the one which users of the library would use.
Also I don't want it to look like I'm hijacking this repository's issue, so maybe we should move this discussion to an issue in bkthomps/Containers or in email?
from collections-c.
Also I don't want it to look like I'm hijacking this repository's issue, so maybe we should move this discussion to an issue in bkthomps/Containers or in email?
Certainly better.
from collections-c.
Related Issues (20)
- How about adding AVL or Red Black tree data structure? HOT 4
- (deque.c): inline function upper_pow_two only valid for 32 bit size_t HOT 5
- PQueue heap-overflow HOT 1
- deque_remove_at error
- cc_array_reverse() fails when element count is even HOT 1
- Implementation error in `cc_array_reverse` and `cc_deque_reverse`
- cmake configuration failed on latest MSYS2 HOT 1
- document is inaccurate about key_compare function
- Is "stdio.h" necessary? HOT 1
- The call to mem_alloc in cc_array_subarray() is unsafe
- How about adding Bloom Filter data structure? HOT 2
- cc_list.c (index > list1->size)maybe is wrong
- file: cc_list.c function:static INLINE void merge(……) HOT 1
- File: cc_list.c Function: cc_list_iter_add
- Feature Request: provide release versioning HOT 1
- RFC: Distro specific packages HOT 1
- What happened to macros such as: DEQUE_FOREACH? HOT 1
- when make occurs error HOT 1
- Segfault when calling tree_min or tree_max with a sentinel node
- Treetable: Segfault when calling get_successor_node on root if it is the greatest key HOT 1
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 collections-c.