blob: de2f3a973db3b5d34f772045a17d4185e9280bee (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
#include <stdio.h>
#include <string.h>
#include "h_collection.h"
HSortedCollection::HSortedCollection( ccIndex aLimit, ccIndex aDelta ) :
HCollection( aLimit, aDelta ),
duplicates( false )
{
}
int HSortedCollection::compare( const void *key1, const void *key2 ) const
{
return strcmp(( const char* )key1, ( const char* )key2 );
}
ccIndex HSortedCollection::indexOf( const void *item )
{
ccIndex i;
if ( search( keyOf( item ), i ) == false )
return ccNotFound;
if ( duplicates )
while( i < count && item != items[i] )
i++;
return ( i < count ) ? i : ccNotFound;
}
bool HSortedCollection::insert( void* item )
{
ccIndex i;
if ( search( keyOf( item ), i ) == 0 || duplicates )
return atInsert( i, item );
return false;
}
const void* HSortedCollection::keyOf( const void* item ) const
{
return item;
}
void* HSortedCollection::search( const void* key )
{
ccIndex temp;
if ( search( key, temp ) == false )
return 0;
return items[ temp ];
}
bool HSortedCollection::search( const void* key, ccIndex& index )
{
ccIndex l = 0;
ccIndex h = count - 1;
bool res = false;
while( l <= h )
{ ccIndex i = ( l+h )/2;
int c = compare( keyOf( items[ i ] ), key );
if ( c < 0 )
l = i + 1;
else
{ h = i - 1;
if ( c == 0 )
{ res = true;
lastItem = i;
if ( !duplicates )
l = i;
} } }
index = l;
return res;
}
|