コード例 #1
0
    bool tryFill(vector<vector<char> > &board, vector<vector<int>> killMaskBoard, int emptyCount) {
        if (emptyCount == 0) return true;

        auto xy = findLeastChoicePos(killMaskBoard);
        if (xy.first == -1) return false;
        int x = xy.first, y = xy.second;

        int availables[9];
        int count = getAvailables(availables, killMaskBoard[y][x]);
        for (int i = 0; i < count; ++i) {
            char c = '1' + availables[i];

            assert(board[y][x] == '.');
            board[y][x] = c;

            vector<vector<int>> newKillMaskBoard(killMaskBoard);
            fillKillMaskBoard(newKillMaskBoard, x, y, c);
            newKillMaskBoard[y][x] = 0x1ff;

            if (tryFill(board, newKillMaskBoard, emptyCount - 1)) return true;

            board[y][x] = '.';
        }

        return false;
    }
コード例 #2
0
   void HistoricalReplay::processOrders(InstrumentCB & icb, const Tick & tick, bool executeOnLimitOrStop)
   {
      // Scan all orders and check for a fill against the current tick
      for (auto it = std::begin(icb.orders); it != std::end(icb.orders); ++it)
      {
         numeric fillPrice;
         long filledQuantity;
         long transactionQuantity;
         long newPosition;
         long previousPosition = icb.instrumentPosition.position;
         bool filled = it->tryFill(tick, previousPosition, executeOnLimitOrStop, fillPrice, filledQuantity, transactionQuantity, newPosition);
         if (filled)
         {
            if (filled)
            {
               // Update the position
               icb.instrumentPosition = { newPosition, tick.timestamp };

               // Some previous exit orders may need to be cancelled. For instance, if we
               // just exited a long position, any previous exit orders are cancelled.
               bool removeExits;
               if ((previousPosition > 0 && newPosition <= 0) || (previousPosition < 0 && newPosition >= 0))
               {
                  removeExits = true;
               }
               else
               {
                  removeExits = false;
               }

               // cancel the orders
               if (removeExits)
               {
                  for (auto oo = std::begin(icb.orders); oo != it; ++oo)
                  {
                     // Cancel active, exit orders
                     if (oo->isExit() && oo->isActive()) oo->cancel();
                  }
               }

               // Mark the current order as filled
               it->fill();
               // Add a transaction to the portfolio
               Poco::Logger::root().debug("appending transaction: " + icb.instrument->symbol() + 
                                          ": " + Poco::DateTimeFormatter::format(tick.timestamp, "%Y-%m-%d") + 
                                          ": " + Poco::NumberFormatter::format(transactionQuantity) + 
                                          ", " + Poco::NumberFormatter::format(filledQuantity));
               portfolio_.appendTransaction(*icb.instrument, tick.timestamp, transactionQuantity, fillPrice, 0.0);
               // Add an execution
               icb.executions.emplace_back(tick.timestamp, fillPrice, filledQuantity);
               // Add a notification (posted after the order processing loop finishes)
               icb.orderNotifications.emplace_back(&*it, &icb.executions.back());
            }
         }
      }
   }
コード例 #3
0
    void solveSudoku(vector<vector<char> > &board) {
        int emptyCount = 0;

        vector<vector<int>> killMaskBoard(9, vector<int>(9, 0));
        for (int y = 0; y < 9; ++y) {
            for (int x = 0; x < 9; ++x) {
                char c = board[y][x];
                if (c != '.') {
                    killMaskBoard[y][x] = 0x1ff;
                    fillKillMaskBoard(killMaskBoard, x, y, c);
                } else {
                    ++emptyCount;
                }
            }
        }

        tryFill(board, killMaskBoard, emptyCount);
    }