I am using C NOT C++!
I know the C++ collections, but I was wondering if Microsoft has a C based List structure of some type, like the linux kernel provides, that I can use in a user mode project?
I would prefer not rolling my own.
Only thing in the Windows API is interlocked singly linked lists, which are used via InterlockedPushEntrySList and InterlockedPopEntrySList .
For device drivers, there is LIST_ENTRY, but I am not sure if this can be pulled into user-mode.
Many algorithms books and websites contain implementations of linked lists that can easily be ported to C. Rolling your own is not too difficult.
reusable collections are tough in C, it just doesn't have the flexibility or metadata (how do you know when you are overflowing this array list and need to reallocate? How do will reallocate work if the rest of the code is using a custom alloc?
You can do it (you CAN do anything in c), but it gets abstract really fast.
On the other hand, creating a linked list in c yourself is downright fun. Arrays are already there, hashes are annoying but not impossible, trees are fun, ...
Also--people who think in c tend to be constantly optimizing. Setting every linked list operation behind a function call instead of just using this=this.next would probably disgust many of them (rightfully so).
Related
I have a Python function that loops over a list and I want to convert it to Cython for performance gain.
The lists it accepts contain a mix of strings, integers, and floats, so I'm not sure how to statically type the variables involved (I don't know C).
What would be the most efficient way to implement something like this in Cython?
You seem to be hoping for a C type that has all the flexibility of the Python object, but is somehow magically faster.
There is basically a good option and a bad option here:
The good option is to accept that such a type doesn't really exist.
Therefore, you should leave your data extracted from the list untyped so that it remains a regular Python object. Not everything in Cython needs to be typed - the vast majority of Python code should run unchanged.
It might be worth typing your list as list, since Cython can generate slightly more efficient loops when it knows the iterable is a list.
The bad option is to use a feature of C called a union which represents a variable that is one of a limited number of dissimilar types. I am not recommending this (especially for someone that doesn't know C already) because there is no "easy" Cython wrapping (you'll have to dive directly into the C details). You will find handling strings in a union particularly challenging.
Pursue this option at your own peril.
Arrays are one of the fundamental data structures and I was reading them up on LeetCode and saw the following -
They exist in all programming languages, and are used as the basis for most other data structures.
The claim that they exist in all programming languages seems a bit extravagant to me but maybe that's just because I am a beginner.
So are Arrays really present in all programming languages and I've seen some quite cryptic ones while exploring Code Golf pages which seem to be intentionally made so, so do they still use Arrays or are there any alternatives to arrays as well?
Almost every claim about "all programming languages" is false, simply because of the fact that there are so many programming languages that for every claim there is a counter example.
So let's restrict to practical programming languages.
Then, yes, every practical programming language offers the possibility to collect things of similar type in an ordered collection.
However, depending in your definition of 'Array', and on the exact conditions when arrays 'exist' in a programming language, this need not be an array. For example:
Some scripting languages (such as Perl, if I remember correctly) only have dictionaries, and an array is simply a dictionary with numbers as keys.
Some languages only have linked lists (for example some functional and logical programming languages).
Some languages don't have arrays or lists, but are expressive enough that linked lists can be easily defined.
you can find some home made language that do not respect any standard like the one used in the application ProRealTime: the name of the language is ProRealCode and is used to program indicators or scanners on financial markets.
it has a feature to get older data with x[index], but you cannot create proper array and loop in to it.
The main problem of this home made language are:
they can have some bugs (so you do not know if your code is buggy or the execution machine)
you have no serious debugger
For me, an array is an abstraction that has a concept of the element count, and not necessarily maps it to contiguous memory. I think of Fortran arrays since it was the first programming language and it sets the expectations of what arrays are.
Here are some examples of (mainstream) languages without a concept of arrays:
Assembly Language
Logo, or turtle graphics.
XSLT
C
In lieu of arrays, some alternatives are pointers with offsets (C, assembly), linked lists, and other tree-like structures. Also, some languages may have the concept of a stack (assembly) which has some of the features of arrays. C++ does have arrays in the form of std::vector<T>, and other STL structures.
This is a community wiki, and so I hope the community can edit/add to this freely.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C Analog To STL
Oftentimes, when programming in C, I find myself wishing that I had access to something like the vector or list classes from C++, and I end up implementing a sort of stripped-down version of some high-level data structure, but it takes a fair amount of time. I am wondering if anyone knows of a good library of high quality simple data structures in C?
Well, there's the GLib library, library, which is what is used to implement the GTK+ widget set. GLib uses GObject as a way to do OOP in C. But GObjectified C is famous for being very verbose.
Other than GLib as others have suggested, you could create your own generic types without any type safety, such as a linked list that uses void * as the generic pointer to the next stack object. Some have even used macros to create template-like behaviour in ANSI C with some reasonable success. Search around cplusplus.com's forums for an example or two (there aren't many more than that if I recall correctly).
Alternatively you could implement your own OOP scheme as described in the paper Object-Oriented Programming With ANSI C, though it's something I wouldn't recommend. You might as well take the time you'd spend creating that beast (and possibly not understanding how it works in the end) and learn some of GLib to get familiar with it.
Personally I prefer the first approach. You lose type safety and gain a bunch of casts, but it's the fastest to implement. The real problem with that method is the data that is stored. std::stack<int> converted to C is simply a struct stack that has a data field of type int. What about std::vector<std::stack<int> >, an addressable list of stacks of integers? The C standard cannot guarantee that casting from a pointer type to an integral type will work without some loss of information. In other words, while you can necessarily use pointers, you can't use plain integers to store information. Perhaps a few unrolled template specializations would work -- one for int, (one for long long?), one for double, and one for pointer types.
In the end, the best solution is to use a library like GLib, but if you prefer to roll your own, take care when creating it. You never know when you'll need a combination of types of items in a container.
I like GLib for this kind of thing; the code you end up writing is clean, the libraries are easy to use, stable, mature and cross-platform, and the documentation is excellent.
You may also want to take a look at Rusty Russell's CCAN project. I haven't used any code from it but I will definitely go dip my toe in the next time I have similar needs:
http://ccan.ozlabs.org/
The Idea
That nice snippets of C code should be moved out of junkcode directories and exposed to a wider world, where they can become something useful.
CCAN is loosely modelled after the successful CPAN project for Perl code development and
sharing.
Is there a library that implements lock-free algorithms(queue, linked list and others) written in C (not in C++)? I've taken a look at some libraries like Intel's, but I would like to use generic libraries, at least more generic than Intel's one.
See Practical lock-free data structures from the University of Cambridge
I've written my own, Rig, currently queue, stack and list are there, hash-table will soon follow. While I'm still working on it, it is intended for public consumption, and the API is mostly stable, just use the SVN trunk. :)
The only other such library in C that I know of is liblfds, though I've never used it.
liblfds
http://www.liblfds.org
Wiki with full API documentation, forum for questions, blog for reading the author rattle on :-)
Platform independent. Out of the box for Windows, Linux, Intel and ARM.
Release 7 should be out in a month or two. Will add run-time cache line alignment, backoff and SMR. (SMR also gives a ton of the other CPU types - basically, anything GCC compiles on which supports atomic ops, e.g. SPARC, MIPS, IA64, etc).
Also, there's no license - you can use the code however you want. Make money! It's not GPL.
I'm currently writing a lock-free lib but it's C++. Here's an STL-like Lock-Free Doubly-Linked List.
The memory manager it uses is quite powerful (32-bit CAS without ABA issues) so I'm using it to create a complete set of containers: a lock-free map/set (using skip lists), a lock-free bag (instead of queue/stack), and a lock-free unordered map (hash table using split-ordered lists).
For more info about the doubly-linked list check out my answer to a related question.
in spite of having so many efficient data structures, why is only linked list used
so heavily in systems programming? Is it because it allows least usage of heap/less buggy code?
Regards,
Pwn
Linked List is a very efficient data structure to use for something like a queue or stack where you want to add something at the end or remove it from the beginning. Systems programming deals a lot with queues and stacks.
I'm guessing because it's just extremely simple to implement and understand. And it has an extremely small overhead.
The kernel has access to copious amounts of memory (it's in charge of it, after all) and mainly has to control lists of things (rather than associative structures that connect one thing with another).
So it's just a good match. There is no (or rarely) any need to complicate it further.
Linked lists are quite efficient for many systems-level tasks:
Queues (for scheduling)
Collections where your main operation is to visit every single thing in the collection, e.g., gather information about every active process
Memory blocks (for first-fit allocation)
Collections where you add or remove one element at a time
It's quite rare in systems programming to have a collection where you have to look things up by key, or to have to take the union of two sets. Except of course in filesystems, where you will find that more sophisticated data structures are used.
Is it because it allows least usage of heap/less buggy code?
No. In many cases, a linked list is the most appropriate data structure for the job.
Linked lists provide an easy implementation for several important abstract data structures, including stacks, queues, hash tables, symbolic expressions, and skip lists.
Its partly because efficent datastructures tend to have overheads that are relatively large when dealing with small numbers of entries, and partly because a lot of OS data-structures are FIFOs which linked-lists are good for.
Usually it is because you need a list of stuff - that is, you often pretty much just need something you can add/remove easily at either end or remove/insert at a node pointer you already have and iterate over.
There's plenty of areas where you don't need random access or search capabilities - or the space you need to search is so small that a linked list is anyway faster than more "fancy" data structures.
Sometimes though, it's also because linked lists are easiy to implement.
There's no good answer, because you're making wrong assumptions. It's not true that only linked list is used heavily. It's used when there are many things which need to be traversed in sequence - like free memory segments list, queue-like structures, etc.
There are many places where you need exactly such structure. There are other places where you don't need it.
Linked lists can easily be tied-in with other data-structures, (such as hash-tables for example). Also they can be translated easily to arrays and there are different ways of implementing them, for example one or two way linked list.