Example #1
0
File: ctable.c Project: petsc/petsc
PetscErrorCode  PetscTableAddCountExpand(PetscTable ta,PetscInt key)
{
    PetscErrorCode ierr;
    PetscInt       ii      = 0,hash = PetscHash(ta,key);
    const PetscInt tsize   = ta->tablesize,tcount = ta->count;
    PetscInt       *oldtab = ta->table,*oldkt = ta->keytable,newk,ndata;

    PetscFunctionBegin;
    /* before making the table larger check if key is already in table */
    while (ii++ < tsize) {
        if (ta->keytable[hash] == key) PetscFunctionReturn(0);
        hash = (hash == (ta->tablesize-1)) ? 0 : hash+1;
    }

    ta->tablesize = PetscIntMultTruncate(2,ta->tablesize);
    if (tsize == ta->tablesize) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Table is as large as possible; ./configure with the option --with-64-bit-integers to run this large case");
    ierr = PetscMalloc1(ta->tablesize,&ta->table);
    CHKERRQ(ierr);
    ierr = PetscCalloc1(ta->tablesize,&ta->keytable);
    CHKERRQ(ierr);

    ta->count = 0;
    ta->head  = 0;

    /* Build a new copy of the data */
    for (ii = 0; ii < tsize; ii++) {
        newk = oldkt[ii];
        if (newk) {
            ndata = oldtab[ii];
            ierr  = PetscTableAdd(ta,newk,ndata,INSERT_VALUES);
            CHKERRQ(ierr);
        }
    }
    ierr = PetscTableAddCount(ta,key);
    CHKERRQ(ierr);
    if (ta->count != tcount + 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_COR,"corrupted ta->count");

    ierr = PetscFree(oldtab);
    CHKERRQ(ierr);
    ierr = PetscFree(oldkt);
    CHKERRQ(ierr);
    PetscFunctionReturn(0);
}
Example #2
0
PetscErrorCode  PetscTableAddCountExpand(PetscTable ta,PetscInt key)
{
  PetscErrorCode ierr;
  PetscInt       ii      = 0,hash = PetscHash(ta,key);
  const PetscInt tsize   = ta->tablesize,tcount = ta->count;
  PetscInt       *oldtab = ta->table,*oldkt = ta->keytable,newk,ndata;

  PetscFunctionBegin;
  /* before making the table larger check if key is already in table */
  while (ii++ < ta->tablesize) {
    if (ta->keytable[hash] == key) PetscFunctionReturn(0);
    hash = (hash == (ta->tablesize-1)) ? 0 : hash+1;
  }

  /* alloc new (bigger) table */
  if (ta->tablesize == PETSC_MAX_INT/4) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_COR,"ta->tablesize < 0");
  ta->tablesize = 2*tsize;
  if (ta->tablesize <= tsize) ta->tablesize = PETSC_MAX_INT/4;

  ierr = PetscMalloc1(ta->tablesize,&ta->table);CHKERRQ(ierr);
  ierr = PetscCalloc1(ta->tablesize,&ta->keytable);CHKERRQ(ierr);

  ta->count = 0;
  ta->head  = 0;

  /* Build a new copy of the data */
  for (ii = 0; ii < tsize; ii++) {
    newk = oldkt[ii];
    if (newk) {
      ndata = oldtab[ii];
      ierr  = PetscTableAdd(ta,newk,ndata,INSERT_VALUES);CHKERRQ(ierr);
    }
  }
  ierr = PetscTableAddCount(ta,key);CHKERRQ(ierr);
  if (ta->count != tcount + 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_COR,"corrupted ta->count");

  ierr = PetscFree(oldtab);CHKERRQ(ierr);
  ierr = PetscFree(oldkt);CHKERRQ(ierr);
  PetscFunctionReturn(0);
}