int ReaderViewNative::doCommand( int cmd, int param ) { switch (cmd) { case DCMD_OPEN_RECENT_BOOK: { return openRecentBook(); } break; case DCMD_CLOSE_BOOK: { return closeBook(); } break; case DCMD_RESTORE_POSITION: { if ( _docview->isDocumentOpened() ) { _docview->restorePosition(); } } break; default: return 0; } return 1; }
static int testMoveOperations() { Book book; UINT64 hashKey = 4711; BookMove move; openBook(&book, "moduletest"); createEmptyBook(&book); move.move = 7; assert(getBookmoveOffset(&book, hashKey, move.move) == ILLEGAL_OFFSET); move.move = 17; move.nextAlternative = ILLEGAL_OFFSET; appendBookmove(&book, hashKey, &move); assert(getBookmoveOffset(&book, hashKey, move.move) == 0 * sizeof(BookMove)); move.move = 19; appendBookmove(&book, hashKey, &move); assert(getBookmoveOffset(&book, hashKey, move.move) == 1 * sizeof(BookMove)); hashKey += 160939; assert(getBookmoveOffset(&book, hashKey, move.move) == ILLEGAL_OFFSET); appendBookmove(&book, hashKey, &move); assert(getBookmoveOffset(&book, hashKey, move.move) == 2 * sizeof(BookMove)); closeBook(&book); assert(remove("moduletest.bki") == 0); assert(remove("moduletest.bkm") == 0); return 0; }
bool ReaderViewNative::openRecentBook() { CRLog::debug("ReaderViewNative::openRecentBook()"); int index = 0; if ( _docview->isDocumentOpened() ) { CRLog::debug("ReaderViewNative::openRecentBook() : saving previous document state"); _docview->swapToCache(); _docview->getDocument()->updateMap(); _docview->savePosition(); closeBook(); index = 1; } LVPtrVector<CRFileHistRecord> & files = _docview->getHistory()->getRecords(); CRLog::info("ReaderViewNative::openRecentBook() : %d files found in history, startIndex=%d", files.length(), index); if ( index < files.length() ) { CRFileHistRecord * file = files.get( index ); lString16 fn = file->getFilePathName(); CRLog::info("ReaderViewNative::openRecentBook() : checking file %s", LCSTR(fn)); // TODO: check error if ( LVFileExists(fn) ) { return loadDocument( fn ); } else { CRLog::error("file %s doesn't exist", LCSTR(fn)); return false; } //_docview->swapToCache(); } else { CRLog::info("ReaderViewNative::openRecentBook() : no recent book found in history"); } return false; }
static int testPositionOperations() { Book book; UINT64 hashKey = BOOKINDEX_SIZE + 100; BookPosition position; position.hashKey = hashKey; position.nextPosition = ILLEGAL_OFFSET; position.firstMove = 0; openBook(&book, "moduletest"); createEmptyBook(&book); assert(getBookpositionOffset(&book, hashKey) == ILLEGAL_OFFSET); addBookposition(&book, &position); assert(getBookpositionOffset(&book, hashKey) == getBookpositionIndexOffset(hashKey)); position.hashKey += BOOKINDEX_SIZE; addBookposition(&book, &position); assert(getBookpositionOffset(&book, position.hashKey) == BOOKINDEX_SIZE * sizeof(BookPosition)); closeBook(&book); assert(remove("moduletest.bki") == 0); assert(remove("moduletest.bkm") == 0); return 0; }
bool NTriangulation::intelligentSimplify() { bool changed; { // Begin scope for change event block. ChangeEventSpan span(this); // Reduce to a local minimum. changed = simplifyToLocalMinimum(true); // Clone to work with when we might want to roll back changes. NTriangulation* use; // Variables used for selecting random 4-4 moves. std::vector<std::pair<NEdge*, int> > fourFourAvailable; std::pair<NEdge*, int> fourFourChoice; unsigned long fourFourAttempts; unsigned long fourFourCap; NEdge* edge; EdgeIterator eit; int axis; while (true) { // --- Random 4-4 moves --- // Clone the triangulation and start making changes that might or // might not lead to a simplification. // If we've already simplified then there's no need to use a // separate clone since we won't need to undo further changes. use = (changed ? this : new NTriangulation(*this)); // Make random 4-4 moves. fourFourAttempts = fourFourCap = 0; while (true) { // Calculate the list of available 4-4 moves. fourFourAvailable.clear(); // Use edges() to ensure the skeleton has been calculated. for (eit = use->edges().begin(); eit != use->edges().end(); eit++) { edge = *eit; for (axis = 0; axis < 2; axis++) if (use->fourFourMove(edge, axis, true, false)) fourFourAvailable.push_back( std::make_pair(edge, axis)); } // Increment fourFourCap if needed. if (fourFourCap < COEFF_4_4 * fourFourAvailable.size()) fourFourCap = COEFF_4_4 * fourFourAvailable.size(); // Have we tried enough 4-4 moves? if (fourFourAttempts >= fourFourCap) break; // Perform a random 4-4 move on the clone. fourFourChoice = fourFourAvailable[ static_cast<unsigned>(rand()) % fourFourAvailable.size()]; use->fourFourMove(fourFourChoice.first, fourFourChoice.second, false, true); // See if we can simplify now. if (use->simplifyToLocalMinimum(true)) { // We have successfully simplified! // Start all over again. fourFourAttempts = fourFourCap = 0; } else fourFourAttempts++; } // Sync the real triangulation with the clone if appropriate. if (use != this) { // At this point, changed == false. if (use->size() < size()) { // The 4-4 moves were successful; accept them. cloneFrom(*use); changed = true; } delete use; } // At this point we have decided that 4-4 moves will help us // no more. // --- Open book and close book moves --- if (hasBoundaryTriangles()) { // Clone again, always -- we don't want to create gratuitous // boundary triangles if they won't be of any help. use = new NTriangulation(*this); // Perform every book opening move we can find. TriangleIterator fit; bool opened = false; bool openedNow = true; while (openedNow) { openedNow = false; for (fit = use->triangles().begin(); fit != use->triangles().end(); ++fit) if (use->openBook(*fit, true, true)) { opened = openedNow = true; break; } } // If we're lucky, we now have an edge that we can collapse. if (opened) { if (use->simplifyToLocalMinimum(true)) { // Yay! cloneFrom(*use); changed = true; } else { // No good. // Ditch use and don't open anything. opened = false; } } delete use; // If we did any book opening stuff, start all over again. if (opened) continue; // If we've made it this far then there seems to be // nothing left to do. // // Perform book *closing* moves to simplify the boundary // of the triangulation, even if this does not actually // reduce the number of tetrahedra. // // Since we always want to simplify the boundary, make // the changes directly to this triangulation. bool closed = false; EdgeIterator eit; for (eit = edges().begin(); eit != edges().end(); ++eit) if (closeBook(*eit, true, true)) { closed = true; changed = true; // We don't actually care whether we reduce the // number of tetrahedra or not. Ignore the // return value from simplifyToLocalMinimum(). simplifyToLocalMinimum(true); break; } // If we *did* manage to close a book, there might be // further internal simplifications that we can now do. // Back to the top. if (closed) continue; } // Nothing more we can do here. break; } } // End scope for change event span. return changed; }