Example #1
0
/* Desc: Inserts key, value pair into the hash map
 * Pre:  ht is not null
 * Post: new link with key k and value v added
 */
void insertMap (struct hashMap * ht, KeyType k, ValueType v)
{
    assert( ht != NULL );
    int idx = findKeyIndex( ht, k );

    /* Check if key is already in table */
    if( containsKey( ht, k ) )
    {
        /* Replace the hash link as it already exists */
        ht->table[ idx ]->value = v;
    }
    else
    {
        /* Not in table, so create a hashLink */
        struct hashLink *new_lnk = malloc( sizeof( struct hashLink ) );

        /* Fill hashLink with data */
        new_lnk->value = v;
        new_lnk->key = k;
        new_lnk->next = ht->table[ idx ];

        /* Add the new link to the table */
        ht->table[ idx ] = new_lnk;
        ht->count += 1;
    }
}
Example #2
0
string Mimic::generateText()
{
	stringstream ss;
	stringstream splitter;
	string key = myMap[0].key;
	splitter.str(key);
	string key1, key2;
	splitter >> key1 >> key2;
	string value = myMap[0].randomValue();
	ss << key << " " << value;
	//cout << "Adding " << key << " " << value << "to stream." << endl;
	//cout << "Current contents of stream: " << ss.str() << endl;
	while(value != "THE_END")
	{
		//int randomKeyIndex = rand() % myMap.size();
		//key2 = value;
		//string oldValue = value;//am trying to figure out how to have "I like to boat" and then the key becomes "to boat" etc. But I'm done for now.
		stringstream splitter1;
		splitter1.str(key);
		splitter1 >> key1 >> key2;
		key = key2 + " " + value;
		value = myMap[findKeyIndex(key)].randomValue();
		if (value == "THE_END")
			break;
		ss << " " << value;
		//cout << "Adding " << value << "to stream." << endl;
	}
	//cout << "This is what I am returning:" << ss.str();
	return ss.str();
	
}
Example #3
0
void zStr::getText(long offset, char **idxbuf, char **buf) const {
	char *ch;
	char *idxbuflocal = 0;
	getKeyFromIdxOffset(offset, &idxbuflocal);
	__u32 start;
	__u32 size;

	do {
		idxfd->seek(offset, SEEK_SET);
		idxfd->read(&start, 4);
		idxfd->read(&size, 4);
		start = swordtoarch32(start);
		size = swordtoarch32(size);

		*buf = (*buf) ? (char *)realloc(*buf, size*2 + 1) : (char *)malloc(size*2 + 1);
		*idxbuf = (*idxbuf) ? (char *)realloc(*idxbuf, size*2 + 1) : (char *)malloc(size*2 + 1);
		memset(*buf, 0, size + 1);
		memset(*idxbuf, 0, size + 1);
		datfd->seek(start, SEEK_SET);
		datfd->read(*buf, (int)(size));

		for (ch = *buf; *ch; ch++) {		// skip over index string
			if (*ch == 10) {
				ch++;
				break;
			}
		}
		memmove(*buf, ch, size - (unsigned long)(ch-*buf));

		// resolve link
		if (!strncmp(*buf, "@LINK", 5)) {
			for (ch = *buf; *ch; ch++) {		// null before nl
				if (*ch == 10) {
					*ch = 0;
					break;
				}
			}
			findKeyIndex(*buf + 6, &offset);
		}
		else break;
	}
	while (true);	// while we're resolving links

	if (idxbuflocal) {
		__u32 localsize = strlen(idxbuflocal);
		localsize = (localsize < (size - 1)) ? localsize : (size - 1);
		strncpy(*idxbuf, idxbuflocal, localsize);
		(*idxbuf)[localsize] = 0;
		free(idxbuflocal);
	}
	__u32 block = 0;
	__u32 entry = 0;
	memmove(&block, *buf, sizeof(__u32));
	memmove(&entry, *buf + sizeof(__u32), sizeof(__u32));
	block = swordtoarch32(block);
	entry = swordtoarch32(entry);
	getCompressedText(block, entry, buf);
}
Example #4
0
/* Desc: Removes the hashlink with key k
 * Pre:  ht is not null
 * Post: link freed
 */
void removeKey (struct hashMap * ht, KeyType k)
{
    assert( ht != NULL );

    if( containsKey( ht, k ) )
    {
        free( ht->table[ findKeyIndex( ht, k ) ] );
        ht->count -= 1;
    }
}
Example #5
0
	void AnimationChannel::getInterpolatedTransform(float t, glm::mat4 &res)
	{
		glm::uvec3 key = glm::uvec3(static_cast<unsigned int>(t));
		glm::uvec3 nextKey = glm::uvec3(key.x + 1);

		if (nextKey.z >= translation.size() || nextKey.x >= scale.size() || nextKey.y >= rotation.size())
			findKeyIndex(t, key, nextKey);

		res = glm::translate(glm::mat4(1), glm::mix(translation[key.z].value, translation[nextKey.z].value, (t - translation[key.z].time) / translation[key.z].deltaTime));
		res = glm::scale(res, glm::mix(scale[key.x].value, scale[nextKey.x].value, (t - scale[key.x].time) / scale[key.x].deltaTime));
		res *= glm::mat4_cast(glm::normalize(glm::slerp(rotation[key.y].value, rotation[nextKey.y].value, (t - rotation[key.y].time) / rotation[key.y].deltaTime)));
	}
Example #6
0
/* Desc: "boolean" function answering if k is in ht
 * Pre:  ht is not NULL
 * Post: returns truth status
 */
int containsKey (struct hashMap * ht, KeyType k)
{
    assert( ht != NULL );

    struct hashLink *ptr = ht->table[ findKeyIndex( ht, k ) ];
    while( ptr != NULL )
    {
        if( compare( ptr->key, k ) == 0 )
        {
            return( 1 );
        }
        ptr = ptr->next;
    }

    return( 0 );
}
Example #7
0
/* Desc: Returns a pointer to the value stored at key
 * Pre:  ht is not null
 * Post: pointer to value found
 */
ValueType* atMap (struct hashMap * ht, KeyType k)
{
    assert( ht != NULL );

    struct hashLink *ptr = ht->table[ findKeyIndex( ht, k ) ];
    while( ptr != NULL )
    {
        if( compare( ptr->key, k ) == 0 )
        {
            return( &( ptr->value ) );
        }
        ptr = ptr->next;
    }

    /* If no value, return null */
    return NULL;
}
Example #8
0
void zStr::setText(const char *ikey, const char *buf, long len) {

	static const char nl[] = {13, 10};

	__u32 start, outstart;
	__u32 size, outsize;
	__s32 endoff;
	long idxoff = 0;
	__s32 shiftSize;
	char *tmpbuf = 0;
	char *key = 0;
	char *dbKey = 0;
	char *idxBytes = 0;
	char *outbuf = 0;
	char *ch = 0;

	len = (len < 0) ? strlen(buf) : len;
	stdstr(&key, ikey, 3);
	if (!caseSensitive) toupperstr_utf8(key, strlen(key)*3);

	char notFound = findKeyIndex(ikey, &idxoff, 0);
	if (!notFound) {
		getKeyFromIdxOffset(idxoff, &dbKey);
		int diff = strcmp(key, dbKey);
		if (diff < 0) {
		}
		else if (diff > 0) {
			idxoff += IDXENTRYSIZE;
		}
		else if ((!diff) && (len > 0 /*we're not deleting*/)) { // got absolute entry
			do {
				idxfd->seek(idxoff, SEEK_SET);
				idxfd->read(&start, 4);
				idxfd->read(&size, 4);
				start = swordtoarch32(start);
				size = swordtoarch32(size);

				tmpbuf = new char [ size + 2 ];
				memset(tmpbuf, 0, size + 2);
				datfd->seek(start, SEEK_SET);
				datfd->read(tmpbuf, size);

				for (ch = tmpbuf; *ch; ch++) {		// skip over index string
					if (*ch == 10) {
						ch++;
						break;
					}
				}
				memmove(tmpbuf, ch, size - (unsigned long)(ch-tmpbuf));

				// resolve link
				if (!strncmp(tmpbuf, "@LINK", 5) && (len)) {
					for (ch = tmpbuf; *ch; ch++) {		// null before nl
						if (*ch == 10) {
							*ch = 0;
							break;
						}
					}
					findKeyIndex(tmpbuf + IDXENTRYSIZE, &idxoff);
					delete [] tmpbuf;
				}
				else break;
			}
			while (true);	// while we're resolving links
		}
	}

	endoff = idxfd->seek(0, SEEK_END);

	shiftSize = endoff - idxoff;

	if (shiftSize > 0) {
	        idxBytes = new char [ shiftSize ];
		idxfd->seek(idxoff, SEEK_SET);
		idxfd->read(idxBytes, shiftSize);
	}

	outbuf = new char [ len + strlen(key) + 5 ];
	sprintf(outbuf, "%s%c%c", key, 13, 10);
	size = strlen(outbuf);
	if (len > 0) {	// NOT a link
		if (!cacheBlock) {
			flushCache();
			cacheBlock = new EntriesBlock();
			cacheBlockIndex = (zdxfd->seek(0, SEEK_END) / ZDXENTRYSIZE);
		}
		else if (cacheBlock->getCount() >= blockCount) {
			flushCache();
			cacheBlock = new EntriesBlock();
			cacheBlockIndex = (zdxfd->seek(0, SEEK_END) / ZDXENTRYSIZE);
		}
		__u32 entry = cacheBlock->addEntry(buf);
		cacheDirty = true;
		outstart = archtosword32(cacheBlockIndex);
		outsize = archtosword32(entry);
		memcpy (outbuf + size, &outstart, sizeof(__u32));
		memcpy (outbuf + size + sizeof(__u32), &outsize, sizeof(__u32));
		size += (sizeof(__u32) * 2);
	}
	else {	// link
		memcpy(outbuf + size, buf, len);
		size += len;
	}

	start = datfd->seek(0, SEEK_END);

	outstart = archtosword32(start);
	outsize  = archtosword32(size);

	idxfd->seek(idxoff, SEEK_SET);
	if (len > 0) {
		datfd->seek(start, SEEK_SET);
		datfd->write(outbuf, size);

		// add a new line to make data file easier to read in an editor
		datfd->write(&nl, 2);
		
		idxfd->write(&outstart, 4);
		idxfd->write(&outsize, 4);
		if (idxBytes) {
			idxfd->write(idxBytes, shiftSize);
		}
	}
	else {	// delete entry
		if (idxBytes) {
			idxfd->write(idxBytes+IDXENTRYSIZE, shiftSize-IDXENTRYSIZE);
			idxfd->seek(-1, SEEK_CUR);	// last valid byte
			FileMgr::getSystemFileMgr()->trunc(idxfd);	// truncate index
		}
	}

	if (idxBytes)
		delete [] idxBytes;
	delete [] key;
	delete [] outbuf;
	free(dbKey);
}