1 /*
2 * cWeighedIndex.cc
3 * Avida
4 *
5 * Called "weighted_index.cc" prior to 12/7/05.
6 * Copyright 1999-2011 Michigan State University. All rights reserved.
7 * Copyright 1993-2003 California Institute of Technology.
8 *
9 *
10 * This file is part of Avida.
11 *
12 * Avida is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
14 *
15 * Avida is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License along with Avida.
19 * If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23 #include "cOrderedWeightedIndex.h"
24 #include <iostream>
25
26 using namespace std;
27
cOrderedWeightedIndex()28 cOrderedWeightedIndex::cOrderedWeightedIndex()
29 : item_weight(0)
30 , cum_weight(0)
31 , item_value(0)
32 {
33 }
34
35
~cOrderedWeightedIndex()36 cOrderedWeightedIndex::~cOrderedWeightedIndex()
37 {
38 }
39
40
41
42
SetWeight(int value,double in_weight)43 void cOrderedWeightedIndex::SetWeight(int value, double in_weight)
44 {
45 int cur_size = item_value.GetSize();
46 item_weight.Resize(cur_size + 1);
47 cum_weight.Resize(cur_size + 1);
48 item_value.Resize(cur_size + 1);
49
50 item_value[cur_size] = value;
51 item_weight[cur_size] = in_weight;
52 cum_weight[cur_size] = (cur_size == 0) ? in_weight : cum_weight[cur_size-1] + in_weight;
53 }
54
FindPosition(double position)55 int cOrderedWeightedIndex::FindPosition(double position){
56 return Lookup(position, 0, GetSize()-1);
57 }
58
59
Lookup(double weight,int ndxA,int ndxE)60 int cOrderedWeightedIndex::Lookup(double weight, int ndxA, int ndxE)
61 {
62 int mid = ndxA + (ndxE - ndxA) / 2;
63
64 if (cum_weight[mid]-item_weight[mid] <= weight && cum_weight[mid] > weight)
65 {
66 return item_value[mid];
67 }
68
69 if (cum_weight[mid] > weight)
70 {
71 return Lookup(weight, ndxA, mid-1);
72 }
73 else{
74 return Lookup(weight, mid+1, ndxE);
75 }
76 }
77
78
79
80