tsnaik

appid_hashing

Feb 6th, 2016
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. //#include <appid_sessionstore.c>
  6.  
  7. struct hash *hashTable = NULL;
  8. uint32_t eleCount = 0;
  9.  
  10. struct node
  11. {
  12.     uint32_t ip1, ip2;
  13.     uint16_t p1, p2,sessid;
  14.     //char name[100];
  15.     struct node *next;
  16. };
  17.  
  18. struct hash
  19. {
  20.     struct node *head;
  21.     uint32_t count;
  22. };
  23.  
  24.  
  25.  
  26. uint32_t getIndex(uint32_t ip1, uint32_t ip2, uint16_t p1, uint16_t p2)
  27. {
  28.     return (ip1 + ip2 + p1 + p2) % eleCount;
  29. //  return 1;  
  30.  
  31. }
  32.  
  33. uint32_t isSmaller(uint32_t a, uint32_t b)
  34. {
  35.     if (a < b)
  36.         return 1;
  37.  
  38.     return 0;
  39.     //a < b ? return 1 : return 0;
  40. }
  41.  
  42. /*int isLarger(int a, int b)
  43. {
  44.     a > b ? return 1 : return 0;
  45.     u
  46. }
  47. */
  48. struct node * createhNode(uint32_t ip1, uint32_t ip2, uint16_t p1, uint16_t p2, uint16_t sessid)
  49. {
  50.  
  51.     struct node *newnode;
  52.     newnode = (struct node *) malloc(sizeof(struct node));
  53.     if (isSmaller(ip1, ip2))
  54.     {
  55.         newnode->ip1 = ip1;
  56.         newnode->ip2 = ip2;
  57.  
  58.         newnode->p1 = p1;
  59.         newnode->p2 = p2;
  60.         newnode->sessid = sessid;
  61.     }
  62.     else
  63.     {
  64.         newnode->ip1 = ip2;
  65.         newnode->ip2 = ip1;
  66.  
  67.         newnode->p1 = p2;
  68.         newnode->p2 = p1;
  69.         newnode->sessid = sessid;
  70.     }
  71.  
  72.  
  73.  
  74.     //strcpy(newnode->name, name);
  75.     newnode->next = NULL;
  76.     return newnode;
  77. }
  78.  
  79. int searchInHash(uint32_t ip1, uint32_t ip2, uint16_t p1, uint16_t p2)
  80. {
  81.  
  82.  
  83.     uint32_t swap_ip;
  84.     uint16_t swap_port;
  85.  
  86.     if (!isSmaller(ip1, ip2))
  87.     {
  88.         swap_ip = ip1;
  89.         ip1 = ip2;
  90.         ip2 = swap_ip;
  91.  
  92.         swap_port = p1;
  93.         p1 = p2;
  94.         p2 = swap_port;
  95.     }
  96.  
  97.     uint32_t hashIndex = getIndex(ip1, ip2, p1, p2), flag = 0;
  98.     struct node *myNode=NULL;
  99.     myNode = hashTable[hashIndex].head;
  100.     if (!myNode) {
  101.         printf("Search element unavailable in hash table\n");
  102.         return 0;
  103.     }
  104.     while (myNode != NULL) {
  105.         if (myNode->ip1 == ip1 && myNode->ip2 == ip2 &&
  106.             myNode->p1 == p1 && myNode->p2 == p2)
  107.         {
  108.            /* printf("VoterID  : %d\n", myNode->key);
  109.             printf("Name     : %s\n", myNode->name);
  110.             printf("Age      : %d\n", myNode->age);
  111.             */
  112.             printf("Found %u %u %u %u\n", myNode->ip1, myNode->p1, myNode->ip2, myNode->p2);
  113.             flag = 1;
  114.             break;
  115.         }
  116.         myNode = myNode->next;
  117.     }
  118.     if (!flag)
  119.     {
  120.         printf("Search element unavailable in hash table\n");
  121.         return 0;
  122.     }
  123.     return 1;
  124. }
  125.  
  126.  
  127. int insertToHash(uint32_t ip1, uint32_t ip2, uint16_t p1, uint16_t p2, uint16_t sessid)
  128. {
  129.     if (searchInHash(ip1, ip2, p1, p2) == 1)
  130.     {
  131.         return 0;
  132.     }
  133.     uint32_t hashIndex = getIndex(ip1, ip2, p1, p2);
  134.     struct node *newnode = createhNode(ip1, ip2, p1, p2,sessid);
  135.     /* head of list for the bucket with index "hashIndex" */
  136.     if (!hashTable[hashIndex].head) {
  137.         hashTable[hashIndex].head = newnode;
  138.     insertion(sessid,newnode);   // appid_sessionstore
  139.         hashTable[hashIndex].count = 1;
  140.         return 1;
  141.     }
  142.     /* adding new node to the list */
  143.     newnode->next = (hashTable[hashIndex].head);
  144.     /*
  145.      * update the head of the list and no of
  146.      * nodes in the current bucket
  147.      */
  148.     hashTable[hashIndex].head = newnode;
  149.     insertion(sessid,newnode);
  150.     hashTable[hashIndex].count++;
  151.     return 1;
  152. }
  153.  
  154. void deleteFromHash(int ip1, int ip2, int p1, int p2)
  155. {
  156.     uint32_t swap_ip;
  157.     uint16_t swap_port;
  158.  
  159.     if (!isSmaller(ip1, ip2))
  160.     {
  161.         swap_ip = ip1;
  162.         ip1 = ip2;
  163.         ip2 = swap_ip;
  164.  
  165.         swap_port = p1;
  166.         p1 = p2;
  167.         p2 = swap_port;
  168.     }
  169.    
  170.  
  171.  
  172.     /* find the bucket using hash index */
  173.     uint32_t hashIndex = getIndex(ip1, ip2, p1, p2), flag = 0;
  174.     struct node *temp, *myNode;
  175.     /* get the list head from current bucket */
  176.     myNode = hashTable[hashIndex].head;
  177.     if (!myNode)
  178.     {
  179.         printf("Given data is not present in hash Table!!\n");
  180.         return;
  181.     }
  182.     temp = myNode;
  183.     while (myNode != NULL) {
  184.         /* delete the node with given key */
  185.         if (myNode->ip1 == ip1 && myNode->ip2 == ip2 &&
  186.             myNode->p1 == p1 && myNode->p2 == p2)
  187.         {
  188.             flag = 1;
  189.         deletion(myNode->sessid);
  190.             if (myNode == hashTable[hashIndex].head)
  191.                 hashTable[hashIndex].head = myNode->next;
  192.             else
  193.                 temp->next = myNode->next;
  194.  
  195.             hashTable[hashIndex].count--;
  196.             free(myNode);
  197.             break;
  198.         }
  199.         temp = myNode;
  200.         myNode = myNode->next;
  201.     }
  202.     if (flag)
  203.         printf("Data deleted successfully from Hash Table\n");
  204.     else
  205.         printf("Given data is not present in hash Table!!!!\n");
  206.     return;
  207. }
  208.  
  209. void deleteFromHashbySessId(uint16_t sessid)
  210. {
  211.     struct node *temp = searchHead(sessid);
  212.     deleteFromHash(temp->ip1,temp->ip2,temp->p1,temp->p2);
  213.  
  214.  
  215. }
  216.  
  217. void display()
  218. {
  219.     struct node *myNode;
  220.     uint32_t i;
  221.     for (i = 0; i < eleCount; i++) {
  222.         if (hashTable[i].count == 0)
  223.             continue;
  224.         myNode = hashTable[i].head;
  225.         if (!myNode)
  226.             continue;
  227.         printf("\nData at index %u in Hash Table:\n", i);
  228.         printf("ip1     p1          ip2           p2\n");
  229.         printf("--------------------------------------------\n");
  230.         while (myNode != NULL) {
  231.             printf("%u             ", myNode->ip1);
  232.             printf("%u             ", myNode->p1);
  233.             printf("%u             ", myNode->ip2);
  234.             printf("%u\n", myNode->p2);
  235.             myNode = myNode->next;
  236.         }
  237.     }
  238.     return;
  239. }
  240.  
  241. void initializeHash(uint16_t n)
  242. {
  243.     if(hashTable==NULL)
  244.     {
  245.             hashTable = (struct hash *) calloc(n, sizeof(struct hash));
  246.     }
  247. }
  248.  
  249.  
  250.  
  251. /*int main()
  252. {
  253.     uint32_t n, ip1, ip2, p1,p2;
  254.     int ch;
  255.     printf("Enter the number of index:");
  256.     scanf("%d", &n);
  257.     eleCount = n;
  258.      create hash table with "n" no of buckets
  259.     hashTable = (struct hash *) calloc(n, sizeof(struct hash));
  260.     while (1)
  261.     {
  262.         printf("\n1. Insertion\t2. Deletion\n");
  263.         printf("3. Searching\t4. Display\n5. Exit\n");
  264.         printf("Enter your choice:");
  265.         scanf("%d", &ch);
  266.         switch (ch)
  267.         {
  268.         case 1:
  269.             printf("Enter ip1:");
  270.             scanf("%u", &ip1);
  271.             getchar();
  272.             printf("Enter p1:");
  273.             scanf("%u", &p1);
  274.             getchar();
  275.             printf("Enter ip2:");
  276.             scanf("%u", &ip2);
  277.             getchar();
  278.             printf("Enter p2:");
  279.             scanf("%u", &p2);
  280.             getchar();
  281.           printf("Name:");
  282.             fgets(name, 100, stdin);
  283.             name[strlen(name) - 1] = '\0';
  284.             printf("Age:");
  285.             scanf("%d", &age);
  286.             inserting new node to hash table
  287.            
  288.             if (insertToHash(ip1, ip2, p1, p2))
  289.             {
  290.                 printf("\nInserted\n");
  291.             }
  292.             else
  293.             {
  294.                 printf("\nDid not insert\n");
  295.             }
  296.             break;
  297.  
  298.         case 2:
  299.             printf("Enter ip1:");
  300.             scanf("%u", &ip1);
  301.             getchar();
  302.             printf("Enter p1:");
  303.             scanf("%u", &p1);
  304.             getchar();
  305.             printf("Enter ip2:");
  306.             scanf("%u", &ip2);
  307.             getchar();
  308.             printf("Enter p2:");
  309.             scanf("%u", &p2);
  310.              delete node with "key" from hash table
  311.             deleteFromHash(ip1, ip2, p1, p2);
  312.             break;
  313.  
  314.         case 3:
  315.             printf("Enter ip1:");
  316.             scanf("%u", &ip1);
  317.             getchar();
  318.             printf("Enter p1:");
  319.             scanf("%u", &p1);
  320.             getchar();
  321.             printf("Enter ip2:");
  322.             scanf("%u", &ip2);
  323.             getchar();
  324.             printf("Enter p2:");
  325.             scanf("%u", &p2);
  326.             searchInHash(ip1, ip2, p1, p2);
  327.             break;
  328.         case 4:
  329.             display();
  330.             break;
  331.         case 5:
  332.             exit(0);
  333.         default:
  334.             printf("U have entered wrong option!!\n");
  335.             break;
  336.         }
  337.     }
  338.     return 0;
  339. }*/
Add Comment
Please, Sign In to add comment