bool ScStruct::append(ScAddr const & elAddr) { check_expr(mContext); if (!hasElement(elAddr)) return mContext->createEdge(sc_type_arc_pos_const_perm, mAddr, elAddr).isValid(); return false; }
/* Calls hasElement>findElement. adds element to end of array. * O(n) */ bool addElement(SET *sp, char *elt) { assert(sp->count < Max_Unique); if (hasElement(sp, elt) == false){ sp->elts[sp->count] = strdup(elt); assert(sp != NULL); sp->count++; return true; } return false; }
/*function description:add elt to the set pointed to by sp, and return whether the set changed *This function has a big O of O(n) */ bool addElement(SET *sp, char *elt) { assert((sp != NULL) && (elt != NULL)); if (hasElement(sp, elt)==false) { sp->elts[sp->count]=strdup(elt); assert(sp->elts[sp->count]!=NULL); //adds element to end of array (sp->count)++; return true; } return false; }
bool PlyReader::requestProperty(const std::string& elementName, const std::string& propertyName, int dataType, int dataOffset, int countType, int countOffset) { SURGSIM_ASSERT(isValid()) << "'" << m_filename << "' is an invalid .ply file"; SURGSIM_ASSERT(hasElement(elementName)) << "The element <" << elementName << "> has not been requested yet, you cannot access properties for it"; SURGSIM_ASSERT(hasProperty(elementName, propertyName)) << "The requested property <" << propertyName << "> cannot be found in element <" << elementName << ">."; bool result = false; bool scalar = isScalar(elementName, propertyName); bool wantScalar = (countType == 0); if (wantScalar && !scalar) { SURGSIM_FAILURE() << "Trying to access a list property as a scalar." << "for element <" << elementName << "> and property <" << propertyName << ">."; } else if (!wantScalar && scalar) { SURGSIM_FAILURE() << "Trying to access a scalar property as a list." << "for element <" << elementName << "> and property <" << propertyName << ">."; } if (hasProperty(elementName, propertyName) && (scalar == wantScalar)) { auto itBegin = std::begin(m_requestedElements[elementName].requestedProperties); auto itEnd = std::end(m_requestedElements[elementName].requestedProperties); bool doAdd = std::find_if(itBegin, itEnd, [propertyName](PropertyInfo p){return p.propertyName == propertyName;}) == itEnd; if (doAdd) { PropertyInfo info; info.propertyName = propertyName; info.dataType = m_data->types[dataType]; info.dataOffset = dataOffset; info.countType = m_data->types[countType]; info.countOffset = countOffset; m_requestedElements[elementName].requestedProperties.push_back(info); result = true; } } return result; }
/*function description:remove elt from the set pointed to by sp, and then return whether the set changed *This function has a big O of O(n) */ bool removeElement(SET *sp, char *elt) { int location; assert((sp != NULL) && (elt != NULL)); if (hasElement(sp, elt)==true) { location = findElement(sp, elt); free(sp->elts[location]); sp->elts[location] = sp->elts[sp->count-1]; //points the pointer from the gap to the last element in the array sp->count--; return true; } return false; }
int main() { binTree* root = NULL; int i = 0; while(i == 0) { char c; int cur; scanf("%c", &c); switch (c) { case 'a': scanf("%d", &cur); root = addElement(root, cur); break; case 'r': scanf("%d", &cur); root = removeElement(root, cur); break; case 'i': scanf("%d", &cur); if(hasElement(root, cur) == 0) { printf("Element doesn't belong to tree\n"); } else { printf("Element belongs to tree\n"); } break; case 'p': printParenthesis(root); printf("\n"); break; case 'u': printUp(root); printf("\n"); break; case 'd': printDown(root); printf("\n"); break; case 'c': clearTree(root); i = 1; break; } } return 0; }
bool addElement(SET * sp, char *elt){ assert(sp!=NULL & elt!=NULL); unsigned pos; bool found; if(hasElement(sp,elt)==true) return false; if(sp->count==sp->length) return false; pos = findElement(sp, elt, &found); elt=strdup(elt); sp->data[pos]=elt; sp->flags[pos]=FILLED; sp->count++; return true; }
// remove Element in set bool removeElement (SET *sp, char *elt) { // if element is not present then return false if (hasElement(sp, elt) == false) return false; bool found; // value to allow findElement() to be called // get index value int index = findElement(sp, elt, &found); // shift elements into position of element in question for (int j = index; j < (sp-> count-1); j++) { sp -> words[j] = sp->words[j + 1]; } // set old last value to null to accomodate shift sp->words[sp->count-1] = NULL; sp->count--; // decrement counter return true; }
bool ScStruct::append(ScAddr const & elAddr, ScAddr const & attrAddr) { check_expr(mContext); if (!hasElement(elAddr)) { ScAddr const edge = mContext->createEdge(sc_type_arc_pos_const_perm, mAddr, elAddr); if (edge.isValid()) { ScAddr const edge2 = mContext->createEdge(sc_type_arc_pos_const_perm, attrAddr, edge); if (edge2.isValid()) return true; // cleanup mContext->eraseElement(edge); } } return false; }
int main(int argc, char *argv[]) { FILE *fp; char buffer[BUFSIZ]; SET *odd; int words; /* Check usage and open the file. */ if (argc != 2) { fprintf(stderr, "usage: %s file1\n", argv[0]); exit(EXIT_FAILURE); } if ((fp = fopen(argv[1], "r")) == NULL) { fprintf(stderr, "%s: cannot open %s\n", argv[0], argv[1]); exit(EXIT_FAILURE); } /* Insert or delete words to compute their parity. */ words = 0; odd = createSet(MAX_SIZE); while (fscanf(fp, "%s", buffer) == 1) { words ++; if (hasElement(odd, buffer)) removeElement(odd, buffer); else addElement(odd, buffer); } printf("%d total words\n", words); printf("%d words occur an odd number of times\n", numElements(odd)); fclose(fp); destroySet(odd); exit(EXIT_SUCCESS); }
bool PlyReader::requestElement(const std::string& elementName, std::function<void* (const std::string&, size_t)> startElementCallback, std::function<void (const std::string&)> processElementCallback, std::function<void (const std::string&)> endElementCallback) { SURGSIM_ASSERT(isValid()) << "'" << m_filename << "' is an invalid .ply file"; bool result = false; if (hasElement(elementName) && m_requestedElements.find(elementName) == m_requestedElements.end()) { ElementInfo info; info.name = elementName; info.startElementCallback = startElementCallback; info.endElementCallback = endElementCallback; info.processElementCallback = processElementCallback; m_requestedElements[elementName] = info; result = true; } return result; }
int BodyElementGroup::removeElement(int be_id) { // Element does not exist if ( !hasElement(be_id) ) return nofElements; // Remove one element int* tmp_ids = NULL; int* tmp_tags = NULL; // Allocate new arries // NOTE: Do not delete these!!! if ( nofElements > 1 ) { tmp_ids = new int[nofElements-1]; tmp_tags = new int[nofElements-1]; // Copy old data, but skip be_id for (int i = 0; i < nofElements; i++) { if ( be_id == elementIds[i] ) continue; tmp_ids[i] = elementIds[i]; tmp_tags[i] = elementTags[i]; } } // Delete old data delete[] elementIds; delete[] elementTags; // Update object data nofElements--; elementIds = tmp_ids; elementTags = tmp_tags; return nofElements; }
int BodyElementGroup::addElement(int be_id) { // Element exists if ( hasElement(be_id) ) return nofElements; BodyElement* be = model->getBodyElementById(be_id); // Add element // Allocate new arries // NOTE: Do not delete these!!! int* tmp_ids = new int[nofElements+1]; int* tmp_tags = new int[nofElements+1]; // Copy old data for (int i = 0; i < nofElements; i++) { tmp_ids[i] = elementIds[i]; tmp_tags[i] = elementTags[i]; } // Delete old data delete[] elementIds; delete[] elementTags; // Add new info tmp_ids[nofElements] = be_id; tmp_tags[nofElements] = be->Tag(); // Update object data nofElements++; elementIds = tmp_ids; elementTags = tmp_tags; return nofElements; }
bool XMLParser::accessElement(unsigned elem_type) { bool has_elem; xmlNode *elems[4]; if(!root_elem) throw Exception(ERR_OPR_NOT_ALOC_ELEM_TREE,__PRETTY_FUNCTION__,__FILE__,__LINE__); elems[ROOT_ELEMENT]=curr_elem->parent; elems[CHILD_ELEMENT]=curr_elem->children; elems[NEXT_ELEMENT]=curr_elem->next; elems[PREVIOUS_ELEMENT]=curr_elem->prev; /* Checks whether the current element has the element that is to be accessed. The flag 'has_elem' is also used on the method return to indicate if the element has been accessed or not. */ has_elem=hasElement(elem_type); if(has_elem) curr_elem=elems[elem_type]; return(has_elem); }
int main (int argc, char *argv []) { SET *set; FILE *fp; char buffer [BUFSIZ]; int words; /* Check usage and open the first file. */ if (argc == 1 || argc > 3) { fprintf (stderr, "usage: %s file1 [file2]\n", argv [0]); exit (EXIT_FAILURE); } if ((fp = fopen (argv [1], "r")) == NULL) { fprintf (stderr, "%s: cannot open %s\n", argv [0], argv [1]); exit (EXIT_FAILURE); } /* Insert all words into the set. */ words = 0; if ((set = createSet (MAX_SIZE)) == NULL) { fprintf (stderr, "%s: failed to create set\n", argv [0]); exit (EXIT_FAILURE); } while (fscanf (fp, "%s", buffer) == 1) { words ++; if (!hasElement (set, buffer)) if (!insertElement (set, strdup (buffer))) fprintf (stderr, "set full\n"); } printf ("%d total words\n", words); printf ("%d unique words\n", numElements (set)); fclose (fp); /* Try to open the second file. */ if (argc == 3) { if ((fp = fopen (argv [2], "r")) == NULL) { fprintf (stderr, "%s: cannot open %s\n", argv [0], argv [1]); exit (EXIT_FAILURE); } /* Delete all words in the second file. */ while (fscanf (fp, "%s", buffer) == 1) deleteElement (set, buffer); printf ("%d remaining words\n", numElements (set)); } destroySet (set); exit (EXIT_SUCCESS); }