ChangeSorter.cpp #1

  • //
  • guest/
  • sam_stafford/
  • p4hl/
  • src/
  • dlls/
  • ChangeSorter.cpp
  • View
  • Commits
  • Open Download .zip Download (1 KB)
// ChangeSorter.cpp: implementation of the ChangeSorter class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ChangeSorter.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

ChangeSorter::ChangeSorter()
{
	head = NULL;
}

ChangeSorter::~ChangeSorter()
{
	if (head != NULL) delete head;
}

void ChangeSorter::AddChange(StrBuf input)
{
	if (head == NULL)
	{
		head = new ChangeNode(input);
		return;
	} 
	if (input == head->change) {
		return;
	}
	ChangeNode* newnode = new ChangeNode(input);
	if (newnode->value == head->value) {
		return;
	}
	if (newnode->value < head->value)
	{
		newnode->next = head;
		head = newnode;
		return;
	}
	ChangeNode* prev = head;
	ChangeNode* working = head->next;
	while (working != NULL)
	{
		if (working->value == newnode->value) {
			return;
		}
		if (working->value > newnode->value)
		{
			newnode->next = working;
			prev->next = newnode;
			return;
		}
		prev = prev->next;
		working = working->next;
	}
	prev->next = newnode;
	return;
}

int ChangeSorter::GetPos(StrBuf input)
{
	if (head == NULL) return 0;
	int val = atoi((const char*) input.Text());
	ChangeNode* working = head;
	int counter = 1;
	while (val > working->value)
	{
		if (working->next == NULL) break;
		working = working->next;
		counter++;
	}
	if (val == working->value) return counter;
	return 0;
}
# Change User Description Committed
#6 1709 Sam Stafford Propagate bug fix and include change from p4hltest.

No functional change - P4HL never used "numarrows" so the bug
doesn't affect it.
#5 1689 Sam Stafford Integrate 02.1 API and code cleanup to P4HL.
 Lots of work.  Phew.
#4 1548 Sam Stafford Integrate RevType change to make sure it works.
 It does.
#3 1420 Sam Stafford Dummy integrate changes made thus far back to mainline, so it doesn't
get propagated back there later.
#2 1349 Sam Stafford Overhaul to ChangeSorter implementation.
 Member variables of ChangeSorter have
been made private, since nothing was (or should be) accessing them directly, and
methods, which remain public, should look the same from outside.

The original ChangeSorter was quick and dirty because I wanted to see P4HL working
quickly, but it was also about as inefficient as possible because it used single links
and linear searches.

The new and improved ChangeSorter is a doubly-linked list with intelligent searching
that's geared toward sequential access of elements that are close to each other, which
is indeed the nature of most of the queries that P4HL makes.  Even in a worst-case
scenario, this version should perform at least as well as the old one.

No noticeable performance change or bugs as of yet, but in theory we'd see the difference
with really large sets of data (ones larger than P4HL can show us).
#1 937 Sam Stafford Renaming my guest directory to the more conventional
sam_stafford.
//guest/samwise/p4hl/src/dlls/ChangeSorter.cpp
#1 936 Sam Stafford Adding P4HL to the public depot.
 See relnotes.txt for
installation instructions; all relevant files are under
p4hl/dist.

Source code is under p4hl/src in the form of a VC++ project.