These are sections of C code used to implement a hashmap using open-addressing. I don't understand what the while loop in the insert function is checking and the if statement in the twoSum Function is checking for.
Does the while loop condition check if the value is in the array or if the value isn't null. Does the if statement check if the value is not 0? I don't understand.
I always thought something like this would work like a while(1) loop that needs to manually break.
I want to know what the conditions of the loops guard statement are I understand the body.
void insert(int *keys, int *values, int key, int value) {
int index = hash(key);
while (values[index]) { // What does this check for?
index = (index + 1) % SIZE;
}
keys[index] = key;
values[index] = value;
}
int* twoSum(int* nums, int numsSize, int target) {
int keys[SIZE];
int values[SIZE] = {0};
for (int i = 0; i < numsSize; i++) {
int complements = target - nums[i];
int value = search(keys, values, complements);
if (value) { // What does this line check for?
int *indices = (int *) malloc(sizeof(int) * 2);
indices[0] = value - 1;
indices[1] = i;
return indices;
}
insert(keys, values, nums[i], i + 1);
}
return NULL;
}