intarray.cc #1

  • //
  • 2016-1/
  • guest/
  • dannyz_snps/
  • p4/
  • 2016-1/
  • support/
  • intarray.cc
  • View
  • Commits
  • Open Download .zip Download (1 KB)
/*
 * Copyright 1995, 1996 Perforce Software.  All rights reserved.
 *
 * This file is part of Perforce - the FAST SCM System.
 */

# include <stdhdrs.h>

// Remove these next two:
# include <debug.h>
# include <strbuf.h>

# include "intarray.h"

// If you know that this IntArray's values are in sorted order, and you want to
// find the slot number of a particular value, this routine binary-searches
// the array for you. Make sure you call SetCount() first to establish the
// limits of the search.
//
// It returns -1 if the value isn't present in this array.
//
// For example, if your array contains: 1,3,5,6,7,8,9,10, then
//  Find(0) => -1, Find(1) => 0, Find(2) => -1, Find(5)=> 2, Find(8) => 5
//
// If you think you need to modify this routine, read DbArray::Search first.
int
IntArray::Find( int v )
{
	int lo = 0;
	int hi = count;

	for(;;)
	{
	    int index = ( lo + hi ) / 2;

	    if( lo == hi )
		return index < count && ints[ index ] == v ? index : -1;

	    if( v <= ints[ index ] )
		hi = index;
	    else if( index != lo )
		lo = index;
	    else
		lo = hi;
	}
}
# Change User Description Committed
#1 21443 dannyz_snps "Forking branch 2016-1 of perforce_software-p4 to dannyz_snps-p4."
//guest/perforce_software/p4/2016-1/support/intarray.cc
#1 19472 Liz Lam Initial add of the 2016.1 p4/p4api source code.