void test() {
  /* Field testing: NWERC 2012 Problem I, Kattis pieceittogether */
  /* TODO: UVa 11294, UVa 10319 */

  for (int n = 1; n <= 15; n++) {
    for (int iter = 0; iter < 100; iter++) {
      int cnt = randint(1, 100);
      vii clauses(cnt);
      for (int i = 0; i < cnt; i++) {
        int a = randint(1, n) * (randint(1, 2) == 1 ? 1 : -1);
        int b = randint(1, n) * (randint(1, 2) == 1 ? 1 : -1);
        clauses[i] = ii(a, b);
      }
      // cout << n << " " << iter << " " << cnt << endl;

      TwoSat ts(n);
      iter(it,clauses) {
        ts.add_or(it->first, it->second);
      }
      if (ts.sat()) {
        vector<bool> is_true(n+1, false);
        // for (int i = 0; i < size(all_truthy); i++) if (all_truthy[i] > 0) is_true[all_truthy[i]] = true;
        rep(i,0,n) if (V[n+(i+1)].val == 1) is_true[i+1] = true;
        for (int i = 0; i < cnt; i++) {
          bool a = is_true[abs(clauses[i].first)],
             b = is_true[abs(clauses[i].second)];
          assert_true((clauses[i].first > 0 ? a : !a) || (clauses[i].second > 0 ? b : !b), true);
        }
      } else {
        for (int j = 0; j < (1<<n); j++) {
 void NearSpansUnordered::initialize()
 {
     this->slop = query->getSlop();
     this->totalLength = 0;
     this->more = true;
     this->firstTime = true;
     
     Collection<SpanQueryPtr> clauses(query->getClauses());
     queue = newLucene<CellQueue>(clauses.size());
     subSpans = Collection<SpansPtr>::newInstance(clauses.size());
     ordered = Collection<SpansCellPtr>::newInstance();
     
     for (int32_t i = 0; i < clauses.size(); ++i)
     {
         SpansCellPtr cell(newLucene<SpansCell>(shared_from_this(), clauses[i]->getSpans(reader), i));
         ordered.add(cell);
         subSpans[i] = cell->spans;
     }
 }
void test() {
    /* Field testing: NWERC 2012 Problem I, UVa 11294, UVa 10319 */

    for (int n = 1; n <= 20; n++) {
        for (int iter = 0; iter < 1000; iter++) {
            int cnt = randint(1, 4);
            vii clauses(cnt);
            for (int i = 0; i < cnt; i++) {
                int a = randint(1, n) * (randint(1, 2) == 1 ? 1 : -1);
                int b = randint(1, n) * (randint(1, 2) == 1 ? 1 : -1);
                clauses[i] = ii(a, b);
            }

            vi all_truthy;
            if (two_sat(n, clauses, all_truthy)) {
                vector<bool> is_true(n+1, false);
                for (int i = 0; i < size(all_truthy); i++) if (all_truthy[i] > 0) is_true[all_truthy[i]] = true;
                for (int i = 0; i < cnt; i++) {
                    bool a = is_true[abs(clauses[i].first)],
                         b = is_true[abs(clauses[i].second)];
                    assert_true((clauses[i].first > 0 ? a : !a) || (clauses[i].second > 0 ? b : !b), true);
                }
            } else {
                for (int j = 0; j < (1<<n); j++) {

                    bool ok = true;
                    for (int i = 0; i < cnt; i++) {
                        bool a = j & (1 << (abs(clauses[i].first) - 1)),
                             b = j & (1 << (abs(clauses[i].second) - 1));
                        ok = ok && ((clauses[i].first > 0 ? a : !a) || (clauses[i].second > 0 ? b : !b));
                    }

                    assert_false(ok, true);
                }
            }
        }
    }
}
Exemplo n.º 4
0
HWND GetCandPosHintFromComp(UIEXTRA *lpUIExtra, InputContext *lpIMC,
                            DWORD iClause, LPPOINT ppt)
{
  FOOTMARK();
  HWND hCompWnd;

  // is it vertical?
  BOOL fVert = (lpIMC->lfFont.A.lfEscapement == 2700);

  // get comp str
  CompStr *lpCompStr = lpIMC->LockCompStr();
  if (lpCompStr == NULL) return NULL;
  // get comp str
  std::wstring str(lpCompStr->GetCompStr(), lpCompStr->dwCompStrLen);
  const WCHAR *psz = str.c_str();
  const WCHAR *pch = psz;

  // get clause info
  DWORD *pdw = lpCompStr->GetCompClause();
  DWORD *pdwEnd = pdw + lpCompStr->dwCompClauseLen / sizeof(DWORD);
  std::set<DWORD> clauses(pdw, pdwEnd);

  DWORD ich = 0;
  DWORD dwClauseIndex = 0;
  for (int i = 0; i < MAXCOMPWND; i++) {
    if (lpIMC->cfCompForm.dwStyle) {
      hCompWnd = lpUIExtra->uiComp[i].hWnd;
    } else {
      hCompWnd = lpUIExtra->hwndDefComp;
    }

    // get client rect
    RECT rc;
    ::GetClientRect(hCompWnd, &rc);

    // starting position
    int x, y;
    if (fVert) {
      x = rc.right - UNDERLINE_HEIGHT;
      y = 0;
    } else {
      x = y = 0;
    }

    DWORD cch = ::GetWindowLong(hCompWnd, FIGWL_COMPSTARTNUM);
    DebugPrintA("ich: %d, cch: %d, dwClauseIndex: %d\n", ich, cch, dwClauseIndex);

    HDC hDC = ::GetDC(hCompWnd);
    HFONT hFont = (HFONT)::GetWindowLongPtr(hCompWnd, FIGWLP_FONT);
    HFONT hOldFont = NULL;
    if (hFont) hOldFont = (HFONT)::SelectObject(hDC, hFont);

    // is it end?
    SIZE siz;
    BOOL bIsDone = FALSE;
    const WCHAR *lpEnd = &pch[cch];
    while (pch < lpEnd) {
      // get size of text
      ::GetTextExtentPoint32W(hDC, pch, 1, &siz);

      if (dwClauseIndex == iClause) {
        if (fVert) {
          ppt->x = x;
          ppt->y = y + siz.cx;
        } else {
          ppt->x = x;
          ppt->y = y + siz.cy;
        }
        ::ClientToScreen(hCompWnd, ppt);
        bIsDone = TRUE;
        break;
      }

      // go to next position
      ++pch;
      ++ich;
      if (fVert)
        y += siz.cx;
      else
        x += siz.cx;

      if (clauses.count(ich) > 0) {
        ++dwClauseIndex;
      }
    }

    ::SelectObject(hDC, hOldFont);
    ::ReleaseDC(hCompWnd, hDC);

    if (bIsDone || lpIMC->cfCompForm.dwStyle == 0) {
      break;
    }
  }

  lpIMC->UnlockCompStr();
  return hCompWnd;
} // GetCandPosHintFromComp
Exemplo n.º 5
0
void DrawTextOneLine(HWND hCompWnd, HDC hDC, const WCHAR *pch,
                     DWORD ich, DWORD cch, CompStr *lpCompStr, BOOL fVert) {
  FOOTMARK();

  if (cch == 0) return;

  // attribute
  BYTE *lpattr = lpCompStr->GetCompAttr();

  // get clause info
  DWORD *pdw = lpCompStr->GetCompClause();
  DWORD *pdwEnd = pdw + lpCompStr->dwCompClauseLen / sizeof(DWORD);
  std::set<DWORD> clauses(pdw, pdwEnd);

  // get client rect and fill white
  RECT rc;
  ::GetClientRect(hCompWnd, &rc);
  ::FillRect(hDC, &rc, (HBRUSH)GetStockObject(WHITE_BRUSH));

  // starting position
  int x, y;
  if (fVert) {
    x = rc.right - UNDERLINE_HEIGHT;
    y = 0;
  } else {
    x = y = 0;
  }

  // set opaque mode
  ::SetBkMode(hDC, OPAQUE);

  // is it end?
  SIZE siz;
  const WCHAR *lpEnd = &pch[cch];
  while (pch < lpEnd) {

    // set color and pen
    HPEN hPen;
    switch (lpattr[ich]) {
    case ATTR_TARGET_CONVERTED:
      ::SetTextColor(hDC, RGB(255, 255, 255));
      ::SetBkColor(hDC, RGB(0, 51, 0));
      hPen = ::CreatePen(PS_SOLID, 1, RGB(0, 51, 0));
      break;
    case ATTR_CONVERTED:
      ::SetTextColor(hDC, RGB(0, 51, 0));
      ::SetBkColor(hDC, RGB(255, 255, 255));
      hPen = ::CreatePen(PS_SOLID, 1, RGB(0, 51, 0));
      break;
    default:
      ::SetTextColor(hDC, RGB(0, 51, 0));
      ::SetBkColor(hDC, RGB(255, 255, 255));
      hPen = ::CreatePen(PS_DOT, 1, RGB(0, 121, 0));
      break;
    }
    // draw text
    ::TextOutW(hDC, x, y, pch, 1);

    // get size of text
    ::GetTextExtentPoint32W(hDC, pch, 1, &siz);

    // draw underline if target converted
    INT nClauseSep = 2 * (clauses.count(ich + 1) > 0);
    HGDIOBJ hPenOld = ::SelectObject(hDC, hPen);
    if (lpattr[ich] == ATTR_TARGET_CONVERTED) {
      if (fVert) {
        ::MoveToEx(hDC, x + 1, y, NULL);
        ::LineTo(hDC, x + 1, y + siz.cx - nClauseSep);
        ::MoveToEx(hDC, x + 2, y, NULL);
        ::LineTo(hDC, x + 2, y + siz.cx - nClauseSep);
      } else {
        ::MoveToEx(hDC, x, y + siz.cy - 1, NULL);
        ::LineTo(hDC, x + siz.cx - nClauseSep, y + siz.cy - 1);
        ::MoveToEx(hDC, x, y + siz.cy, NULL);
        ::LineTo(hDC, x + siz.cx - nClauseSep, y + siz.cy);
      }
    } else {
      if (fVert) {
        ::MoveToEx(hDC, x + 1, y, NULL);
        ::LineTo(hDC, x + 1, y + siz.cx - nClauseSep);
      } else {
        ::MoveToEx(hDC, x, y + siz.cy - 1, NULL);
        ::LineTo(hDC, x + siz.cx - nClauseSep, y + siz.cy - 1);
      }
    }
    ::DeleteObject(::SelectObject(hDC, hPenOld));

    // draw cursor (caret)
    if (lpCompStr->dwCursorPos == ich) {
      ::SelectObject(hDC, ::GetStockObject(BLACK_PEN));
      if (fVert) {
        ::MoveToEx(hDC, x, y, NULL);
        ::LineTo(hDC, x + siz.cy, y);
        ::MoveToEx(hDC, x, y + 1, NULL);
        ::LineTo(hDC, x + siz.cy, y + 1);
      } else {
        ::MoveToEx(hDC, x, y, NULL);
        ::LineTo(hDC, x, y + siz.cy);
        ::MoveToEx(hDC, x + 1, y, NULL);
        ::LineTo(hDC, x + 1, y + siz.cy);
      }
    }

    // go to next position
    ++pch;
    ++ich;
    if (fVert)
      y += siz.cx;
    else
      x += siz.cx;
  }

  // draw caret at last if any
  if (lpCompStr->dwCursorPos == ich) {
    ::SelectObject(hDC, ::GetStockObject(BLACK_PEN));
    if (fVert) {
      ::MoveToEx(hDC, x, y, NULL);
      ::LineTo(hDC, x + siz.cy, y);
      ::MoveToEx(hDC, x, y + 1, NULL);
      ::LineTo(hDC, x + siz.cy, y + 1);
    } else {
      ::MoveToEx(hDC, x, y, NULL);
      ::LineTo(hDC, x, y + siz.cy);
      ::MoveToEx(hDC, x + 1, y, NULL);
      ::LineTo(hDC, x + 1, y + siz.cy);
    }
  }
}
Exemplo n.º 6
0
// calc the position of composition windows and move them
void CompWnd_Move(UIEXTRA *lpUIExtra, InputContext *lpIMC) {
  FOOTMARK();

  lpUIExtra->dwCompStyle = lpIMC->cfCompForm.dwStyle;

  HFONT hFont = NULL;
  HFONT hOldFont = NULL;
  if (lpIMC->cfCompForm.dwStyle) {  // style is not CFS_DEFAULT
    // lock the COMPOSITIONSTRING structure
    if (!lpIMC->HasCompStr()) return;
    CompStr *lpCompStr = lpIMC->LockCompStr();
    if (lpCompStr == NULL) return;

    // set the rectangle for the composition string
    RECT rcSrc;
    if (lpIMC->cfCompForm.dwStyle & CFS_RECT)
      rcSrc = lpIMC->cfCompForm.rcArea;
    else
      ::GetClientRect(lpIMC->hWnd, &rcSrc);

    POINT ptSrc = lpIMC->cfCompForm.ptCurrentPos;
    ::ClientToScreen(lpIMC->hWnd, &ptSrc);
    ::ClientToScreen(lpIMC->hWnd, (LPPOINT)&rcSrc.left);
    ::ClientToScreen(lpIMC->hWnd, (LPPOINT)&rcSrc.right);

    // check the start position
    if (!::PtInRect(&rcSrc, ptSrc)) {
      lpIMC->UnlockCompStr();
      return;
    }

    // hide the default composition window
    HWND hwndDef = lpUIExtra->hwndDefComp;
    if (::IsWindow(hwndDef)) {
      ::ShowWindow(hwndDef, SW_HIDE);
    }

    std::wstring str(lpCompStr->GetCompStr(), lpCompStr->dwCompStrLen);
    const WCHAR *psz = str.c_str();
    const WCHAR *pch = psz;
    DWORD ich = 0, iClause = 0;

    // clause info
    DWORD *pdw = lpCompStr->GetCompClause();
    DWORD *pdwEnd = pdw + lpCompStr->dwCompClauseLen / sizeof(DWORD);
    std::set<DWORD> clauses(pdw, pdwEnd);

    if (!lpUIExtra->bVertical) {  // not vertical font
      int dx = rcSrc.right - ptSrc.x;
      int curx = ptSrc.x, cury = ptSrc.y;

      // set the composition string to each composition window.
      // the composition windows that are given the compostion string
      // will be moved and shown.
      for (int i = 0; i < MAXCOMPWND; i++) {
        HWND hwnd = lpUIExtra->uiComp[i].hWnd;
        if (::IsWindow(hwnd)) {
          HDC hDC = ::GetDC(hwnd);
          hFont = (HFONT)::GetWindowLongPtr(hwnd, FIGWLP_FONT);
          if (hFont) hOldFont = (HFONT)::SelectObject(hDC, hFont);

          SIZE siz;
          siz.cy = 0;

          int num = NumCharInDX(hDC, pch, dx);
          if (num) {
            ::GetTextExtentPoint32W(hDC, pch, num, &siz);

            lpUIExtra->uiComp[i].rc.left = curx;
            lpUIExtra->uiComp[i].rc.top = cury;
            siz.cx += CARET_WIDTH;
            siz.cy += UNDERLINE_HEIGHT;
            lpUIExtra->uiComp[i].rc.right = siz.cx;
            lpUIExtra->uiComp[i].rc.bottom = siz.cy;
            ::SetWindowLong(hwnd, FIGWL_COMPSTARTSTR, LONG(pch - psz));
            ::SetWindowLong(hwnd, FIGWL_COMPSTARTNUM, num);
            ::MoveWindow(hwnd, curx, cury, siz.cx, siz.cy, TRUE);
            ::ShowWindow(hwnd, SW_SHOWNOACTIVATE);

            pch += num;
            ich += num;

            if (clauses.count(ich) > 0) {
              ++iClause;
            }
          } else {
            ::SetRectEmpty(&lpUIExtra->uiComp[i].rc);
            ::SetWindowLong(hwnd, FIGWL_COMPSTARTSTR, 0);
            ::SetWindowLong(hwnd, FIGWL_COMPSTARTNUM, 0);
            ::ShowWindow(hwnd, SW_HIDE);
          }
          ::InvalidateRect(hwnd, NULL, FALSE);

          dx = rcSrc.right - rcSrc.left;
          curx = rcSrc.left;
          cury += siz.cy + UNDERLINE_HEIGHT;

          if (hOldFont) ::SelectObject(hDC, hOldFont);
          ::ReleaseDC(hwnd, hDC);
        }
      }
    } else {  // vertical font
      int dy = rcSrc.bottom - ptSrc.y;
      int curx = ptSrc.x, cury = ptSrc.y;

      for (int i = 0; i < MAXCOMPWND; i++) {
        HWND hwnd = lpUIExtra->uiComp[i].hWnd;
        if (::IsWindow(hwnd)) {
          HDC hDC = ::GetDC(hwnd);
          hFont = (HFONT)::GetWindowLongPtr(hwnd, FIGWLP_FONT);
          if (hFont) hOldFont = (HFONT)::SelectObject(hDC, hFont);

          SIZE siz;
          siz.cy = 0;
          int num = NumCharInDY(hDC, pch, dy);
          if (num) {
            ::GetTextExtentPoint32W(hDC, pch, num, &siz);

            lpUIExtra->uiComp[i].rc.left = curx - siz.cy;
            lpUIExtra->uiComp[i].rc.top = cury;
            siz.cy += UNDERLINE_HEIGHT;
            siz.cx += CARET_WIDTH;
            lpUIExtra->uiComp[i].rc.right = siz.cy;
            lpUIExtra->uiComp[i].rc.bottom = siz.cx;
            ::SetWindowLong(hwnd, FIGWL_COMPSTARTSTR, LONG(pch - psz));
            ::SetWindowLong(hwnd, FIGWL_COMPSTARTNUM, num);
            ::MoveWindow(hwnd, curx, cury, siz.cy, siz.cx, TRUE);
            ::ShowWindow(hwnd, SW_SHOWNOACTIVATE);

            pch += num;
            ich += num;

            if (clauses.count(ich) > 0) {
              ++iClause;
            }
          } else {
            ::SetRectEmpty(&lpUIExtra->uiComp[i].rc);
            ::SetWindowLong(hwnd, FIGWL_COMPSTARTSTR, 0);
            ::SetWindowLong(hwnd, FIGWL_COMPSTARTNUM, 0);
            ::ShowWindow(hwnd, SW_HIDE);
          }
          ::InvalidateRect(hwnd, NULL, FALSE);

          dy = rcSrc.bottom - rcSrc.top;
          cury = rcSrc.top;
          curx -= siz.cy + UNDERLINE_HEIGHT;

          if (hOldFont) ::SelectObject(hDC, hOldFont);
          ::ReleaseDC(hwnd, hDC);
        }
      }
    }
    lpIMC->UnlockCompStr();
  } else {  // style is CFS_DEFAULT
    HWND hwndDef = lpUIExtra->hwndDefComp;
    if (::IsWindow(hwndDef)) {
      ::SetWindowLong(hwndDef, FIGWL_COMPSTARTSTR, 0);
      ::SetWindowLong(hwndDef, FIGWL_COMPSTARTNUM, 0);

      // hide all non-default comp windows
      for (int i = 0; i < MAXCOMPWND; i++) {
        HWND hwnd = lpUIExtra->uiComp[i].hWnd;
        if (::IsWindow(hwnd)) {
          ::ShowWindow(hwnd, SW_HIDE);
        }
      }

      // get width and height of composition string
      int width = 0, height = 0;
      HDC hDC = ::GetDC(hwndDef);
      CompStr *lpCompStr = lpIMC->LockCompStr();
      if (lpCompStr) {
        if (lpCompStr->dwCompStrLen > 0) {
          std::wstring str(lpCompStr->GetCompStr(), lpCompStr->dwCompStrLen);
          const WCHAR *psz = str.c_str();
          SIZE siz;
          ::GetTextExtentPoint32W(hDC, psz, lstrlenW(psz), &siz);
          width = siz.cx;
          height = siz.cy;
        }
        lpIMC->UnlockCompStr();
      }
      ::ReleaseDC(hwndDef, hDC);

      // calculate new window extent
      RECT rc;
      ::GetWindowRect(hwndDef, &rc);
      POINT pt;
      pt.x = rc.left;
      pt.y = rc.top;
      TheIME.SetUserData(L"ptDefComp", &pt, sizeof(pt));
      width += 2 * ::GetSystemMetrics(SM_CXEDGE) + CARET_WIDTH;
      height += 2 * ::GetSystemMetrics(SM_CYEDGE) + UNDERLINE_HEIGHT;

      // move and show window
      ::MoveWindow(hwndDef, rc.left, rc.top, width, height, TRUE);
      ::ShowWindow(hwndDef, SW_SHOWNOACTIVATE);

      // redraw window
      ::InvalidateRect(hwndDef, NULL, FALSE);
    }
  }
} // CompWnd_Move
Exemplo n.º 7
0
 //! output of clause-set to out
 void operator() () {
   comment();
   clauses();
 }