Example #1
0
int astFollowposAttribute(struct ast *ast, struct ast_node *node){
	if(node->op == OP_OR){
		astFollowposAttribute(ast, node->child0);
		astFollowposAttribute(ast, node->child1);

		node->followpos = newSet();
		return 0;
	} else if(node->op == OP_CAT){
		astFollowposAttribute(ast, node->child0);
		astFollowposAttribute(ast, node->child1);

		node->followpos = newSet();
		struct set_iterator si;
		int leafNumber;
		si = setIterator(node->child0->lastpos);
		while(nextSetItem(&si, &leafNumber)){
			struct ast_node *leafNode;
			assert(leafNumber < leafCount);
			leafNode = leafIndex[leafNumber];
			setAddSet(leafNode->followpos, node->child1->firstpos);
		}
		return 0;
	} else if(node->op == OP_STAR){
		astFollowposAttribute(ast, node->child0);

		node->followpos = newSet();
		struct set_iterator si;
		int leafNumber;

		si = setIterator(node->lastpos);
		while(nextSetItem(&si, &leafNumber)){
			struct ast_node *leafNode;
			assert(leafNumber < leafCount);
			leafNode = leafIndex[leafNumber];
			setAddSet(leafNode->followpos, node->firstpos);
		}
		return 0;
	} else if(node->op == OP_LITERAL){
		node->followpos = newSet();
		return 0;
	} else if(node->op == OP_EPSILON){
		node->followpos = newSet();
		return 0;
	} else {
		fprintf(stderr, "unknown op: %d\n", node->op);
		exit(1);
	}
}
//set union with set {x}
Set Set::operator+(int x) const
{
    Set newSet(*this);

    if(!newSet.member(x)) { newSet.insert(x); }
    return newSet;
}
Example #3
0
DenseIntVectSet& DenseIntVectSet::operator|=(const DenseIntVectSet& d)
{
  if (m_domain.contains(d.m_domain))
        {
          BoxIterator bit(d.m_domain);
          int i=0;
          for (bit.begin(); bit.ok(); ++bit, ++i)
          {
                if (d.m_bits[i]) m_bits.setTrue(m_domain.index(bit()));
          }
        }
  else if (d.m_domain.contains(m_domain))
        {
          DenseIntVectSet newSet = d;
          BoxIterator bit(m_domain);
          int i=0;
          for (bit.begin(); bit.ok(); ++bit, ++i)
          {
                if (m_bits[i]) newSet.m_bits.setTrue(newSet.m_domain.index(bit()));
          }
          *this = newSet;
        }
  else
        {
          Box newDomain = minBox(m_domain, d.m_domain);
          DenseIntVectSet newSet(newDomain, false);
          newSet |= *this;
          newSet |= d;
          *this = newSet;
        }
  return *this;
}
Example #4
0
void DenseIntVectSet::coarsen(int iref)
{
  if (iref == 1) return;
  CH_assert(iref >= 1);
  // int refinements = iref/2;
  CH_assert((iref/2)*2 == iref); // check iref for power of 2

  Box newDomain(m_domain);
  newDomain.coarsen(iref);
  DenseIntVectSet newSet(newDomain, false);
  BoxIterator bit(m_domain);
  int count=0;
  for (bit.begin(); bit.ok(); ++bit, ++count)
    {
      if (m_bits[count])
        {
          IntVect iv(bit());
          iv.coarsen(iref);
          long index = newDomain.index(iv);
          newSet.m_bits.setTrue(index);
        }
    }

  *this = newSet;
}
Example #5
0
/*
 * Calculates and returns the set theoretic union of two sets. A union creates a set whose elements
 * are all elements from setA and all elements from setB. For this to work, the two sets should
 * contain the same type of elements and the comparison functions for setA and setB should be
 * functionally equivalent as well.
 *
 * Arguments:
 * setA -- The first set in the pair of sets to union
 * setB -- The second set in the pair of sets to union
 * comparisonfunction -- A function to compare the elements in the union. If this is NULL, the
 *                       comparison function from setA will be used.
 *
 * Returns:
 * A set containing all non-equivalent elements from setA and setB.
 */
Set *setUnion( Set *setA, Set *setB, ComparisonFunction comparisonFunction ) {
    // Ensure that the proper comparison function gets used
    if( ! comparisonFunction ) {
        comparisonFunction = setA->elements->comparisonFunction;
    }

    // Get the elements from A & B, create a new Set
    void **elementsA = bstElements( setA->elements );
    void **elementsB = bstElements( setB->elements );
    Set *unionResult = newSet( comparisonFunction );

    // Insert the elements from A into the set
    for( int i = 0; i < setA->size; i++ ) {
        setAdd( unionResult, elementsA[i] );
    }

    // Insert the elements from B into the set
    for( int i = 0; i < setB->size; i++ ) {
        setAdd( unionResult, elementsB[i] );
    }

    // Free the structure from the element vectors
    free( elementsA );
    free( elementsB );

    return unionResult;
}
Example #6
0
int astLastposAttribute(struct ast *ast, struct ast_node *node){
	if(node->op == OP_OR){
		astLastposAttribute(ast, node->child0);
		astLastposAttribute(ast, node->child1);

		node->lastpos = newSetFromUnion(node->child0->lastpos, node->child1->lastpos);
		return 0;
	} else if(node->op == OP_CAT){
		astLastposAttribute(ast, node->child0);
		astLastposAttribute(ast, node->child1);

		if(node->child1->nullable){
			node->lastpos = newSetFromUnion(node->child0->lastpos,
					node->child1->lastpos);
		} else {
			node->lastpos = newSetFromSet(node->child1->lastpos);
		}
		return 0;
	} else if(node->op == OP_STAR){
		astLastposAttribute(ast, node->child0);
		node->lastpos = newSetFromSet(node->child0->lastpos);
		return 0;
	} else if(node->op == OP_LITERAL){
		node->lastpos = newSetFromInteger(node->leafNumber);
		return 0;
	} else if(node->op == OP_EPSILON){
		// TODO: wtf here?
		node->lastpos = newSet();
		return 0;
	} else {
		fprintf(stderr, "invalid op in  nullable()\n");
		exit(1);
	}
}
Example #7
0
// Update stored cell numbers using map.
// Do in two passes to prevent allocation if nothing changed.
void topoSet::topoSet::updateLabels(const labelList& map)
{
    // Iterate over map to see if anything changed
    bool changed = false;

    for
    (
        labelHashSet::const_iterator iter = begin();
        iter != end();
        ++iter
    )
    {
        if ((iter.key() < 0) || (iter.key() > map.size()))
        {
            FatalErrorIn
            (
                "topoSet::updateLabels(const labelList&, labelHashSet)"
            )   << "Illegal content " << iter.key() << " of set:" << name()
                << " of type " << type() << endl
                << "Value should be between 0 and " << map.size()-1
                << abort(FatalError);
        }

        label newCellI = map[iter.key()];

        if (newCellI != iter.key())
        {
            changed = true;

            break;
        }
    }

    // Relabel (use second Map to prevent overlapping)
    if (changed)
    {
        labelHashSet newSet(2*size());

        for
        (
            labelHashSet::const_iterator iter = begin();
            iter != end();
            ++iter
        )
        {
            label newCellI = map[iter.key()];

            if (newCellI >= 0)
            {
                newSet.insert(newCellI);
            }
        }

        transfer(newSet);
    }
}
Example #8
0
int main(){
	uset intset;
	newSet(&intset,CHAR);
	int i;
	for(i=0;i<2000;i++){
		addElem(&i,&intset);
	}
	for(i=2100;i>1500;i--){
		remElem(&i,&intset);
	}
	deletSet(&intset);
	return 0;
}
Example #9
0
int main(int argc, char **argv)
{
	int N = (argc < 2) ? 20 : atoi(argv[1]);
	if (N < 20) N = 20;
	Set s;
	s = newSet();
	int i;
	char x[50];
	for (i = 0; i < N; i++) {
		randomString(x);
		insertInto(s,x);
		printf("Insert %s\n",x);
		showSet(s);
	}
	disposeSet(s);
	return 0;
}
Example #10
0
/*
 * Creates a new set where the elements in the set derived by applying the map function to every
 * element in the set passed to the function
 *
 * Arguments:
 * set                -- The set whose elements will be mapped over
 * function           -- The function that will be applied to every element in the set. This
 *                       function should allocate new space for the result rather than overwrite the
 *                       space already present.
 * comparisonfunction -- A function that can be used to compare elements in the codomain of the
 *                       mapping function. This can safely be set to NULL if it is known that the
 *                       codomain is the set as the domain of the mapping function. If this is NULL
 *                       and the codomain is different, the behavior is unspecified and could lead
 *                       to errors and crashes depending on the nature of the original set's
 *                       comparison function.
 *
 * Returns:
 * A new set containing every element within the original set after the function has been applied.
 */
Set *setMap( Set *set, MapFunction function, ComparisonFunction comparisonFunction) {
    if( ! comparisonFunction ) {
        comparisonFunction = set->elements->comparisonFunction;
    }

    // Create the new set and get the elements from the old set
    Set *result = newSet( comparisonFunction );
    void **elements = bstElements( set->elements );

    // Apply the function to each element in the old set, then add it to the new set
    for( int i = 0; i < set->size; i++ ) {
        void *element = function( elements[i] );
        setAdd( result, element );
    }

    free( elements );
    return result;
}
Example #11
0
File: save.c Project: BPaden/garglk
/*----------------------------------------------------------------------*/
static void restoreSets(AFILE saveFile) {
  SetInitEntry *initEntry;

  if (header->setInitTable != 0)
    for (initEntry = (SetInitEntry *)pointerTo(header->setInitTable);
	 !isEndOfArray(initEntry); initEntry++) {
      Aint setSize;
      Set *set;
      int i;

      fread((void *)&setSize, sizeof(setSize), 1, saveFile);
      set = newSet(setSize);
      for (i = 0; i < setSize; i++) {
	Aword member;
	fread((void *)&member, sizeof(member), 1, saveFile);
	addToSet(set, member);
      }
      setInstanceAttribute(initEntry->instanceCode, initEntry->attributeCode, (Aptr)set);
    }
}
Example #12
0
/*
 * Calculates the set theoretic intersection of two sets. An intersection creates a set whose
 * elements are present in setA AND present in setB. For this to work, the two sets should
 * contain the same type of elements and the comparison functions for setA and setB should be
 * functionally equivalent as well.
 *
 * Arguments:
 * setA -- The first set in the pair of sets to intersection
 * setB -- The second set in the pair of sets to intersection
 * comparisonfunction -- A function to compare the elements in the union. If this is NULL, the
 *                       comparison function from setA will be used.
 *
 * Returns:
 * A set containing all elements present in both sets.
 */
Set *setIntersect( Set *setA, Set *setB, ComparisonFunction comparisonFunction ) {
    // Ensure that the proper comparison function gets used
    if( ! comparisonFunction ) {
        comparisonFunction = setA->elements->comparisonFunction;
    }

    Set *intersectionResult = newSet( comparisonFunction );

    // Iterate over the smaller set
    if( setA->size <= setB->size ) {
        void **elements = bstElements( setA->elements );

        for( int i = 0; i < setA->size; i++ ) {
            void *element = elements[i];

            // If the element is in both sets, add it
            if( isInSet( setB, element ) ) {
                setAdd( intersectionResult, element );
            }
        }

        free( elements );
    } else {
        void **elements = bstElements( setB->elements );

        for( int i = 0; i < setB->size; i++ ) {
            void *element = elements[i];

            // If the element is in both sets, add it
            if( isInSet( setA, element ) ) {
                setAdd( intersectionResult, element );
            }
        }

        free( elements );
    }

    return intersectionResult;
}
Example #13
0
void DenseIntVectSet::refine(int iref)
{
  if (iref == 1) return;
  if (isEmpty()) return;
  CH_assert(iref >= 1);
  //int refinements = iref/2;
  CH_assert((iref/2)*2 == iref); // check iref for power of 2
  Box newDomain(m_domain);
  newDomain.refine(iref);
  DenseIntVectSet newSet(newDomain, false);
  IntVect iv;
  BoxIterator bit(newDomain);
  int count=0;
  for (bit.begin(); bit.ok(); ++bit, ++count)
    {
      iv = bit();
      iv.coarsen(iref);
      if (this->operator[](iv))
        {
          newSet.m_bits.setTrue(count);
        }
    }
  *this = newSet;
}
//set difference with set {x}
Set Set::operator-(int x) const
{
    Set newSet(*this);
    if(newSet.member(x)){ newSet.deleteNode(x); }
    return newSet;
}
TEST(Expression_SetRandom, setComprehension)
{
	int arr[] = { 1, 1, 2, 8, 15 };
	TVP S = newSet(5, arr); //Technically this isn't a set but we need this for testing
	TVP t = newInt(10);

	//{ exp | v in set S . P}

	TVP comp = NULL;
	{
		//S, check and unwrap
		assert(S->type == VDM_SET && "Value is not a set");
		UNWRAP_COLLECTION(col, S);

		int count = 0;
		int size = DEFAULT_SET_COMP_BUFFER;
		struct TypedValue** buf = (struct TypedValue**) calloc(size, sizeof(struct TypedValue*));

		for (int i = 0; i < col->size; i++)
		{
			TVP v= col->value[i]; // set binding

			TVP cond =vdmLessOrEqual(v,t);//P or NULL if none
			if(cond==NULL || cond->value.boolVal)
			{
				if(count>=size)
				{
					//buffer too small add memory chunk
					size+=(DEFAULT_SET_COMP_BUFFER_STEPSIZE*sizeof(struct TypedValue*));
					buf = (struct TypedValue**)realloc(buf,size);
				}
				TVP element = vdmClone(v); // exp, the vdmClone here will be inside any expression
				buf[count++]=element;
			}
			if(cond!=NULL)
			{
				vdmFree(cond);
			}
		}

		EXPECT_EQ(1, buf[0]->value.intVal);
		EXPECT_EQ(2, buf[1]->value.intVal);
		EXPECT_EQ(8, buf[2]->value.intVal);

		comp = newSetWithValues(count, buf);

		for (int i = 0; i < count; i++)
		{
			vdmFree(buf[i]);
		}
		free(buf);
	}

//	COMPREHENSION(exp,var,S,vdmLessThan(var,t));
	EXPECT_EQ(VDM_SET, comp->type);
	UNWRAP_COLLECTION(col, comp);
	EXPECT_EQ(3, col->size);

	//Wrap up.
	vdmFree(S);
	vdmFree(t);
}