bool CTextureRefs::ContainsRef(unsigned fmt, unsigned x, unsigned y, unsigned width, unsigned height) { // Pack texture reference into bitfield unsigned texRef = (fmt&7)<<24|(x&0x7E0)<<13|(y&0x7E0)<<7|(width&0x7E0)<<1|(height&0x7E0)>>5; // Check if using array or hashset if (m_size <= TEXREFS_ARRAY_SIZE) { // See if texture reference held in array for (unsigned i = 0; i < m_size; i++) { if (texRef == m_array[i]) return true; } return false; } else // See if texture reference held in hashset return HashContains(texRef); }
/* Adding a specific string to hashtable * @str: char buffer to add to hash * @hashTab: hash table to add the string */ int HashAdd(char *str, HashTable *hashTab, int curDocID){ // allocate memory for the new node GenHashTableNode *addNode; // see if a word is contained already in the hashtable. // Two cases (word already contained or not) are processed // very differently addNode = HashContains(str, hashTab); if(!addNode){ // the word is not contained in the hashtable. // allocate memory for a hashtable node, WordNode // and DocNode addNode = calloc(1, sizeof(GenHashTableNode)); if(!addNode) return 0; WordNode *addWord; addWord = calloc(1, sizeof(GenHashTableNode)); if(!addWord) return 0; addWord->word = str; // add current word into the WordNode DocNode *addDoc; addDoc = calloc(1, sizeof(DocNode)); if(!addDoc) return 0; // link the DocNode, WordNode, and hashtable node correctly. addDoc->documentID = curDocID; // store documentID passed into function addDoc->occurrences = 1; // count of occurences starts at 1 addWord->docs = addDoc; addNode->hashKey = addWord; // find the index that the word hashes to unsigned long hashValue = JenkinsHash(str, MAX_HASH_SLOT); GenHashTableNode *presentNode = hashTab->table[hashValue]; if(presentNode == NULL){ // didn't find anything in slot, so add the current node // at slot. hashTab->table[hashValue] = addNode; } else{ // found something in the slot, so go down the list until // the last element and append while(presentNode->next != NULL){ presentNode = presentNode->next; } presentNode->next = addNode; } return 1; } else{ // the word was already in the hashtable free(str); // we do not need the actual words anymore so free. // iterate through the DocNode chain stored at the current WordNode // position, while keeping a previous pointer so we can use it later // to append to this DocNode chain DocNode *curDoc = ((WordNode*)addNode->hashKey)->docs; DocNode *prevDoc; for( ; curDoc != NULL; curDoc = curDoc->nextDoc){ if(curDoc->documentID == curDocID){ // if there is a document ID match, we have seen the // word already in a the same document to increment // its occurrence value and return curDoc->occurrences = (curDoc->occurrences + 1); return 1; } prevDoc = curDoc; } DocNode *addDoc; addDoc = calloc(1, sizeof(DocNode)); if(!addDoc) return 0; // we didnt find a DocNode with the same document ID as the current // file so store the document ID into a new DocNode, and the occurrence // of this word in this document is the starting number, 1. addDoc->documentID = curDocID; addDoc->occurrences = 1; // append this new DocNode into the DocNode chain. prevDoc->nextDoc = addDoc; return 1; } }