//  Insert string subscript  i  into the global hash table.
//  Sequence and information about the string are in
//  global variables  Data, String_Start, String_Info, ....
static
void
Put_String_In_Hash(int i) {
  String_Ref_t  ref = 0;
  char  * p, * window;
  int  kmers_inserted = 0;
  int  skip_ct;
  uint64  key, key_is_bad;
  int  j;

  if  (String_Info [i] . length < Kmer_Len)
    return;

  p = window = Data + String_Start [i];
  key = key_is_bad = 0;
  for  (j = 0;  j < Kmer_Len;  j ++) {
    key_is_bad |= (uint64) (Char_Is_Bad [(int) * p]) << j;
    key |= (uint64) (Bit_Equivalent [(int) * (p ++)]) << (2 * j);
  }

  setStringRefStringNum(ref, i);
  if  (i > MAX_STRING_NUM) {
    fprintf (stderr, "Too many strings for hash table--exiting\n");
    exit (1);
  }
  setStringRefOffset(ref, TRUELY_ZERO);
  skip_ct = 0;
  setStringRefEmpty(ref, TRUELY_ZERO);

  if  (key_is_bad == false) {
    Hash_Insert (ref, key, window);
    kmers_inserted ++;
  }

  while  ((* p) != '\0') {
    window ++;

    {
      String_Ref_t newoff = getStringRefOffset(ref) + 1;
      assert(newoff < OFFSET_MASK);
      setStringRefOffset(ref, newoff);
    }

    if  (++ skip_ct > HASH_KMER_SKIP)
      skip_ct = 0;

    key_is_bad >>= 1;
    key_is_bad |= (uint64) (Char_Is_Bad [(int) * p]) << (Kmer_Len - 1);
    key >>= 2;
    key |= (uint64) (Bit_Equivalent [(int) * (p ++)]) << (2 * (Kmer_Len - 1));

    if  (skip_ct == 0 && ! key_is_bad) {
      Hash_Insert (ref, key, window);
      kmers_inserted ++;
    }
  }
}
Ejemplo n.º 2
0
int Hash_Expand(HashTable hashT) {
    int oldCount = hashT->array_size;
    int newCount = hashT->array_size * 2;
    
    int x;
    HashElem *oldTable = hashT->table;
    HashElem aux, aux2;
    hashT->table = (HashElem *) malloc(sizeof (HashElem) * newCount);
    hashT->array_size = newCount;
    hashT->size = 0;

    for (x = 0; x < newCount; x++) {
        hashT->table[x] = NULL;
    }

    for (x = 0; x < oldCount; x++) {
        aux = oldTable[x];
        while (aux != NULL) {
            Hash_Insert(hashT, aux->data);
            aux2 = aux;
            aux = aux->next;
            free(aux2);
        }
    }
    free(oldTable);

    hashT->array_size = newCount;

    return 0;
}
Ejemplo n.º 3
0
void* thread_code(void *arg)
{
  struct hash_job_desc *jd = (struct hash_job_desc *) arg;
  int numOfOps = NUM_OPS_PER_THREAD;
  int i = 0;
  int *addlist = malloc(sizeof(int) * numOfOps);
  if (addlist == NULL) {
    printf(" cannot allocate more ..., reduce space \n");
    exit(-1);
  }

  while (i < numOfOps) {
    //    addlist[i] = rand() % (numOfOps * num_threads);
    // since key is unique...
    addlist[i] = i + (numOfOps * jd->tid);
    Hash_Insert(&h, addlist + i, addlist[i]);
    (jd->ic)++;
    i++;
  }

  i = 0;
  while (i < numOfOps) {
    if (i % 3 == 0) {
      Hash_Delete(&h, addlist[i]);
      (jd->dc)++;
    }
    i++;
  }

  i = 0;
  while (i < numOfOps) {
    if (Hash_Lookup(&h, addlist[i]) != NULL)
      (jd->lc)++;
    i++;
  }


  //  printf("  END   Thread-%04d with %6d ic, %6d dc, %6d lc\n",
  //         jd->tid, jd->ic, jd->dc, jd->lc);

  free(addlist);

  return NULL;
}
Ejemplo n.º 4
0
int main()
{
    pthread_t thread[3];
    counter_t counter;

    printf("Testing Counter...\n");

    printf("Begin single thread test\n");

    printf("Counter Init\n");
    Counter_Init(&counter, 0);

    printf("Counter_GetValue\n");
    int counter_val = Counter_GetValue(&counter);
    printf("expected 0 got %d\n", counter_val);

    printf("Counter_Increment\n");
    Counter_Increment(&counter);
    counter_val = counter_val = Counter_GetValue(&counter);
    printf("expected 1 got %d\n", counter_val);

    printf("Counter_Decrement\n");
    Counter_Decrement(&counter);
    counter_val = Counter_GetValue(&counter);
    printf("expected 0 got %d\n", counter_val);

    printf("Begin multithreaded test\n");

    printf("Counter Init\n");
    Counter_Init(&counter, 0);

    pthread_create(&thread[0], NULL, Counter_Increment, (void*)&counter);
    printf("Counter is at %d after 1 increment call\n", Counter_GetValue(&counter));
    pthread_create(&thread[1], NULL, Counter_Increment, (void*)&counter);
    printf("Counter is at %d after 2 increment calls\n", Counter_GetValue(&counter));
    pthread_create(&thread[2], NULL, Counter_Increment, (void*)&counter);
    printf("Counter is at %d after 3 increment calls\n", Counter_GetValue(&counter));

    pthread_join(thread[0], NULL);
    pthread_join(thread[1], NULL);
    pthread_join(thread[2], NULL);

    printf("Count after 3 threads = %d\n", Counter_GetValue(&counter));

    printf("Done Testing Counter\n\n");

    list_insert_arg insert_arg[3];
    list_lookup_arg lookup_arg[3];
    list_t list;

    printf("Testing list...\n");

    printf("Begin single-thread test\n");

    printf("List_Init()\n");
    List_Init(&list);

    printf("List_Insert()\n");
    List_Insert(&list, (void *)0x1, 1);
    List_Insert(&list, (void *)0x2, 2);
    List_Insert(&list, (void *)0x3, 3);

    printf("Expected %p got %p\n", (void *)0x1, List_Lookup(&list, 1));
    printf("Expected %p got %p\n", (void *)0x3, List_Lookup(&list, 3));
    printf("Expected %p got %p\n", (void *)0x2, List_Lookup(&list, 2));

    printf("List_Delete()\n");
    List_Delete(&list, 1);

    printf("Expected %p got %p\n", (void *)0x2, List_Lookup(&list, 2));
    printf("Expected %p got %p\n", (void *)0x3, List_Lookup(&list, 3));
    printf("Expected %p got %p\n", NULL, List_Lookup(&list, 1));

    printf("Single-thread test completed\n");

    printf("Begin multi-thread test\n");

    printf("List_Init()\n");
    List_Init(&list);

    printf("Parallel List_Insert()\n");

    insert_arg[0].list = &list;
    insert_arg[0].element = (void *)0x1;
    insert_arg[0].key = 1;

    pthread_create(&thread[0], NULL, pthread_list_insert, &insert_arg[0]);

    insert_arg[1].list = &list;
    insert_arg[1].element = (void *)0x2;
    insert_arg[1].key = 2;

    pthread_create(&thread[1], NULL, pthread_list_insert, &insert_arg[1]);

    insert_arg[2].list = &list;
    insert_arg[2].element = (void *)0x3;
    insert_arg[2].key = 3;

    pthread_create(&thread[2], NULL, pthread_list_insert, &insert_arg[2]);

    pthread_join(thread[0], NULL);
    pthread_join(thread[1], NULL);
    pthread_join(thread[2], NULL);

    printf("Parallel List_Insert() completed\n");

    printf("Parallel List_Lookup()\n");

    lookup_arg[0].list = &list;
    lookup_arg[0].key = 1;

    pthread_create(&thread[0], NULL, pthread_list_lookup, &lookup_arg[0]);

    lookup_arg[1].list = &list;
    lookup_arg[1].key = 2;

    pthread_create(&thread[1], NULL, pthread_list_lookup, &lookup_arg[1]);

    lookup_arg[2].list = &list;
    lookup_arg[2].key = 3;

    pthread_create(&thread[2], NULL, pthread_list_lookup, &lookup_arg[2]);

    pthread_join(thread[0], NULL);
    pthread_join(thread[1], NULL);
    pthread_join(thread[2], NULL);

    printf("Parallel List_Lookup() completed\n");

    printf("List_Delete()\n");
    List_Delete(&list, 1);

    printf("Expected %p got %p\n", (void *)0x2, List_Lookup(&list, 2));
    printf("Expected %p got %p\n", (void *)0x3, List_Lookup(&list, 3));
    printf("Expected %p got %p\n", NULL, List_Lookup(&list, 1));

    printf("Multi-thread test completed\n");

    printf("Done testing list\n\n");

    hash_t hash;

    printf("Testing hash...\n");
    printf("Begin single-thread test\n");

    printf("Hash_Init()\n");
    Hash_Init(&hash, 2);

    printf("Hash_Insert()\n");
    Hash_Insert(&hash, (void *)0x1, 1);
    Hash_Insert(&hash, (void *)0x2, 2);
    Hash_Insert(&hash, (void *)0x3, 3);

    printf("Hash_Lookup()\n");
    printf("Expected %p got %p\n", (void *)0x2, Hash_Lookup(&hash, 2));
    printf("Expected %p got %p\n", (void *)0x3, Hash_Lookup(&hash, 3));
    printf("Expected %p got %p\n", (void *)0x1, Hash_Lookup(&hash, 1));

    printf("Hash_Delete()\n");
    Hash_Delete(&hash, 1);

    printf("Hash_Lookup()\n");
    printf("Expected %p got %p\n", (void *)0x2, Hash_Lookup(&hash, 2));
    printf("Expected %p got %p\n", (void *)0x3, Hash_Lookup(&hash, 3));
    printf("Expected %p got %p\n", NULL, Hash_Lookup(&hash, 1));

    printf("Single-thread test completed\n");
    printf("Done testing hash...\n");

    return 0;
}
Ejemplo n.º 5
0
LRESULT CALLBACK WndProc(
	HWND hWnd,
	UINT nMessage,
	WPARAM wParam,
	LPARAM lParam
	)
{
	RECT ClientRect;
	switch (nMessage)
	{
	case WM_CREATE_IRC_WINDOW:
		if (Hash_Get(pHash, (char*)wParam))
		{
			ContextIRC.hCurrentWindow = Hash_Get(pHash, (char*)wParam);
		}
		else
		{
			static HWND __hWnd__;
			__hWnd__ = Create_Window(__hWnd__, hWnd);
			if (((char*)wParam)[0] == '#')
			{
				ShowWindow(ContextIRC.hCurrentWindow, SW_HIDE);
				ContextIRC.hCurrentWindow = __hWnd__;
				strArray_Add(ChannelNames, szChannel);
				Hash_Insert(pHash, ContextIRC.hCurrentWindow, (char*)wParam);
				ShowWindow(Hash_Get(pHash, (char*)wParam), SW_SHOW);
				ResizeWindows(ContextIRC.dwOldWidth, ContextIRC.dwOldHeight);
				SendMessage(ContextIRC.hCurrentWindow, WM_SETFONT, (WPARAM)ContextIRC.hFont, (LPARAM)TRUE);
			}
			else
			{
				ContextIRC.hCurrentWindow = Array_Index(pArray, Array_Num(pArray));
				ContextIRC.hCurrentWindow = __hWnd__;
				Hash_Insert(pHash, ContextIRC.hCurrentWindow, (char*)wParam);
				ResizeWindows(ContextIRC.dwOldWidth, ContextIRC.dwOldHeight);
				SendMessage(ContextIRC.hCurrentWindow, WM_SETFONT, (WPARAM)ContextIRC.hFont, (LPARAM)TRUE);
			}
			ContextIRC.hCurrentWindow = Array_Index(pArray, Array_Num(pArray));
			Hash_Insert(pHash, ContextIRC.hCurrentWindow, (char*)wParam);
			SendMessage(ContextIRC.hCurrentWindow, WM_SETFONT, (WPARAM)ContextIRC.hFont, (LPARAM)TRUE);
			break;
	}
	case WM_CREATE:
		pArray = Array_Init(pArray);
		ContextIRC.hCurrentWindow = Create_Window(ContextIRC.hCurrentWindow, hWnd);
		ContextIRC.hFont = InitFont(ContextIRC.hCurrentWindow);
		ContextIRC.hTypeBuffer = InitTypeBuffer(ContextIRC.hFont, hWnd);
		ContextIRC.hParentProc = SetWindowLongPtr(ContextIRC.hTypeBuffer, GWL_WNDPROC, (LONG_PTR)TypeBufferProc);
		ContextIRC.hNetworkThread = CreateThread(NULL, 0, Connect, NULL, 0, &ContextIRC.hNetworkThreadID);
		break;
	case WM_DESTROY:
		DestroyContext();
		PostQuitMessage(0);
		break;
	case WM_HOTKEY:
	{
					  register uint32_t nIndex = 0;
					  if (wParam == IRCContext->LeftKey)
					  {
						  if (Array_Num(pArray) <= 0)
							  break;
						  for (nIndex = Array_Num(pArray); nIndex > 0; nIndex--)
						  {
							  if (ContextIRC.hCurrentWindow == Array_Index(pArray, 0))
							  {
								  ShowWindow(ContextIRC.hCurrentWindow, SW_HIDE);
								  ContextIRC.hCurrentWindow = Array_Index(pArray, Array_Num(pArray));

								  strcpy_s(szChannel, 96, strArray_Index(ChannelNames, strArray_Num(ChannelNames)));
								  
								  ShowWindow(ContextIRC.hCurrentWindow, SW_SHOW);
								  MoveWindow(ContextIRC.hCurrentWindow, 0, 0, ContextIRC.dwOldWidth, ContextIRC.dwOldHeight, TRUE);
								  break;
							  }
							  nIndex--;
							  ShowWindow(ContextIRC.hCurrentWindow, SW_HIDE);
							  ContextIRC.hCurrentWindow = Array_Index(pArray, nIndex);

							  strcpy_s(szChannel, 96, strArray_Index(ChannelNames, nIndex - 1));

							  ShowWindow(ContextIRC.hCurrentWindow, SW_SHOW);
							  MoveWindow(ContextIRC.hCurrentWindow, 0, 0, ContextIRC.dwOldWidth, ContextIRC.dwOldHeight, TRUE);
							  break;
						  }
					  }
					  if (wParam == IRCContext->RightKey)
					  {
						  for (nIndex = 0; nIndex <= Array_Num(pArray); nIndex++)
						  {
							  if (Array_Num(pArray) <= 0)
								  break;
							  if (ContextIRC.hCurrentWindow == Array_Index(pArray, nIndex))
							  {
								  if (ContextIRC.hCurrentWindow == Array_Index(pArray, Array_Num(pArray)))
								  {
									  ShowWindow(ContextIRC.hCurrentWindow, SW_HIDE);
									  ContextIRC.hCurrentWindow = Array_Index(pArray, 0);

									  strcpy_s(szChannel, 96, strArray_Index(ChannelNames, 0));

									  ShowWindow(ContextIRC.hCurrentWindow, SW_SHOW);
									  MoveWindow(ContextIRC.hCurrentWindow, 0, 0, ContextIRC.dwOldWidth, ContextIRC.dwOldHeight, TRUE);
									  break;
								  }
								  nIndex++;
								  ShowWindow(ContextIRC.hCurrentWindow, SW_HIDE);
								  ContextIRC.hCurrentWindow = Array_Index(pArray, nIndex);

								  strcpy_s(szChannel, 96, strArray_Index(ChannelNames, nIndex - 1));

								  ShowWindow(ContextIRC.hCurrentWindow, SW_SHOW);
								  MoveWindow(ContextIRC.hCurrentWindow, 0, 0, ContextIRC.dwOldWidth, ContextIRC.dwOldHeight, TRUE);
								  break;
							  }
						  }
					  }
					  break;
	}
	case WM_SIZE:
		ResizeWindows(LOWORD(lParam), HIWORD(lParam) - IRCContext->nTypeHeight);
		GetClientRect(hWnd, &ClientRect);
		ContextIRC.dwOldWidth = LOWORD(lParam);
		ContextIRC.dwOldHeight = HIWORD(lParam);
		MoveWindow(ContextIRC.hTypeBuffer, 0, ClientRect.bottom - IRCContext->nTypeHeight, LOWORD(lParam), IRCContext->nTypeHeight, TRUE);
		break;
	default:
		return DefWindowProc(hWnd, nMessage, wParam, lParam);
	}
	return 0;
}
Ejemplo n.º 6
0
//  Insert string subscript  i  into the global hash table.
//  Sequence and information about the string are in
//  global variables  basesData, String_Start, String_Info, ....
static
void
Put_String_In_Hash(uint32 curID, uint32 i) {
  String_Ref_t  ref = 0;
  int           skip_ct;
  uint64        key;
  uint64        key_is_bad;
  int           j;

  uint32        kmers_skipped  = 0;
  uint32        kmers_bad      = 0;
  uint32        kmers_inserted = 0;

  char *p      = basesData + String_Start[i];
  char *window = basesData + String_Start[i];

  key = key_is_bad = 0;

  for (uint32 j=0;  j<G.Kmer_Len; j ++) {
    key_is_bad |= (uint64) (Char_Is_Bad[(int) * p]) << j;
    key        |= (uint64) (Bit_Equivalent[(int) * (p ++)]) << (2 * j);
  }

  setStringRefStringNum(ref, i);

  if (i > MAX_STRING_NUM)
    fprintf (stderr, "Too many strings for hash table--exiting\n"), exit(1);

  setStringRefOffset(ref, TRUELY_ZERO);

  skip_ct = 0;

  setStringRefEmpty(ref, TRUELY_ZERO);

  if (key_is_bad == false) {
    Hash_Insert(ref, key, window);
    kmers_inserted++;

  } else {
    kmers_bad++;
  }

  while (*p != 0) {
    window++;

    String_Ref_t newoff = getStringRefOffset(ref) + 1;
    assert(newoff < OFFSET_MASK);

    setStringRefOffset(ref, newoff);

    if (++skip_ct > HASH_KMER_SKIP)
      skip_ct = 0;

    key_is_bad >>= 1;
    key_is_bad |= (uint64) (Char_Is_Bad[(int) * p]) << (G.Kmer_Len - 1);

    key >>= 2;
    key  |= (uint64) (Bit_Equivalent[(int) * (p ++)]) << (2 * (G.Kmer_Len - 1));

    if (skip_ct > 0) {
      kmers_skipped++;
      continue;
    }

    if (key_is_bad) {
      kmers_bad++;
      continue;
    }

    Hash_Insert(ref, key, window);
    kmers_inserted++;
  }

  //fprintf(stderr, "STRING %u skipped %u bad %u inserted %u\n",
  //        curID, kmers_skipped, kmers_bad, kmers_inserted);
}