I have a data structure that supports following operations:
An item can be inserted in constant time. For that item the data structure assigns a unique positive integer.
(Clarification: assigned integer is not a function of inserted item, and user has no choice on the assigned integer. It is chosen solely by the data structure.)
Using that integer the item can be found in constant time.
Using that integer the item can be removed in constant time.
It is implemented using an array of pointers where the assigned integers are indices where the items are stored. Unused indices are chained up in linked-list fashion for constant time insertion.
What is/should be the name of such data structure?
It's an array with a "free list."
As it sounds like a hash based data structure, how about calling it "Simple Hash List".
Read more about hash list here http://en.wikipedia.org/wiki/Hash_list
Related
Are lists and arrays different in python?
Many articles refer to the following as an array: ar = [0]*N of N element. Which is a list. Not sure if these words are used interchangeably in python. And then there is a module called array
The terms "list" and "array" are not interchangeable in either Python or computer science (CS).
In CS an array data structure is defined, as you've already noted, as a contiguous block of data elements all of which are the same size. Python's list does not conform to this definition as each element of a Python list can not only be a completely different data type, but can even be another data structure type such as list, dictionary, or set.
Python stores each element of a list as a separate data object, and the references to those objects are stored in Python's list data type. My past reading indicates that the list is stored as an array, but I've measured the list's time complexity and I suspect that it may technically be a hash table.
Python's lists may be ordered, and you can use and access a Python list as if it were an array as I frequently do, but don't be confused by Python's use of square brackets: it's not an array.
Python's array module implements an actual array data type for storing numerical data (actually, I think you can store character data as well).
Suppose we have a structure array of up to 50 elements that will be added in turn from a buffer write function. How do I find the current number of recordings made in array if the maximum number of items has not been reached?
typedef struct
{
remoteInstructionReceived_t instruction;
uint16_t parameter;
} instructionData_type;
remoteInstructionReceived_t commandBuffer[50];
C arrays are fixed-size: there are always exactly 50 objects in your array. If your program logic requires some of them to be "inactive" (e.g. not written yet), you must keep track of such information separately. For example, you could use a size_t variable to store the number of "valid" entries in the array.
An alternative would be to designate a value of remoteInstructionReceived_t as a terminator, similarly to how 0 is used as a terminator for NUL-terminated strings. Then, you wouldn't have to track the "useful length" of the array separately, but you'd have to ensure a terminator always follows the last valid item in it.
In general, length-tracking is likely both more efficient and more maintainable. I am only mentioning the second (terminator) option out of a sense of completeness.
You can't, C doesn't have a way of knowing if a variable "has a value". All values are values, and no value is more real than any else.
The answer is that additional state, i.e. some form of counter variable, is required to hold this information. Typically you use that when inserting new records, to know where the next record should go.
Have you considered using a different data structure? You can wrap your structure to allow the creation of a linked list, for example. The deletion would be real just by freeing memory. Besides, it's more efficient for some kinds of operation, such as addin elements in the middle of the list.
I find enum to be similar to an array,with the elements in it being numbered from 0. So what is the difference between an array and enum?
An Enum is basically a group of named constants.
It is an alternative to numbered flag parameters.
(It also doesn't have to be numbered from zero, you can specify the numbering.)
An Enum could be days of the week for example.
A Enum could departments in a company: eg SALES, BILLING, HR ...
A array is a sequence of memory locations. It is a collection. Each element in that collection is indexed by a number. So using that number you can retrieve the value stored at that location. Much like the page number in a book lets you look up the content of that page, the index on an array lets you look up the value stored at that location.
For example, if your company had numbered physical mail boxes for each department (starting from zero):
and you were creating some very simple software to let users log in to check how many letters they had uncollected,
you might choose to store them in an Array of Ints. Where the index is the mail box number, and the value is the number of letters in the box.
Then for ease of programming, you might choose define the departments as Enums (as described before) such that you could index of department name. But that would be getting more advanced.
You may be confusing Enum, with Enumerable which is a term used in some languages to describe any collection type which can be iterated though in sequence (Eg IEnumerable in C#). This term is not often used in C. It is not in anyway the same at all as Enum.
Array is a variable that can contain multiple elements with index starting from 0 whereas enum is an user defined datatype that contains a list of members for which an integer constant is assigned starting from 0. in case of enum the numbers starting from 0 are not indexes whereas in case of an array they are indexes. Also in case of enum you can assign your own constant values to the members that may or may not start from 0 and may or may not be in a sequence.
The main difference is that an array is a value and an enum is a type. And One main difference we can say that an array is a collection of other values (that is it contains other values, you can iterate through them or access individual ones by index), whereas an enum value is simply one atomic value. It doesn't contain any other values.
Both of them are totally different. Array is a value and enum is a type. array is a collection of different values whereas enum value is simply one value.
Array is used to iterate among various values using index whereas enum is assigned some atomic value and iterated so that we can easily iterate the type.
Array's value are assigned by using = operator 'eg. int a[4]={1,2,3,4}' and enum is the user defined data type and it is assigned like
enum days{sun,mon,tue,wed,thr,fri,sat}
If i write in Lua
local var = {1,2,3,4,5,6}
Variable var is array.
If i want to keep it as array (not hash), i must use table.insert, table.remove etc.
This code will turn it into hash:
var["key"] = 4
QUESTION:
Does this code turn array variable into hash?
local var = {1,2,3,4,5}
var[4] = "string"
var[6] = "string"
var[1] = "string"
As others already pointed, Lua only has tables. Internally, the values you put in the table, may be stored in its array or hash part, but it's an implementation detail that users don't need to worry about.
In your particular case, the keys will be stored in the array part only (even after assignment), as you are not creating any new keys. According to a detailed description in Lua Performance Tips ("About tables" section), the initial assignment will allocate 6 slots in the array part and then you just re-assign those. If you add var[7] = "string", then this value goes into the hash part and this will trigger a re-hash as the hash part has size 0 at this point. Lua will calculate how many slots are needed to accommodate all values in the array part (7 now) and "chooses as the size of the array part the largest power of 2 such that more than half the elements of the array part are filled".The array part will now have 8 slots and the hash part will still have size 0 as there are no elements to go there.
QUESTION: Does this code turn array variable into hash?
So the answer to this question is "no" (if by "turn into hash" you mean that the table gets a non-zero hash part). If you do var[8] = "string" (without assigning var[7]), the hash part will get non-zero size, but if you later do var[7] = "string", this will trigger another re-hash as this element won't fit in the hash part and all these elements will be assigned to the array part again.
Lua is trying hard to keep the most appropriate structure for the elements you have in the table, but the changes to the structure itself are only done during re-hashing.
You are trying to draw a distinction between "arrays" and "hashes" which does not exist in Lua. They're all tables. Some of them just have keys which aren't all positive integers.
Its just a question out of curiosity. Suppose we have an associative array A. How is A["hello"] actually evaluated , as in how does system map to a memory location using index "hello"?
Typically it uses a data structure that facilitates quick lookup in mostly constant time.
One such typical approach is to use a hashtable, where the key ("hello" in your case) would be hashed, and by that I mean that a number is calculated from it. This number is then used as an index into an array, and in the element with that index, the value exists.
Different data structures exists, like binary trees, tries, etc.
You can google for keywords: hashtable, binary tree, trie.