Beispiel #1
0
int main(int argc, const char * argv[]) {
    HashTableNode nodes[2] = {{"Pantera",234}, {"Pantera2",223}};
    HashTable * hashTable = newHashMap(nodes, 2);
    hashTableSet(hashTable, "key2", 666);
    hashTableSet(hashTable, "key3", 666);
    printHashTable(hashTable);
    int key2 = hashTableGet(hashTable, "key2");
    printf("\nkey2 %i\n", key2);
    
    return 0;
}
Beispiel #2
0
bool hasUniqueCharacters(char * str) {
    HashTable  * table = newHashMap(NULL, 0);
    char * ptr = &str[0];
    while (ptr != NULL && *ptr != '\0') {
        char temp[2] = {*ptr,'\0'};
        if(hashTableGet(table, temp) == HashTableNotFound){
            hashTableSet(table, temp, 1);
            printHashTable(table);
            ptr++;
        }else{
            printHashTable(table);
            printf("string does not have unique characters, found at least two %c\n", *ptr);
            ptr = NULL;
        }
    }
    
    return false;
}
Beispiel #3
0
Mesh *meshObjIndex(Mesh *mesh)
{
	Mesh *indexed = malloc(sizeof(Mesh));

	indexed->vertices.data = malloc(mesh->indices.length * sizeof(Vec3));
	indexed->uvs.data = malloc(mesh->indices.length * sizeof(Vec2));
	indexed->normals.data = malloc(mesh->indices.length * sizeof(Vec3));
	indexed->indices.data = malloc(mesh->indices.length * sizeof(unsigned));

	indexed->vertices.length = 0;
	indexed->uvs.length = 0;
	indexed->normals.length = 0;
	indexed->indices.length = mesh->indices.length;

	HashTable *indexTable = hashTableNew();

	unsigned index = 0;
	unsigned *duplicate = NULL;

	for (unsigned i = 0; i < mesh->indices.length; i++) {
		unsigned x = i * 3;		

		/**
		 * Create a unique key from all 3 index
		 * variables.
		 */
		int keyLength = snprintf(NULL, 0, "%i/%i/%i", (int)mesh->indices.data[x], (int)mesh->indices.data[x + 1], (int)mesh->indices.data[x + 2]);

		if (keyLength > 0) {
			char key[keyLength + 1];

			sprintf(key, "%i/%i/%i", (int)mesh->indices.data[x], (int)mesh->indices.data[x + 1], (int)mesh->indices.data[x + 2]);

			if ((duplicate = (unsigned*)hashTableGet(indexTable, hashMurmur3(key))) == NULL) {
				if ((int)mesh->indices.data[x] > 0) {
					indexed->vertices.data[index] = mesh->vertices.data[(int)mesh->indices.data[x] - 1];
					indexed->vertices.length++;
				} else if ((int)mesh->indices.data[x] < 0) {
					indexed->vertices.data[index] = mesh->vertices.data[mesh->vertices.length + (int)mesh->indices.data[x]];
					indexed->vertices.length++;
				} else {
					/**
					 * If vertex data is missing nullify 
					 * the mesh and break out.
					 */
					indexed = meshObjEmpty(indexed);
					break;
				}

				if ((int)mesh->indices.data[x + 1] > 0) {
					indexed->uvs.data[index] = mesh->uvs.data[(int)mesh->indices.data[x + 1] - 1];
					indexed->uvs.length++;
				} else if ((int)mesh->indices.data[x + 1] < 0) {
					indexed->uvs.data[index] = mesh->uvs.data[mesh->uvs.length + (int)mesh->indices.data[x + 1]];
					indexed->uvs.length++;
				}

				if ((int)mesh->indices.data[x + 2] > 0) {
					indexed->normals.data[index] = mesh->normals.data[(int)mesh->indices.data[x + 2] - 1];
					indexed->normals.length++;
				} else if ((int)mesh->indices.data[x + 2] < 0) {
					indexed->normals.data[index] = mesh->normals.data[mesh->normals.length + (int)mesh->indices.data[x + 2]];
					indexed->normals.length++;
				}

				indexed->indices.data[i] = index;
				index++;

				hashTableSet(indexTable, hashMurmur3(key), &indexed->indices.data[i]);
			} else {
				indexed->indices.data[i] = *duplicate;
			}
		}
	}

	hashTableFree(indexTable);

	/**
	 * Resize buffers to match data.
	 */
	if (indexed->uvs.length != indexed->vertices.length) {
		indexed->uvs.length = 0;
	}
	
	if (indexed->normals.length != indexed->vertices.length) {
		indexed->normals.length = 0;
	}

	indexed->vertices.data = realloc(indexed->vertices.data, indexed->vertices.length * sizeof(Vec3));
	indexed->uvs.data = realloc(indexed->uvs.data, indexed->uvs.length * sizeof(Vec2));
	indexed->normals.data = realloc(indexed->normals.data, indexed->normals.length * sizeof(Vec3));

	return indexed;
}