-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoundedSortedArray.H
151 lines (123 loc) · 3.32 KB
/
BoundedSortedArray.H
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/***********************************************************************
BoundedSortedArray.H
BOOM : Bioinformatics Object Oriented Modules
Copyright (C)2012 William H. Majoros ([email protected]).
This is OPEN SOURCE SOFTWARE governed by the Gnu General Public
License (GPL) version 3, as described at www.opensource.org.
***********************************************************************/
#ifndef INCL_BoundedSortedArray_H
#define INCL_BoundedSortedArray_H
#include "Array1D.H"
namespace BOOM {
enum KeepType
{
KEEP_LOW_VALUES,
KEEP_HIGH_VALUES
};
template<class ObjectType,class ScalarType>
class BoundedSortedArray
{
struct Element
{
ObjectType object;
ScalarType scalar;
};
BOOM::Array1D<Element> array;
KeepType keepType;
int maxSize, currentSize;
int search(ScalarType);
public:
BoundedSortedArray(int size,KeepType);
bool add(const ObjectType &,ScalarType);
ObjectType &operator[](int);
const ObjectType &operator[](int) const;
ScalarType getValue(int i) const {return array[i].scalar;}
int size();
};
template<class ObjectType,class ScalarType>
BoundedSortedArray<ObjectType,ScalarType>::BoundedSortedArray(int size,
KeepType k)
: array(size), keepType(k), maxSize(size), currentSize(0)
{
// ctor
}
template<class ObjectType,class ScalarType>
bool BoundedSortedArray<ObjectType,ScalarType>::add(const ObjectType &o,
ScalarType s)
{
// Determine where the new object belongs -- returned index is always >=0,
// and is either an empty slot, or a slot past the right end of the array,
// or the location of an object which is > this one.
int index=search(s);
// If the array is under capacity, just insert it there
if(currentSize<maxSize)
{
if(index<currentSize)
for(int i=currentSize ; i>index ; --i)
array[i]=array[i-1];
Element &e=array[index];
e.object=o;
e.scalar=s;
++currentSize;
}
// Otherwise, lose an object on the left or right end of the array, shift
// everything over, and then insert the new object
else
{
if(keepType==KEEP_LOW_VALUES)
for(int i=maxSize-1 ; i>index ; --i)
array[i]=array[i-1];
else
// keepType==KEEP_HIGH_VALUES
{
--index;
if(index<0) return false;
for(int i=0 ; i<index ; ++i)
array[i]=array[i+1];
}
if(index<maxSize)
{
Element &e=array[index];
e.object=o;
e.scalar=s;
}
else return false;
}
return true;
}
template<class ObjectType,class ScalarType>
ObjectType &BoundedSortedArray<ObjectType,ScalarType>::operator[](int i)
{
return array[i].object;
}
template<class ObjectType,class ScalarType>
const ObjectType &BoundedSortedArray<ObjectType,ScalarType>::operator[](
int i) const
{
return array[i].object;
}
template<class ObjectType,class ScalarType>
int BoundedSortedArray<ObjectType,ScalarType>::search(ScalarType s)
{
/*
Returns the index of a slot which is either undefined, or is
strictly greater than s.
*/
if(currentSize==0) return 0;
int min=0, max=currentSize;
while(min<max)
{
int mid=(min+max)/2;
ScalarType midValue=array[mid].scalar;
if(s<midValue) max=mid;
else min=mid+1;
}
return min;
}
template<class ObjectType,class ScalarType>
int BoundedSortedArray<ObjectType,ScalarType>::size()
{
return currentSize;
}
}
#endif