示例#1
0
void findNSmallest2(T first, T last, size_t n, V oit) {
  std::auto_ptr<ListNode> head;
  std::vector<double> buffer(n, std::numeric_limits<double>::max());
  for (; first != last; ++first) {
    double tmp = *first;
    if (head.get() == NULL) {
      head.reset(new ListNode(tmp));
    }
    else {
      // Insertion into correct place
      ListNode* currNode = head.get();
      ListNode* prevNode = NULL;
      for (;;) {
        if (tmp < currNode->value()) {
          // Insert
          if (prevNode == NULL) {
            // Inserts at start
            head.release();
            head.reset(new ListNode(tmp, currNode));
          }
          else {
            // Insert part way through
            ListNode* newnode = new ListNode(tmp, currNode);
            prevNode->join(newnode);
          }
          break;
        }
        if (currNode->next() == NULL) {
          // Reached end of list
          ListNode* newnode = new ListNode(tmp);
          currNode->join(newnode);
          break;
        } else {
          prevNode = currNode;
          currNode = currNode->next();
        }
      }
    }
    // Trim list to size
    ListNode* currNode = head.get();
    for (size_t i = 0; currNode != NULL && i < n; ++i) {
      if (i == n - 1) {
        ListNode* tmp = currNode->next();
        currNode->join(NULL);
        delete tmp;
      }
      currNode = currNode->next();
    }
  }
  for (ListNode* currNode = head.get(); currNode != NULL;
      currNode = currNode->next()) {
    *oit = currNode->value();
  }
}
示例#2
0
void PointerFIFO::put(void* val)
{
	ListNode *node = allocate();
	node->data(val);
	node->next(NULL);
	if (mTail!=NULL) mTail->next(node);
	mTail=node;
	if (mHead==NULL) mHead=node;
	mSize++;
}
示例#3
0
void PointerFIFO::push_front(void* val)	// by pat
{
	// Pat added this routine for completeness, but never used or tested.
	// The first person to use this routine should remove this assert.
	ListNode *node = allocate();
	node->data(val);
	node->next(mHead);
	mHead = node;
	if (!mTail) mTail=node;
	mSize++;
}
示例#4
0
  /* Return the +index+ numbered element from the beginning. */
  Object* List::locate(STATE, size_t index) {
    ListNode* cur = first_;

    while(index > 0) {
      if(cur->nil_p()) return Qnil;

      cur = cur->next();
      index--;
    }

    if(cur->nil_p()) return Qnil;
    return cur->object();
  }
示例#5
0
  /* Search the List for +obj+ and remove all instances of it.
   *
   * Returns the number of elements removed. */
  size_t List::remove(STATE, const Object* obj) {
    if(empty_p()) return 0;

    size_t deleted = 0, counted = 0;

    ListNode* node = first_;
    ListNode* lst =  nil<ListNode>();
    ListNode* nxt =  nil<ListNode>();

    while(!node->nil_p()) {
      nxt = node->next();

      if(node->object() == obj) {
        deleted++;
        if(lst->nil_p()) {
          first(state, nxt);
        } else {
          lst->next(state, nxt);
        }

        if(last_ == node) {
          last(state, lst);
        }

        lst = nil<ListNode>();
      } else {
        counted++;
        lst = node;
      }

      node = nxt;
    }

    count(state, Integer::from(state, counted));

    return deleted;
  }