Ejemplo n.º 1
0
int insertElement(char * element,int N)
{
    //! insert an element
    //! returns the number of collisions which occurred before the element was inserted

    int bucket,nrofcol=0;
    bucket=hashFunction(element,nrofcol);
    while(nrofcol<size)
    {
        if(hashTable[bucket]==NULL)
        {
            hashTable[bucket]=element;
            return nrofcol;
        }
        nrofcol++;
        bucket++;
        if(nrofcol>nrmax)
            nrmax=nrofcol;
    }
    if(getFillFactor()>MAX_FILL_FACTOR)
        resizeHashTable(N);
    return 0;
}
Ejemplo n.º 2
0
int main()
{
    int N = 100;
    char ** content = readFromFile(N);
    printContentToConsole(content,N);

    initHashTable(N);

    int MAX=0;

    int i=0,resizeTable=0;
    while(i<N)
    {
        int x=hashFunction(*content,i);
        i++;
        if (insertElement(content[i],x)==-1)
        {
            i=0;
            resizeTable++;
        }
    }


    totalNrOfCollisions=0;
    for(i=0;i<N;i++)
    {
        totalNrOfCollisions+=nrOfCollisions[i];
        printf("\nelement %d : %d collisions",i,nrOfCollisions[i]);
        if (MAX<nrOfCollisions[i])
            MAX=nrOfCollisions[i];
    }
    printf("\nthe maximum nr of collisions is %d", MAX);
    printf("\nthe total number of collisions is %d", totalNrOfCollisions);
    printf("\nthe number of resizes is %d",resizeTable);
    return 0;
}
Ejemplo n.º 3
0
int insertValue(int key, HashTable *numbers){

    int h, i;
    
    h = hashFunction(key, numbers->size);
    i = 0;
	
	while(numbers->table[h].key != 0 && (i < numbers->size)){
		
		if(numbers->table[h].key == key){
			return -1;
		}
		
		h = (h + 1) % numbers->size;
	}
	
	if(numbers->table[h].key == 0){
		numbers->table[h].key = key;
		numbers->table[h].value = h;
		return 0;
	}
	
	return 0;	
}
Ejemplo n.º 4
0
void buildTable(HashTable *hash, char *string)
{
	int index = hashFunction(string, hash->size);
	addListElement(hash->table[index], string);
}
Ejemplo n.º 5
0
size_t hashFunction( const YourString &key )
{
	return hashFunction( key.Value() );
}
Ejemplo n.º 6
0
size_t hashFunction( const std::string &key )
{
	return hashFunction( key.c_str() );
}
Ejemplo n.º 7
0
int getKeyFromFile(char const* file)
{
    return ftok(file, hashFunction(file));
}
Ejemplo n.º 8
0
int main(int argc, char* argv[]) {
    
    int idx;
    idx = hashFunction("hello");
    insert(idx, "hello");
    idx = hashFunction("apple");
    insert(idx, "apple");
    idx = hashFunction("cat");
    insert(idx, "cat");
    idx = hashFunction("caterpiller");
    insert(idx, "caterpiller");
    idx = hashFunction("and");
    insert(idx, "and");
    idx = hashFunction("ant");
    insert(idx, "ant");
    idx = hashFunction("ball");
    insert(idx, "ball");
    idx = hashFunction("airplane");
    insert(idx, "airplane");
    idx = hashFunction("airplane");
    insert(idx, "airplane");
    idx = hashFunction("zoo");
    insert(idx, "zoo");
    idx = hashFunction("cat");
    insert(idx, "cat");
    
    for (int i = 0; i < SIZE; i++) {
        if (hashtable[i] != NULL) {
            printf("This is the %dth index\n", i);
            int counter = 0;
            node* temp = hashtable[i];
            while (temp != NULL) {
                printf("The list element at %d is %s\n", counter, temp->word);
                temp = temp->next;
                counter ++;
            }
        }
    }
}
Ejemplo n.º 9
0
vector<int> getCandidates(HashParam hp, vector<float> sp, const Mat& proj)
{
	vector<int> candidates;
	vector<int> frequency;
	vector<int> hashLocation(hp.bucketNumber, -1);
	vector<int> cursors(hp.bucketNumber, 0);
	vector<int> itemNumber(hp.bucketNumber, 0);
	

	for(int bucket = 0; bucket < hp.bucketNumber; bucket ++)
	{
		hashLocation[bucket] = hashFunction(hp, sp, proj.row(bucket));
	}	
		
	candidates.clear();
	frequency.clear();
	for(int bucket = 0; bucket < hp.bucketNumber; bucket ++)
	{
		cursors[bucket] = 0;
		itemNumber[bucket] =  hp.bucketInfo[bucket][hashLocation[bucket]].size();
	}
	int candidateSize = 0;
	int lastElement = -1;
	//printf("start...get candidtate ...\n");
	while(1)
	{
		bool changeFlag = false;
		int min;
		int minBucket;
		for(int i = 0; i < hp.bucketNumber; i ++)
		{
			if(cursors[i] < itemNumber[i])
			{
				changeFlag = true;
				min = hp.bucketInfo[i][hashLocation[i]][cursors[i]];
				minBucket = i;
				break;
			}
		}
		if(!changeFlag)
			break;
		
		for(int i = minBucket + 1; i < hp.bucketNumber; i ++)
		{
			if(cursors[i] < itemNumber[i] && hp.bucketInfo[i][hashLocation[i]][cursors[i]] < min)
			{
				min = hp.bucketInfo[i][hashLocation[i]][cursors[i]];
				minBucket = i;
			}
		}
		if(min == lastElement)
		{
			frequency[candidateSize-1] ++;
		}
		else
		{
			candidates.push_back(min);
			frequency.push_back(1);
			candidateSize ++;
			lastElement = min;
		}
		cursors[minBucket] ++;
	}
	//printf("end...get candidtate ...\n");
	
	return candidates;
}
Ejemplo n.º 10
0
/*implements A* algorithm*/
tree astar(int x, int y, int d, int heuristic){
	char a = 'X';//X simbolize that no action was taken to get there
	tree t, t1, t2;
	char **mat1, **mat2;
	int i, j;
	int level;
	double f;
	queue_vector aux;
	int k;
	
	//copy the starting matrix
	mat1 = newMatrix();
	for(i = 0; i < n; i++){
		for(j = 0; j < m; j++)
			mat1[i][j] = w[i][j];
	}
	
	t1 = insertTree(NULL, d, x, y, mat1, a, 0);
	t = t1;
	
	//compute the f value according to the heuristic
	f = computeHeuristic(x, y, 0, heuristic);
	
	insertQueue(t1, f);
	
	while(n_queue > 0){
		//remove the first element of the queue
		removeQueue(&aux.t, &aux.f);
		t1 = aux.t;
		
		d = t1 -> dirt;
		mat1 = t1 -> mat;
		a = t1 -> action;
		x = t1 -> x;
		y = t1 -> y;
		level = t1 -> level;
		
		//increments the number of nodes expanded
		expanded++;
		
		//adds into the queue
		if(y != 0 && mat1[y-1][x] != '#'){
			mat2 = newMatrix();
			copyMatrix(mat1, mat2);
			
			a = mat1[y-1][x];
			
			mat2[y-1][x] = '@';
			mat2[y][x] = '_';
			
			if(a == '*')
				k = d-1;
			else
				k = d;
			
			if(checkDuplicate(t, mat2, hashFunction(x, y-1), k) == 0){
				//inserts in lowercase if there is dirt
				if(a == '*'){
					removeListDirt(x, y-1);
					t2 = insertTree(t1, d-1, x, y-1, mat2, 'n', level + 1);
					
					if(d-1 == 0)
						return t2;
				}
				else
					t2 = insertTree(t1, d, x, y-1, mat2, 'N', level + 1);
					
				t1 -> N = t2;
				
				//compute the f value according to the heuristic
				f = computeHeuristic(x, y-1, level+1, heuristic);
					
				insertQueue(t2, f);
				generated++;
			}
		}
			
		if(y != n - 1 && mat1[y+1][x] != '#'){
			mat2 = newMatrix();
			copyMatrix(mat1, mat2);
			
			a = mat1[y+1][x];
			
			mat2[y+1][x] = '@';
			mat2[y][x] = '_';
			
			if(a == '*')
				k = d-1;
			else
				k = d;
			
			if(checkDuplicate(t, mat2, hashFunction(x, y+1), k) == 0){
				//inserts in lowercase if there is dirt
				if(a == '*'){
					removeListDirt(x, y+1);
					t2 = insertTree(t1, d-1, x, y+1, mat2, 's', level + 1);
					
					if(d-1 == 0)
						return t2;
				}
				else
					t2 = insertTree(t1, d, x, y+1, mat2, 'S', level + 1);
				
				t1 -> S = t2;
				
				//compute the f value according to the heuristic
				f = computeHeuristic(x, y-1, level+1, heuristic);
				
				insertQueue(t2, f);
				generated++;
			}
		}
		
		if(x != 0 && mat1[y][x-1] != '#'){
			mat2 = newMatrix();
			copyMatrix(mat1, mat2);
			
			a = mat1[y][x-1];
			
			mat2[y][x-1] = '@';
			mat2[y][x] = '_';
			
			if(a == '*')
				k = d-1;
			else
				k = d;
			
			if(checkDuplicate(t, mat2, hashFunction(x - 1, y), k) == 0){
				//inserts in lowercase if there is dirt
				if(a == '*'){
					removeListDirt(x-1, y);
					t2 = insertTree(t1, d-1, x-1, y, mat2, 'w', level + 1);
					
					if(d-1 == 0)
						return t2;
				}
				else
					t2 = insertTree(t1, d, x-1, y, mat2, 'W', level + 1);
					
				t1 -> W = t2;
				
				//compute the f value according to the heuristic
				f = computeHeuristic(x, y-1, level+1, heuristic);
					
				insertQueue(t2, f);
				generated++;
			}
		}
		
		if(x != m - 1 && mat1[y][x+1] != '#'){
			mat2 = newMatrix();
			copyMatrix(mat1, mat2);
			
			a = mat1[y][x+1];
			
			mat2[y][x+1] = '@';
			mat2[y][x] = '_';
			
			if(a == '*')
				k = d-1;
			else
				k = d;
			
			if(checkDuplicate(t, mat2, hashFunction(x + 1, y), k) == 0){
				//inserts in lowercase if there is dirt
				if(a == '*'){
					removeListDirt(x+1, y);
					t2 = insertTree(t1, d-1, x+1, y, mat2, 'e', level + 1);
					
					if(d-1 == 0)
						return t2;
				}
				else
					t2 = insertTree(t1, d, x+1, y, mat2, 'E', level + 1);
					
				t1 -> E = t2;
				
				//compute the f value according to the heuristic
				f = computeHeuristic(x, y-1, level+1, heuristic);
				
				insertQueue(t2, f);
				generated++;
			}
		}
		
		orderQueue();
	}
	
	return NULL;
}
/* Insert an element into the hash table pH.  The key is pKey,nKey
** and the data is "data".
**
** If no element exists with a matching key, then a new
** element is created.  A copy of the key is made if the copyKey
** flag is set.  NULL is returned.
**
** If another element already exists with the same key, then the
** new data replaces the old data and the old data is returned.
** The key is not copied in this instance.  If a malloc fails, then
** the new data is returned and the hash table is unchanged.
**
** If the "data" parameter to this function is NULL, then the
** element corresponding to "key" is removed from the hash table.
*/
void *sqlite4Fts2HashInsert(
  fts2Hash *pH,        /* The hash table to insert into */
  const void *pKey,    /* The key */
  int nKey,            /* Number of bytes in the key */
  void *data           /* The data */
){
  int hraw;                 /* Raw hash value of the key */
  int h;                    /* the hash of the key modulo hash table size */
  fts2HashElem *elem;       /* Used to loop thru the element list */
  fts2HashElem *new_elem;   /* New element added to the pH */
  int (*xHash)(const void*,int);  /* The hash function */

  assert( pH!=0 );
  xHash = hashFunction(pH->keyClass);
  assert( xHash!=0 );
  hraw = (*xHash)(pKey, nKey);
  assert( (pH->htsize & (pH->htsize-1))==0 );
  h = hraw & (pH->htsize-1);
  elem = findElementGivenHash(pH,pKey,nKey,h);
  if( elem ){
    void *old_data = elem->data;
    if( data==0 ){
      removeElementGivenHash(pH,elem,h);
    }else{
      elem->data = data;
    }
    return old_data;
  }
  if( data==0 ) return 0;
  new_elem = (fts2HashElem*)fts2HashMalloc( sizeof(fts2HashElem) );
  if( new_elem==0 ) return data;
  if( pH->copyKey && pKey!=0 ){
    new_elem->pKey = fts2HashMalloc( nKey );
    if( new_elem->pKey==0 ){
      fts2HashFree(new_elem);
      return data;
    }
    memcpy((void*)new_elem->pKey, pKey, nKey);
  }else{
    new_elem->pKey = (void*)pKey;
  }
  new_elem->nKey = nKey;
  pH->count++;
  if( pH->htsize==0 ){
    rehash(pH,8);
    if( pH->htsize==0 ){
      pH->count = 0;
      fts2HashFree(new_elem);
      return data;
    }
  }
  if( pH->count > pH->htsize ){
    rehash(pH,pH->htsize*2);
  }
  assert( pH->htsize>0 );
  assert( (pH->htsize & (pH->htsize-1))==0 );
  h = hraw & (pH->htsize-1);
  insertElement(pH, &pH->ht[h], new_elem);
  new_elem->data = data;
  return 0;
}
Ejemplo n.º 12
0
bool MyHash::Search (int x, int *probes)
{
    int b=hashFunction(x,probes);   //edopismos theshs gia to x
    if(isEmptyTable[b] || hashTable[b]!=x) return false;
    else return true;
}
Ejemplo n.º 13
0
int main()
{
    node *Table[MAX];
    clearTable(Table);
    int a[MAX];
    char *str;
    /*char in;
    char input[CHARS];*/
    int i, j;
    double t;
    clock_t t1,t2;
    t1=clock();
    for (j=0;j<MAX;j++)
    {
        a[j]=0;
    }
    for (j=0; j<2000; j++)
    {
        str=randstring(CHARS);
        insertValue(Table[hashFunction(str)], str);
        a[hashFunction(str)]++;
    }
    t2=clock();
    t=(double)(t2-t1);
    printf ("The insertion of 2000 elements took %f seconds.", &t);
    printf ("\n");
    for (j=0; j<MAX; j++)
    {
        printf ("%d ", a[j]);
    }

    /*while(1)
    {
        printf("(I)nsert\n(S)earch\n(L)ist all\n\n");
        printf(" > ");
        scanf("%d",&in);
        switch(in)
        {
            case 1:
                printf("\tInserting: \n");
                printf("\t > "); scanf("%s",input);
                Table[hashFunction(input)] = insertValue(Table[hashFunction(input)],input);
                break;
            case 2:
                printf("\tSearching value: \n");
                printf("\t > "); scanf("%s", &input);
                if (hasValue(Table[hashFunction(input)],input))
                {
                    printf("%s is in the table!\n",input);
                }
                else
                {
                    printf("%s is not in the table!\n",input);
                }
                break;
            case 3:
                for (i = 0; i < MAX; i++)
                {
                    printf("[%d] ",i);
                    printList(Table[i]);
                }
                break;
            default:
                break; return;
        }
        printf("_______________________________________________________\n");
    }*/
}
Ejemplo n.º 14
0
void Tid_HashTable::insert(unsigned key, int offset)
{
    unsigned hashed_key;
    hashed_key = hashFunction(key);
    int index = getBucketIndex(hashed_key, globalDepth); // koitaw ta globalDepth deksia bits gia na dw se poio index tha paw
    Bucket* tempBucket = bucketArray.get(index);

    if(tempBucket->empty == true)
    {
        tempBucket->insert(key, offset);
    }
    else
    {
        // An to key yparxei...
        if(tempBucket->key == key)
        {

        }
        else
        {
            unsigned bhashed_key = hashFunction(tempBucket->key);  // Bucket hashed key
            while(getBucketIndex(bhashed_key, globalDepth) == getBucketIndex(hashed_key, globalDepth))
            {
                doubleTableSize();
            }
            int index2 = getBucketIndex(bhashed_key, globalDepth);
            index = getBucketIndex(hashed_key, globalDepth);

            if(tempBucket->localDepth == globalDepth -1)                        // Simple split otan uparxoun 2 pointers sto bucket
            {
                bucketArray.set(index, new Bucket(key, offset, globalDepth));
                bucketArray.get(index2)->localDepth++;
                maxLocalCounter.set(maxLocalCounter.size()-1, maxLocalCounter.get(maxLocalCounter.size()-1)+2);
            }
            else           // Split otan uparxoyn perissoteroi pointers sto bucket
            {
                unsigned local = bucketArray.get(index2)->localDepth;
                while(getBucketIndex(bhashed_key, local) == getBucketIndex(hashed_key, local))  // Oso to localDepth den einai arketo gia na diaxwristoun ta 2 kleidia, auksanetai kai diaxwrizontai ta buckets
                {
                    if(getBucketIndex(bhashed_key, local+1) != getBucketIndex(hashed_key, local+1)) // An sto localDepth+1 diaxwrizetai vges apo loop
                        break;
                    local++;
                    bucketArray.get(index2)->localDepth++;
                    Bucket* newBucket = new Bucket(local);                                        // Neo empty bucket

                    unsigned oldindex = getBucketIndex(hashed_key, local);
                    unsigned newindex;                                              // Ypologismos tou bucket index poy tha diaforopoieitai sto bit pou orizei to neo local
                    unsigned i = pow(2, local-1);
                    if((oldindex & i) == 0)
                        newindex = oldindex + i;
                    else
                        newindex = oldindex - i;
                    unsigned dist = pow(2, local);

                    for(unsigned i = newindex; i < size; i+=dist)   // Metafora deiktwn tou index sto newBucket
                    {
                        bucketArray.set(i, newBucket);
                    }
                }

                local++;
                Bucket* tempBucketnew = new Bucket(key, offset, local);
                bucketArray.set(index, tempBucketnew);
                bucketArray.get(index2)->localDepth++;
                unsigned toindex = getBucketIndex(hashed_key, local);        // Ypologismos tou bucket index me to neo local depth
                unsigned dist = pow(2, local);                               // H apostash poy tha exei to bucket index me ton epomeno deikth poy tha deiksei sto new bucket
                for(unsigned i = toindex; i < size; i+=dist)                 // Metafora deiktwn tou indexTable sto tempBucketnew
                {
                    bucketArray.set(i, tempBucketnew);
                }
                if(local == globalDepth)   // An to local iso me global tote ta 2 bucket exoun localDepth = globalDepth kai to maxLocalCounter auksanetai kata 2
                    maxLocalCounter.set(maxLocalCounter.size()-1, maxLocalCounter.get(maxLocalCounter.size()-1)+2);
            }
        }
    }
}
Ejemplo n.º 15
0
int DiskMultiMap::erase(const std::string& key, const std::string& value, const std::string& context)
{
    int nodesDeleted = 0;
    DiskNode checkValues;
    BinaryFile::Offset bucketLoc = hashFunction(key);
    BinaryFile::Offset locofNode;
    BinaryFile::Offset none = 0;
    tracker.read(locofNode, bucketLoc /*+ sizeof(Header)*/);
    if (locofNode == 0)
        return 0;

    else{
        BinaryFile::Offset current;
        BinaryFile::Offset previous;
        BinaryFile::Offset oldNext;
        DiskNode iter;
        
        Header h;
        tracker.read(h, 0);
        current = locofNode;
        tracker.read(iter, current);
        previous = 0;
        do{
            tracker.read(iter, current);
            tracker.read(h,0);
            oldNext = iter.next;//why does this not return 0?
            if (iter.key == key && iter.value == value && iter.context == context)
            {

                    std::cerr << "Erasing node at location: " << current << std::endl;
                    if (previous == 0) //the first one after the bucket
                    {
                        BinaryFile::Offset newLocofNode = iter.next;
                        tracker.write(newLocofNode, bucketLoc/* + sizeof(Header)*/);
                    }
                    else
                    {
                    DiskNode previousNewLink;
                    tracker.read(previousNewLink, previous);
                    previousNewLink.next = iter.next;
                         tracker.write(previousNewLink, previous);
                    }
                    //next pointer on the deleted node??
                    if (h.freeList == 0)
                    {
                       
                        h.freeList = current;
                        iter.next = none;
                        tracker.write(iter, current);
                        tracker.write(h,0);
                    }
                    else{
                        iter.next = h.freeList;
                        h.freeList = current;
                        tracker.write(iter, current);
                        tracker.write(h,0);
                        }
                    if (previous !=0)
                        previous = current;
                    current = oldNext;
                              nodesDeleted++;
                
            }
            else{
            
            previous = current;
            current = oldNext;
            }
            
        }while(current != 0);

    }
    return nodesDeleted;
}
Ejemplo n.º 16
0
unsigned int IdHash::getTabId(const unsigned & id) const
{
    return readAt(hashFunction(id),id);
}
Ejemplo n.º 17
0
//proj:each row represent which dimensions should projection in this hash,different row represent different bucket
void meanshiftCluster(const Mat& feature, Mat& convexPoints)
{
	printf("start to meanshift point...\n");
	
	time_t startTime = time(0);

	printf("start to compute the convex point for every point ...\n");
	int nl  =  feature.rows;
	int nc  =  feature.cols;
	printf("nl: %d, nc: %d.\n", nl, nc);
	convexPoints.create(nl, nc, CV_32FC1);
	
	//set params;
	MeanShiftParam mp;
	setMeanShiftParams(mp);
	
	#ifdef LSH_NEIGHBOR
		HashParam hp;
		setHashParam(hp, nl, nc);
		Mat proj;

		#ifdef LOAD_HASHINFO_FROM_FILE
			const char* hashFile = "./output/hashInfo.dat";
			readHashInfo(hashFile, proj, hp.bucketInfo);
			for(int i = 0; i < hp.bucketNumber; i ++)
			{
				for(int j = 0; j < hp.projectionSize; j ++)
				{
					int t = proj.at<int>(i, j);
					if(t < 0 || t >= nc)
						printf("i: %d, j: %d, t: %d\n", i, j, t);
				}
			}
		#else
			proj.create(hp.bucketNumber, hp.projectionSize, CV_32SC1);
			boostSample(hp.bucketNumber, hp.projectionSize, proj, nc);
			HashAllItems(feature, proj, hp);
			const char* hashFile = "./output/hashInfo.dat";
			saveHashInfo(hashFile, proj, hp.bucketInfo);
		#endif
		
		printf("bucketNumber: %d, projectionSize: %d.\n", hp.bucketNumber, hp.projectionSize);
		printf("bucket length: %d\n", hp.bucketLength);
		printHashParamInfo(hp);
		//mp.windowRadius = setWindow(hp, feature, proj);
		mp.windowRadius = 50.001144;
		mp.tinyNearestD = mp.windowRadius * 0.1;
		hp.votes = 2;
		
	#else
		boostSample(hp.bucketNumber, hp.projectionSize, proj, nc);
	#endif
	//end of set params;
	
	/********** set window **************/

	
	int initialPoint;
	float demoninator;
	vector<float> molecular(nc, 0);
	vector<float> shiftVector(nc, 0);
	int inNeighbor;

	int iterations;
	float oldChange;
	float newChange;
	bool advanceConvexed;
	int advancedConvexedPoint;
	
	vector<int> candidates;
	vector<int> frequency;
	vector<int> hashLocation(hp.bucketNumber, -1);
	vector<int> cursors(hp.bucketNumber, 0);
	vector<int> itemNumber(hp.bucketNumber, 0);
	
	vector<float> closeness;
	
	printf("start to enter circles for every point ...\n");
	for(int counts = 0; counts < nl; counts ++)
	{
		printf("the %d-th iterations ...\n", counts);
		initialPoint = counts;
		iterations = mp.maxIterations;
		advanceConvexed = false;
		for(int j = 0; j < nc; j ++)
		{
			shiftVector[j] =  feature.at<float>(initialPoint, j);
		}
		
		while(iterations > 0)
		{
			iterations --;
			inNeighbor = 0;
			demoninator = 0;
			for(int j = 0; j < nc; j ++)
			{
				molecular[j] =  0;
			}
			
			#ifdef LSH_NEIGHBOR
			//hash the shift vector and get candidates
			for(int bucket = 0; bucket < hp.bucketNumber; bucket ++)
			{
				//////////////////////////////////////////////////////////
				hashLocation[bucket] = hashFunction(hp, shiftVector, proj.row(bucket));
			}	
				
			candidates.clear();
			frequency.clear();
			for(int bucket = 0; bucket < hp.bucketNumber; bucket ++)
			{
				cursors[bucket] = 0;
				itemNumber[bucket] =  hp.bucketInfo[bucket][hashLocation[bucket]].size();
			}
			int candidateSize = 0;
			int lastElement = -1;
			while(1)
			{
				bool changeFlag = false;
				int min;
				int minBucket;
				for(int i = 0; i < hp.bucketNumber; i ++)
				{
					if(cursors[i] < itemNumber[i])
					{
						changeFlag = true;
						min = hp.bucketInfo[i][hashLocation[i]][cursors[i]];
						minBucket = i;
						break;
					}
				}
				if(!changeFlag)
					break;
			
				for(int i = minBucket + 1; i < hp.bucketNumber; i ++)
				{
					if(cursors[i] < itemNumber[i] && hp.bucketInfo[i][hashLocation[i]][cursors[i]] < min)
					{
						min = hp.bucketInfo[i][hashLocation[i]][cursors[i]];
						minBucket = i;
					}
				}
				if(min == lastElement)
				{
					frequency[candidateSize-1] ++;
				}
				else
				{
					candidates.push_back(min);
					frequency.push_back(1);
					candidateSize ++;
					lastElement = min;
				}
				cursors[minBucket] ++;
			}
			
			candidatesNumber.push_back(candidateSize);
			//printf("tag1\n");
			
			//filter the candidates;
			int statisticalFilted = 0;
			int size = candidateSize;
			
			/*closeness.clear();
			closeness.resize(size, 0.0);
			for(int cc = 0; cc < size; cc ++)
			{
				int near = candidates[cc];
				for(int bucket = 0; bucket < hp.bucketNumber; bucket ++)
					closeness[cc] += weight[bucket] * (hashLocation[bucket] - hp.hashInfo[near][bucket]) ** 2;
				if(closeness[cc] > mp.windowRadius)
					continue;
				
				if(counts > cc && closeness[cc] < mp.tinyNearestD)
				{
					advancedConvexedPoint = cc;
					advanceConvexed = true;	
					float* curent_data = convexPoints.ptr<float>(counts);
					const float* convex_data = convexPoints.ptr<float>(cc);
					for(int j = 0; j < nc; j ++)
						curent_data[j] = convex_data[j];
					break;
				}
				double temp = exp(0 - closeness[cc]);
				demoninator +=  temp;
				for(int j = 0; j < nc; j ++)
				{
					molecular[j] +=  temp * feature.at<float>(candidates[cc], j);
				}
				inNeighbor ++;
			}*/
			
			for(int cc = 0; cc < size; cc ++)
			{
				if(frequency[cc] >= hp.votes)  
				{
				 	statisticalFilted ++;
					float t = l2norm(shiftVector, feature.row(candidates[cc]));
					//indicate the cc point have convexed and the hash value vevy same
					//and their distance is very near, it's convex
					if(counts > cc && t < mp.tinyNearestD)
					{
						advancedConvexedPoint = cc;
						advanceConvexed = true;	
						float* curent_data = convexPoints.ptr<float>(counts);
						const float* convex_data = convexPoints.ptr<float>(cc);
						for(int j = 0; j < nc; j ++)
							curent_data[j] = convex_data[j];
						break;
					}
					if(t < mp.windowRadius)
					{
						double temp = exp(0 - t);
						demoninator +=  temp;
						for(int j = 0; j < nc; j ++)
						{
							molecular[j] +=  temp * feature.at<float>(candidates[cc], j);
						}
						inNeighbor ++;
					}
				}
			}
			printf("candidateSize: %d, statisticalFilted: %d, inNeighbor: %d\n", candidateSize, statisticalFilted, inNeighbor);
			neighborFileterNumber.push_back(statisticalFilted);
			//printf("tag2\n");
			
			#else
			for(int i = 0; i < nl; i ++)
			{
				float t = l2norm(shiftVector, feature.row(i));
				if(t < mp.tinyNearestD && counts > i)
				{
					advancedConvexedPoint = i;
					advanceConvexed = true;	
					float* curent_data = convexPoints.ptr<float>(counts);
					const float* convex_data = convexPoints.ptr<float>(i);
					for(int j = 0; j < nc; j ++)
						curent_data[j] = convex_data[j];
					break;
				}
				if(t < windowRadius)
				{
					double temp = exp(0 - t);
					demoninator +=  temp;
					for(int j = 0; j < nc; j ++)
					{
						molecular[j] +=  temp * feature.at<float>(i, j);
					}
					inNeighbor ++;
				}
			}
			#endif

			if(inNeighbor == 0)
			{
				printf("neighbor: 0\n");
				advanceConvexed = true;
				advancedConvexedPoint = counts;
				float* curent_data = convexPoints.ptr<float>(counts);
				const float* convex_data = feature.ptr<float>(advancedConvexedPoint);
				for(int j = 0; j < nc; j ++)
					curent_data[j] = convex_data[j];
				break;
			}
			if(advanceConvexed)
				break;
				
			demoninator = 1.0/demoninator;
			for(int j = 0; j < nc; j ++)
			{
				molecular[j] = 	demoninator * molecular[j];
			}
			//printf("tag3\n ");
			
			
			newChange = l2norm(shiftVector, molecular);
			convexTrend.push_back(newChange);
			if(newChange / oldChange < mp.epsilon)
				break;
			oldChange = newChange;
			shiftVector = molecular;
			//printf("tag4\n ");
		}
		
		convexTrend.push_back(-1);//seprate by -1
		iters.push_back(mp.maxIterations - iterations);
		
		if(!advanceConvexed)
		{
			float* data = convexPoints.ptr<float>(counts);
			for(int j = 0; j < nc; j ++)
				data[j] = shiftVector[j];
		}   
	}

	string writer = "./output/convex.dat";
	MatrixDataIo mdi(writer.c_str(), true, convexPoints);
	
	#ifdef LSH_NEIGHBOR
	printf("destroy the bucket info ...\n");
	destroyBucket(hp);
	#endif
	
	//compute used time
	{
		time_t endTime = time(0);
		int totalUsed = endTime - startTime;
		//comparedTime[1] = totalUsed;
		shiftTime = totalUsed;
		printf("meanshift LSH: %d\n", totalUsed);
	}	
	
	programPause();
	printf("exit meanshift clustering...\n");
}
Ejemplo n.º 18
0
void SlaveMapper::createMapping(int processID, int numberOfProcesses)
{
	int numberOfReceivedTriples = 0;
	std::set<int> visitedTriples;

	std::tr1::hash<std::string> hashFunction;

	char *message1Master2Slaves = (char*)malloc(GlobalConstants::MAX_CHARS_PER_LINE * sizeof(char));
	int message2All2Slave[4], message2AllToSlaveResult[4];

	std::string separationSymbol = MappingConfigurationFileManager::getInstance()->getSeparationSymbol();
	std::string subject, predicate, object, line, triple, tripleReverse;
	int designedProcess, hashSubj, hashPred, hashObj, hashTriple, hashTripleReverse;

	while (true)
	{
		MPI_Bcast(message1Master2Slaves, GlobalConstants::MAX_CHARS_PER_LINE, MPI_CHAR, GlobalConstants::MASTER, MPI_COMM_WORLD);

		line.assign(message1Master2Slaves);

		if (line.find(INPUT_FILE_OVER) != line.npos)
		{
			break;
		}

		subject = line.substr(0, line.find(separationSymbol));
		line = line.erase(0, subject.length() + separationSymbol.length());
		predicate = line.substr(0, line.find(separationSymbol));
		line = line.erase(0, predicate.length() + separationSymbol.length());
		object = line.substr(0, line.find(separationSymbol));

		triple = subject + separationSymbol + predicate + separationSymbol + object;
		tripleReverse = object + separationSymbol + predicate + separationSymbol + subject;

		hashSubj = hashFunction(subject);
		hashPred = hashFunction(predicate);
		hashObj = hashFunction(object);
		hashTriple = hashFunction(triple);
		hashTripleReverse = hashFunction(tripleReverse);
		designedProcess = ((numberOfReceivedTriples % (numberOfProcesses - 1)) + 1);

		message2All2Slave[0] = (visitedTriples.find(hashTriple) != visitedTriples.end() || visitedTriples.find(hashTripleReverse) != visitedTriples.end()) ? 1 : 0;
		message2All2Slave[1] = (mappingId2Node->find(hashSubj) != mappingId2Node->end()) ? 1 : 0;
		message2All2Slave[2] = (mappingId2Predicate->find(hashPred) != mappingId2Predicate->end()) ? 1 : 0;
		message2All2Slave[3] = (mappingId2Node->find(hashObj) != mappingId2Node->end()) ? 1 : 0;

		MPI_Reduce(message2All2Slave, message2AllToSlaveResult, 4, MPI_INT, MPI_MAX, designedProcess, MPI_COMM_WORLD);

		if (processID == designedProcess && message2AllToSlaveResult[0] == 0)
		{
			if (message2AllToSlaveResult[1] == 0)
			{
				mappingId2Node->operator [](hashSubj) = subject;
			}

			if (message2AllToSlaveResult[2] == 0)
			{
				mappingId2Predicate->operator [](hashPred) = predicate;
			}

			if (message2AllToSlaveResult[3] == 0)
			{
				mappingId2Node->operator [](hashObj) = object;
			}

			predicates->push_back(hashPred);
			objects->push_back(hashObj);
			if (mappingNode2Positions->find(hashSubj) == mappingNode2Positions->end())
			{
				mappingNode2Positions->operator [](hashSubj) = *(new std::vector<int>);
			}
			mappingNode2Positions->operator [](hashSubj).push_back(predicates->size() - 1);

			predicates->push_back(hashPred);
			objects->push_back(hashSubj);
			if (mappingNode2Positions->find(hashObj) == mappingNode2Positions->end())
			{
				mappingNode2Positions->operator [](hashObj) = *(new std::vector<int>);
			}
			mappingNode2Positions->operator [](hashObj).push_back(predicates->size() - 1);

			visitedTriples.insert(hashTriple);
		}

		numberOfReceivedTriples++;
	}

	visitedTriples.clear();
	free(message1Master2Slaves);

	int numberOfNodes = mappingId2Node->size(), numberOfPredicates = mappingId2Predicate->size(), numberOfCorrectTriples = objects->size() / 2;
	MPI_Reduce(&numberOfNodes, NULL, 1, MPI_INT, MPI_SUM, GlobalConstants::MASTER, MPI_COMM_WORLD);
	MPI_Reduce(&numberOfPredicates, NULL, 1, MPI_INT, MPI_SUM, GlobalConstants::MASTER, MPI_COMM_WORLD);
	MPI_Reduce(&numberOfCorrectTriples, NULL, 1, MPI_INT, MPI_SUM, GlobalConstants::MASTER, MPI_COMM_WORLD);

	saveMapping(processID);
}
Ejemplo n.º 19
0
linkedList_t* findInTable(symbol_t symbol, hashTable_ref table, int tableSize)
{
	int index = hashFunction(symbol.text, tableSize);

	return find(symbol, table[index]);
}
Ejemplo n.º 20
0
uint32_t PacaReader::HashString( const std::string& toHash ) const
{
    std::hash<std::string> hashFunction;
    return hashFunction( toHash );
}