Example #1
0
int xmlDeleteItem(PXMLNode x,pchar szPath)
{
  PXMLNode t,q,p=xmlGetItem(x,szPath);
  char *name;
  int ok=0;
  if(!p)return 0;
  name=strrchr(szPath,'/');
  if(!name)name=szPath;
  else name++;
  p=p->pParent;
  hashDelete(p->hChildren,name);
  q=p->pChildren;
  t=NULL;
  while(q)
  {
    if(!strcmp(q->szName,name))
    {
      if(t)
      {
        t->pNext=q->pNext;
      }else
      {
        p->pChildren=q->pNext;
      }
      t=q->pNext;
      xmlFree(q);
      q=t;
      ok++;
    }
    t=q;
    if(q)q=q->pNext;
  }
  return ok;
}
int main()
{
	int n,i,j;
 	scanf("%d%d", &n, &k);
	hashInit();
	
	scanf("%d", A+1);
	amax=(amin=A[1]);
 	for(i=2; i<=n; i++)
 	{
	 	scanf("%d", A+i);
		if(A[i]>amax) amax=A[i];
		if(A[i]<amin) amin=A[i];
	}
	for(j=1; j<k; j++)
		hashInsert(A[j]);
	for(i=1; j<n; i++, j++)
	{
		hashInsert(A[j]);
		A[i-1]=findMax();
		printf("%d ",findMin());
		hashDelete(A[i]);
	}
	hashInsert(A[j]);
	A[i-1]=findMax();
	printf("%d\n",findMin());

	for(i=0; i<n-k; i++)
		printf("%d ",A[i]);
	printf("%d\n",A[i]);
	scanf("%d", A);
	return 0;
}
Example #3
0
void streamDelete(struct tuple4 *addr) {
	struct stream *s;
	if (!(s = hashDelete(addr))) {
		if (debug > 2) printf("nidsCallback(): Oops: Attempted to close unexisting connection. Ignoring.\n");
		return;
	}
	streamClose(s);
	free(s);
}
Example #4
0
PUBLIC int websRemoveUser(char *username) 
{
    WebsKey     *key;
    
    assure(username && *username);
    if ((key = hashLookup(users, username)) != 0) {
        freeUser(key->content.value.symbol);
    }
    return hashDelete(users, username);
}
Example #5
0
/*
    Does not recompute abilities for users that use this role
 */
PUBLIC int websRemoveRole(char *name) 
{
    WebsRole    *rp;
    WebsKey     *sym;

    assure(name && *name);
    if (roles) {
        if ((sym = hashLookup(roles, name)) == 0) {
            return -1;
        }
        rp = sym->content.value.symbol;
        hashFree(rp->abilities);
        wfree(rp);
        return hashDelete(roles, name);
    }
    return -1;
}
Example #6
0
int maxMatch(size_t value, size_t *ptr, size_t n)
{
	/*
	 * c: the current maxMatch's length
	 * r: the last bytes save the match's length
	 *    the firsh bytes save the match's index
	 *    if the c=5 and the recall=ptr-m=40, so
	 *    r=40<<8+5
	 */

	size_t c=0,r=0,*p=hTable[value];

	/*
	 * m: the maxMatch's index and the l represent the current
	 * match length
	 *
	 */

	size_t i,j,m,l,s;

	if(*ptr==n||p[0]==0){
		r=(size_t)buffer[*ptr];
		hashInsert(buffer[*ptr],*ptr);
		if(*ptr>SLIDEWIN-1)
			hashDelete(buffer[*ptr-SLIDEWIN],*ptr-SLIDEWIN);
		(*ptr)++;
		//return((int)r);
	}else{
		
		/*
		 * version 1.2
		 *

		j=m=hTable[value][1+(rand()%(hTable[value][0]))];
		s=*ptr;
		c=0;
		while(buffer[j]==buffer[s]){
			j++;
			s++;
			c++;
			if(s>=n||c>=((2<<MATCHLEN-1)-1))
				break;
		}
		*/

		for(i=1;i<=p[0];i++){
			l=0;
			s=*ptr;
			j=p[i];

			while(buffer[j]==buffer[s]){
				j++;
				s++;
				l++;
				if(s>=n||l>=((2<<MATCHLEN-1)-1))
					break;
			}

			if(l>c){
				c=l;
				m=j-l;
			}
		}
		r=*ptr-m;
		r=(r<<MATCHLEN)+c;
	
	/*
	 * insert the match string with the last one to hash
	 * table and delete
	 * the same length of string from hash table header
	 *
	 */

		for(i=0;i<c;i++){
			hashInsert(buffer[*ptr],*ptr);
			if(*ptr>SLIDEWIN-1)
				hashDelete(buffer[*ptr-SLIDEWIN],*ptr-SLIDEWIN);
			(*ptr)++;
		}

		r=r<<8;
	}

	return((int)r);
}
Example #7
0
int main(int argc, char const *argv[]) {

	// default to a table of 1,000 elements.
	size_t size = 1000;

	if (argc > 1) {
		size = atoi(argv[1]);
		if (!size) {
			// couldn't convert argv[1] to a number
			fprintf(stderr, USAGE);
			exit(EXIT_FAILURE);
		}
	}

	// create a new hash table
	Hash *h = hashCreate(size);

	if (!h) {
		// failed to create hash table
		fprintf(stderr, "failed to create hash table\n");
		exit(EXIT_FAILURE);
	} else {
		printf("Successfully created a hash table of size %zu.\n",size);
		FILE *opt;
		if ((opt = fopen(OPTIONS_FILE,"r"))) {
			char c;
			while((c = fgetc(opt)) != EOF) {
				putchar(c);
			}
			fclose(opt);
		} else {
			fprintf(stderr, "failed to open %s\n", OPTIONS_FILE);
			exit(EXIT_FAILURE);
		}
	}

	// create some variables
	char *command, *key, *obj, *res;
	
	bool hasSet,hasGet,onOwn;
	hasSet = hasGet = onOwn = false;

	bool keepGoing = true;

	while(keepGoing) {
		// initialize the variables and print the prompt
		command = key = obj = res = NULL;

		// encourage the user to try out the different commands
		if (!hasSet) {
			printf("%% (type 'set') ");
		} else if (!hasGet) {
			printf("%% (type 'get') ");
		} else if (!onOwn) {
			onOwn = true;
			printf("You're on your own now... available commands: set, get, delete, load\n");
			printf("%% ");
		} else {
			printf("%% ");
		}

		// read the command
		command = readString();

		// process a set command
		if (!strcasecmp(command,"set")) {
			hasSet = true;
			printf("\tkey: ");
			key = readString();

			printf("\tvalue: ");
			obj = readString();

			printf("\tresult: ");
			printf(hashSet(h,key,obj) ? "SUCCESS\n" : "FAILURE\n");

		// process a get command
		} else if (!strcasecmp(command,"get")) {
			hasGet = true;
			printf("\tkey: ");
			key = readString();
			if ((res = (char *) hashGet(h,key))){
				printf("\tresult: %s\n", res);
			} else {
				printf("\tresult: NULL\n");
			}

		// process a delete command
		} else if (!strcasecmp(command,"delete")) {
			printf("\tkey: ");
			key = readString();
			if ((res = (char *) hashDelete(h,key))){
				printf("\tresult: %s\n", res);
				free(res);
			} else {
				printf("\tresult: NULL\n");
			}

		// process a load command
		} else if (!strcasecmp(command,"load")) {
			printf("\tresult: %f\n", hashLoad(h));

		// let the user exit
		} else if (!strcasecmp(command,"exit") || !strcasecmp(command,"quit")) {
			keepGoing = false;			

		// invalid command
		} else if (strlen(command) > 0) {
			printf("invalid command\n");
		} 

		// free the command and key, as necessary
		if (command) { free(command); }
		if (key) { free(key); }

	}

	hashDestroy(h,destroy);
	return EXIT_SUCCESS;
}
Example #8
0
int main() {

  printf("Hello\n");
  printf("Enter a command followed by a number between 0 and 999 inclusive to perform operation on hash table\n");
  printf("Valid commands are: insert #, delete #, search #, list #.\n");
  printf("Enter searchlist to print entire table\n");
  printf("Enter term to print table and exit .\n");
  
  Hashtable * newTable = makeTable(10);
  char * regex = " \n";

  char str [100];
  while(1) {
  
    printf("Enter: ");
    char * result = fgets(str, 100, stdin);
    char * token1 = strtok(result, regex);
    char * token2 = strtok(NULL, regex);
    
    long long val;
    if(token2 != NULL) {
      char * end_ptr = 0;
      val = strtoll(token2,&end_ptr,10);
      if((end_ptr != 0 && *end_ptr != '\n' && *end_ptr != '\0') || overflow(val)){
	printf("Error: invalid input. %s is outside valid range (0 - 999)\n", token2);
	continue;
      }
    }

    if(token1 == NULL) {
      printf("Error: invalid input. \n");
      continue;
    } else if(strcasecmp(token1,"searchlist") == 0) {
      hashListFull(newTable);
    } else if(strcasecmp(token1, "term") == 0) {
      hashListFull(newTable);
      term(newTable);
      printf("You've been terminated\n");
      break;
    } else if(strcasecmp(token1,"insert") == 0) {
      if(token2 == NULL) {
        printf("Error: No number listed\n");
        continue;
      }
      if(hashInsert(val,newTable)) {
	printf("True \n");
      } else {
	printf("False \n");
      }
    }else if(strcasecmp(token1,"search") == 0) {
      if(token2 == NULL) {
        printf("Error: No number listed\n");
        continue;
      }
      if(search(val,newTable)) {
	printf("True\n");
      } else {
	printf("False\n");
      }
    }else if(strcasecmp(token1,"delete") == 0){
      if(token2 == NULL) {
        printf("Error: No number listed\n");
        continue;
      }
      if(hashDelete(val,newTable)) {
	printf("True \n");
      } else {
	printf("False\n");
      }
    }else if(strcasecmp(token1, "list") == 0) {
      if(token2 == NULL) {
	printf("Error: No index listed\n");
	continue;
      }  
      if(isNull(hash(val),newTable)) {
	printf("False\n");
      }else {
	printf("List #%i :", hash(val));
	hashList(hash(val),newTable);
	printf("\n");
      }  
    }else{
      printf("Error: invalid input. %s is not recognized keyword: insert, search,list,delete\n", token1);
      continue;
    }
  }
  
  return 0;
}