void festring::ExtractWord(festring& To) { for(sizetype c = 0; c < Size; ++c) if(Data[c] == ' ') { To.Empty(); To.Append(&Data[c + 1], Size - c - 1); Erase(c, Size - c); SwapData(To); return; } To = *this; Empty(); }
rtn_t *TRemove(rtree_t *tree, rtn_t **parent, rtn_t *root, void *key, is_before fp) { int dir = -9; rtn_t *next = NULL; if(!root) { return(NULL); } if(!(dir = fp(root->data, key))) { if(IsEqual(fp, root->data, key)) { dir = fp((*parent)->data, key); /* if root has only one child*/ if( !(root->next[dir]) || !(root->next[!dir]) ) { if(root->next[dir]) { (*parent)->next[dir] = root->next[dir]; free(root); return(NULL); } (*parent)->next[dir] = root->next[!dir]; free(root); return(NULL); } /* if root has 2 children, swap data with root next, and delete node root next */ next = Next(root, root->next[0], fp); SwapData(&(next->data), &(root->data)); free(next); return(NULL); } } TRemove(tree, &root, root->next[dir], key, fp); /* TBalance(tree, &root);*/ return(NULL); }
void KLTWrapper::InitFeatures(IplImage * imgGray) { /* automatic initialization */ double quality = 0.01; double min_distance = 10; int ni = imgGray->width; int nj = imgGray->height; count = ni / GRID_SIZE_W * nj / GRID_SIZE_H; int cnt = 0; for (int i = 0; i < ni / GRID_SIZE_W - 1; ++i) { for (int j = 0; j < nj / GRID_SIZE_H - 1; ++j) { points[1][cnt].x = i * GRID_SIZE_W + GRID_SIZE_W / 2; points[1][cnt++].y = j * GRID_SIZE_H + GRID_SIZE_H / 2; } } SwapData(imgGray); }
/** * Sort() performs a selection sort on list (it assumes list points to the head * of the list) to sort the elements into ascending order. It makes no * guarantees of the addresses of the list items after sorting, so any ListItem * referenced before a call to Sort() and after may contain different data. Its * second argument is a comparison function that calculates the ordering. This * function takes two const ListItem pointers and return 1 if the first one is * "larger", 0 if they're "equal", and -1 if the second one is "bigger". Since * sorting relies on understanding the data within the ListItem, the comparison * function is left up to the caller to define. This comparison function follows * the same return types as the one used for qsort(). See qsort()'s * documentation for further information. For consistency Sort() returns * SUCCESS if successful. There is no internal error checking and so will never * return anything else. */ int Sort(ListItem *list, int (*CompareItems)(const ListItem *, const ListItem *)) { /* * Declaring variables */ list = GetFirst(list); int listPointerCount = 0; ListItem *listptr; listptr = (ListItem *) (malloc(sizeof (ListItem))); listptr = list->nextItem; /* * Counts the length of the stack and makes counter according * to the length */ while (listptr->nextItem != NULL) { listPointerCount++; /* * We do this several times where we compare based on value at * the location of the pointer list. Depending on the value * returned by compare, we may swap the two pointers. */ if (CompareItems(listptr, list) == TRUE) { SwapData(list, listptr); if (list->previousItem == NULL) { /* * This increments until the counter is back to NULL. * Essentially determines how far to go by counter. */ while (listPointerCount != NULL) { listptr = listptr->nextItem; list = list->nextItem; listPointerCount--; } /* * This continues the search if not a NULL */ } else { listptr = listptr->previousItem; list = list->previousItem; } /* * This checks to see if the pointers are the same. If they are * then we do nothing just increment. */ } else { if (list->previousItem == NULL) { while (listPointerCount != NULL) { listptr = listptr->nextItem; list = list->nextItem; listPointerCount--; } } else { listptr = listptr->previousItem; list = list->previousItem; } } } /* * Repeat and step through the code (identical code) */ while (TRUE) { if (CompareItems(listptr, list) == TRUE) { SwapData(list, listptr); if (list->previousItem == NULL) { listptr = listptr->nextItem; list = list->nextItem; } else { listptr = listptr->previousItem; list = list->previousItem; } } else { if (list->previousItem == NULL) { listptr = listptr->nextItem; list = list->nextItem; } else { listptr = listptr->previousItem; list = list->previousItem; } } if (list->previousItem == NULL) { if (CompareItems(listptr, list) == TRUE) { SwapData(list, listptr); return FALSE; } else { return FALSE; } } } }
void BST::Delete(BasePtr Node, int Value) { BasePtr Parent = NULL, Child; while ( Node != NULL && Node->Data != Value ) if ( Value < Node->Data ) Node = Node->Left; else Node = Node->Right; if ( Node == NULL ) { cerr << "Failed to find " << Value << " for deletion." << endl; return; } // If Node has ANY children, transform it into its // replacement node and delete THAT node. The // height adjustments will always be from wherever // the Parent pointer ends up. if ( ! Node->Left && ! Node-> Right ) // I.e., leaf node { Parent = Node->Parent; if ( Parent == NULL ) // Single-node tree Root = NULL; else if ( Value < Parent->Data ) // Traversed left Parent->Left = NULL; else // else we went right Parent->Right = NULL; Recycle(Node); // only time Node itself goes } else if ( Node->Left == NULL ) // Half-full to the right { Parent = Node; Child = Node->Right; SwapData (Parent, Child); Parent->Left = Child->Left; if (Parent->Left) Parent->Left->Parent = Parent; // CRITICAL to keep the Parent->Right = Child->Right; // parent pointers valid if (Parent->Right) Parent->Right->Parent = Parent; Recycle (Child); } else if ( Node->Right == NULL ) // Half-full to the left { Parent = Node; Child = Node->Left; SwapData (Parent, Child); Parent->Left = Child->Left; if (Parent->Left) Parent->Left->Parent = Parent; Parent->Right = Child->Right; if (Parent->Right) Parent->Right->Parent = Parent; Recycle (Child); } else // Full { Child = Node->Left; while (Child->Right) // In-order predecessor { Parent = Child; Child = Parent->Right; } if ( Parent != NULL ) // DID traverse in left subtree { SwapData (Node, Child); Parent->Right = Child->Left; if (Parent->Right) Parent->Right->Parent = Parent; Recycle (Child); } else // I.e., Node->Left itself { Parent = Node; // for height adjustment SwapData (Node, Child); Parent->Left = Child->Left; if (Parent->Left) Parent->Left->Parent = Parent; Recycle (Child); } } AdjustHt (Parent); }
void UpdateCollisionsAndDestructions(Result* result, int aliveObjectsAmount, float t) { //store the unique indices of the collided objects std::set<int> collidedIndices; //store the unique indices of the destroyed objects std::set<int> destroyedIndices; //check for collisions, death-starts, etc. for (int i = 0; i < aliveObjectsAmount; ++i) { for (int j = 0; j < aliveObjectsAmount; ++j) { if (i != j && std::find(collidedIndices.begin(), collidedIndices.end(), i) == collidedIndices.end() && std::find(collidedIndices.begin(), collidedIndices.end(), j) == collidedIndices.end() && std::find(destroyedIndices.begin(), destroyedIndices.end(), i) == destroyedIndices.end() && std::find(destroyedIndices.begin(), destroyedIndices.end(), j) == destroyedIndices.end()) { BoundingSphere& BSphere1 = result->boundingSpheres[i].boundingSphere; BoundingSphere& BSphere2 = result->boundingSpheres[j].boundingSphere; Type& Type1 = result->types[i].type; Type& Type2 = result->types[j].type; //check if planet or asteroid is in the range of death star if (Type1 == Type::DeathStar && (Type2 == Type::Planet || Type2 == Type::Asteroid) && IsInRange(BSphere1, result->ranges[i].range, BSphere2) || (Type1 == Type::Planet || Type1 == Type::Asteroid) && Type2 == Type::DeathStar && IsInRange(BSphere2, result->ranges[j].range, BSphere1)) { ObjectDestroyed(result, Type::DeathStar, i, j, t, destroyedIndices); } //check if asteroid or death star is in the range of x-wing if (Type1 == Type::XWing && (Type2 == Type::DeathStar || Type2 == Type::Asteroid) && IsInRange(BSphere1, result->ranges[i].range, BSphere2) || (Type1 == Type::DeathStar || Type1 == Type::Asteroid) && Type2 == Type::XWing && IsInRange(BSphere2, result->ranges[j].range, BSphere1)) { //i or j might have been destroyed in the previous check if (std::find(destroyedIndices.begin(), destroyedIndices.end(), i) == destroyedIndices.end() && std::find(destroyedIndices.begin(), destroyedIndices.end(), j) == destroyedIndices.end()) { ObjectDestroyed(result, Type::XWing, i, j, t, destroyedIndices); } } //check if 2 objects collide if (Collide(BSphere1, BSphere2)) { int index = result->collisions_count++; result->collisions[index].body1 = result->boundingSpheres[i].body; result->collisions[index].body2 = result->boundingSpheres[j].body; result->collisions[index].time = t; std::cout << "Collision between " << GetType(result->types[i].type) << " id:" << result->types[i].body << " and " << GetType(result->types[j].type) << " id:" << result->types[j].body << std::endl; collidedIndices.insert(i); collidedIndices.insert(j); } } } } //swap collided objects with alive objects int collidedCounter = 0; for (auto it = collidedIndices.rbegin(); it != collidedIndices.rend(); ++it) { int aliveIndex = aliveObjectsAmount - collidedCounter - 1; int currentIndex = (*it); SwapData(result, currentIndex, aliveIndex); collidedCounter++; } //swap destroyed objects with alive objects int destroyedCounter = 0; for (auto it = destroyedIndices.rbegin(); it != destroyedIndices.rend(); ++it) { int aliveIndex = aliveObjectsAmount - collidedCounter - destroyedCounter - 1; int currentIndex = (*it); SwapData(result, currentIndex, aliveIndex); destroyedCounter++; } }
/** * Function PutDataInPreviousState * Used in undo or redo command. * Put data pointed by List in the previous state, i.e. the state memorised by List * @param aList = a PICKED_ITEMS_LIST pointer to the list of items to undo/redo * @param aRedoCommand = a bool: true for redo, false for undo * @param aRebuildRatsnet = a bool: true to rebuid ratsnet (normal use, and default), false * to just retrieve las state (used in abort commands that do not need to rebuild ratsnest) */ void PCB_EDIT_FRAME::PutDataInPreviousState( PICKED_ITEMS_LIST* aList, bool aRedoCommand, bool aRebuildRatsnet ) { BOARD_ITEM* item; bool not_found = false; bool reBuild_ratsnest = false; // Undo in the reverse order of list creation: (this can allow stacked changes // like the same item can be changes and deleted in the same complex command bool build_item_list = true; // if true the list of esiting items must be rebuilt for( int ii = aList->GetCount()-1; ii >= 0 ; ii-- ) { item = (BOARD_ITEM*) aList->GetPickedItem( ii ); wxASSERT( item ); /* Test for existence of item on board. * It could be deleted, and no more on board: * - if a call to SaveCopyInUndoList was forgotten in Pcbnew * - in zones outlines, when a change in one zone merges this zone with an other * This test avoids a Pcbnew crash * Obviouly, this test is not made for deleted items */ UNDO_REDO_T status = aList->GetPickedItemStatus( ii ); if( status != UR_DELETED ) { if( build_item_list ) // Build list of existing items, for integrity test TestForExistingItem( GetBoard(), NULL ); build_item_list = false; if( !TestForExistingItem( GetBoard(), item ) ) { // Remove this non existant item aList->RemovePicker( ii ); ii++; // the current item was removed, ii points now the next item // decrement it because it will be incremented later not_found = true; continue; } } item->ClearFlags(); // see if we must rebuild ratsnets and pointers lists switch( item->Type() ) { case PCB_MODULE_T: case PCB_ZONE_AREA_T: case PCB_TRACE_T: case PCB_VIA_T: reBuild_ratsnest = true; break; default: break; } switch( aList->GetPickedItemStatus( ii ) ) { case UR_CHANGED: /* Exchange old and new data for each item */ { BOARD_ITEM* image = (BOARD_ITEM*) aList->GetPickedItemLink( ii ); SwapData( item, image ); } break; case UR_NEW: /* new items are deleted */ aList->SetPickedItemStatus( UR_DELETED, ii ); GetBoard()->Remove( item ); break; case UR_DELETED: /* deleted items are put in List, as new items */ aList->SetPickedItemStatus( UR_NEW, ii ); GetBoard()->Add( item ); build_item_list = true; break; case UR_MOVED: item->Move( aRedoCommand ? aList->m_TransformPoint : -aList->m_TransformPoint ); break; case UR_ROTATED: item->Rotate( aList->m_TransformPoint, aRedoCommand ? 900 : -900 ); break; case UR_ROTATED_CLOCKWISE: item->Rotate( aList->m_TransformPoint, aRedoCommand ? -900 : 900 ); break; case UR_FLIPPED: item->Flip( aList->m_TransformPoint ); break; default: { wxString msg; msg.Printf( wxT( "PutDataInPreviousState() error (unknown code %X)" ), aList->GetPickedItemStatus( ii ) ); wxMessageBox( msg ); } break; } } if( not_found ) wxMessageBox( wxT( "Incomplete undo/redo operation: some items not found" ) ); // Rebuild pointers and rastnest that can be changed. if( reBuild_ratsnest && aRebuildRatsnet ) Compile_Ratsnest( NULL, true ); }
ERRORCODE near CTIFFInfo::GetData(TIFF_TAG_TAG nTag, LONG lNumber, TIFF_DATA* pData) { ERRORCODE error; // General use. TIFF_ENTRY_PTR pEntry; SHORT nTypeSize; LPBYTE data_ptr; BOOL fNeedsSwapping; ST_DEV_POSITION offset; /* Zero the data union. A Rational is the largest element, so zero it. */ pData->Rational.numerator = pData->Rational.denominator = 0L; if (!m_Info.valid) { return ERRORCODE_NotInitialized; } if ((pEntry = FindEntry(nTag)) == NULL) { return ERRORCODE_BadParameter; } data_ptr = NULL; /* No data pointed to, yet. */ if (pEntry->entry.type >= MAX_TIFF_TAG_TYPE) { /* Bad type. */ return ERRORCODE_IllegalType; } nTypeSize = nTypeSizes[pEntry->entry.type]; /* See if the entry was read. */ if (pEntry->entry_valid) { /* The entry was read! */ if (lNumber >= pEntry->entry.length) { /* Invalid item number. */ return ERRORCODE_BadParameter; } if (nTypeSize*pEntry->entry.length > sizeof(LONG)) { /* If the default has been set for a Rational, use it. */ if ((pEntry->entry.type == TIFF_RATIONAL) && (pEntry->entry.length == 1) && (pEntry->default_valid)) { memcpy(pData, &pEntry->default_value, nTypeSize); return ERRORCODE_None; } /* The value_offset is a file offset. */ offset = pEntry->entry.value_offset; if (m_Info.byte_order != BYTE_ORDER_IBM) { swap_long(&offset); } if ((error = m_pDevice->seek(offset + (LONG)(lNumber*nTypeSize), ST_DEV_SEEK_SET)) == ERRORCODE_None) { if ((error = m_pDevice->read(pData, nTypeSize)) != ERRORCODE_None) { return error; } /* Swap the data if necessary. */ SwapData(pData, pEntry->entry.type); /* // Special case for Rational values. // If this is a single value on disk, we can set it as the default. // This will prevent the value from being read multiple times. // (i.e. We cache it.) */ if ((pEntry->entry.type == TIFF_RATIONAL) && (pEntry->entry.length == 1)) { /* Set as the default value. */ memcpy(&pEntry->default_value, pData, nTypeSize); pEntry->default_valid = TRUE; } return ERRORCODE_None; } else { /* Multiple items do not have defaults. Just return the error. */ return error; } } else { /* The value_offset is a value. */ /* Copy the 'number'th element out of the field. */ data_ptr = ((LPBYTE)&pEntry->entry.value_offset) + lNumber*nTypeSize; } } /* If we fell through without setting the pointer, use the default. */ if (data_ptr == NULL) { if (pEntry->default_valid) { /* The entry has a default! Send it back. */ data_ptr = (LPBYTE)&pEntry->default_value; fNeedsSwapping = FALSE; } else { /* No default either! */ return ERRORCODE_BadParameter; } } else { fNeedsSwapping = TRUE; } memcpy(pData, data_ptr, nTypeSize); if (fNeedsSwapping) { SwapData(pData, pEntry->entry.type); } return ERRORCODE_None; }