I would like to know if it's possible to create the equivalent of record type in typescript in C and add attribute during the runtime.
my objectiv is to recreate this:
const familyData: string[] = ["paul", "em", "matthias", "kevin"];
const myFamily: Record<string, boolean> = {};
familyData.forEach(d => myFamily[d] = true);
why do I want to do something like this in C ? well if I can do this if will be able to know directly if kevin exist or if gregory doesn't by joust doing this:
const name: string = "kevin"; // it can be anything
console.log(familyData["kevin"]); // expected true after the init / undefined if it doesn't exist or if the object isn't init
and for me this is really time saving for doing calculations.
Note that later I will use this to not only link a name to the true value but also to some data.
But for now In C my stuck with going on all the array until I find the name of the guys and so the time it take will depend of the size of the array each time and want to get some data.
does some one have a solution to create something like this in C ?.
Thanks a lot.
CodePudding user response:
To make my comment an answer:
You're looking for a structure that can map strings to values. In general you could do that with a string-keyed hash table.
Since you're only mapping to booleans (true, false), and I'd assume your use case is fine without the third state (person not in table, person in table with false value, person in table with true value), a hash set would do too; not being present in the set could be interpreted as a false value.
As mentioned in the comments by Craig Estey, this particular case is could also be served by red-black trees or tries.