예제 #1
0
파일: pqueue.c 프로젝트: BuloZB/barbershop
// returns 1 on adding item, 0 on successful update of item, -1 on error
int update(int itemId, int score)
{
	int rval = 0;
	int newscore = score;
    ItemTreeNode itnode = findItem(itemId, item_root);
    if(itnode == NULL)
    {
		rval = 1;
        itnode = createItemTreeNode();
		if(itnode == NULL)
			return -1;
        itnode->item = createItemNode(itemId);
		if(itnode->item == NULL)
			return -1;
		itnode->score = 0;
        item_root = addItemTreeNode(item_root, itnode);
    }

	ScoreTreeNode snode;
    if(itnode->score > 0)
    {
	    snode = findScore(itnode->score, score_root);
		newscore = score + itnode->score;
		//printf("moving itemid %d to score %d from score %d\n", itnode->item->itemId, newscore, snode->score);
		int populated = removeItemNode(snode, itnode->item);
		if(!populated)
		{
			score_root = deleteScoreTreeNode(score_root, snode);
		}
		itnode->score = 0;
    }
	snode = findScore(newscore, score_root);
	if(snode == NULL)
	{
		snode = createScoreTreeNode(newscore);
		if(snode == NULL)
			return -1;
		score_root = addScoreTreeNode(score_root, snode);
	}
	addItemNode(snode, itnode->item);
	itnode->score = snode->score;
	app_stats.updates += 1;
	return rval;
}
예제 #2
0
void ListParser::parseListItems(QStringList &lines, Node &listNode)
{
  while (!lines.empty())
  {
    QChar itemNodeTypeChar(lines[0][0]);

    // Embedded bullet list.
    if (lines[0].length() > 1 &&
        lines[0][1] == '*')
    {
      Buffer listBuffer;
      while (!lines.empty() &&
          lines[0].length() > 1 &&
          lines[0][1] == '*')
      {
        listBuffer.append(lines[0].remove(0, 1) + '\n');
        lines.removeFirst();
      }
      Node *insideItemNode = BulletListParser::parse(listBuffer);
      CHECK(insideItemNode);
      if (listNode.count() == 0)
        listNode.append(createItemNode(itemNodeTypeChar));
      listNode.last()->append(insideItemNode);
    }
    // Embedded numbered list.
    else if (lines[0].length() > 1 &&
        lines[0][1] == '#')
    {
      Buffer listBuffer;
      while (!lines.empty() &&
          lines[0].length() > 1 &&
          lines[0][1] == '#')
      {
        listBuffer.append(lines[0].remove(0, 1) + '\n');
        lines.removeFirst();
      }
      Node *insideItemNode = NumberedListParser::parse(listBuffer);
      CHECK(insideItemNode);
      if (listNode.count() == 0)
        listNode.append(createItemNode(itemNodeTypeChar));
      listNode.last()->append(insideItemNode);
    }
    // Embedded definition list.
    else if (lines[0].length() > 1 &&
        (lines[0][1] == ';' || lines[0][1] == ':'))
    {
      Buffer listBuffer;
      while (!lines.empty() &&
          lines[0].length() > 1 &&
          (lines[0][1] == ';' || lines[0][1] == ':'))
      {
        listBuffer.append(lines[0].remove(0, 1) + '\n');
        lines.removeFirst();
      }
      Node *insideItemNode = DefinitionListParser::parse(listBuffer);
      CHECK(insideItemNode);
      if (listNode.count() == 0)
        listNode.append(createItemNode(itemNodeTypeChar));
      listNode.last()->append(insideItemNode);
    }
    // Just a line of text.
    else
    {
      Buffer textBuffer(lines[0].remove(0, 1).trimmed());
      lines.removeFirst();

      // Only add list item if it is not empty. MediaWiki does the same.
      if (textBuffer.text().length() > 0)
      {
        listNode.append(createItemNode(itemNodeTypeChar));
        TextBlockParser::parse(*listNode.last(), textBuffer);
      }
    }
  }
}