9 #ifndef __libutilxx__sella__algo__WRR_H__
10 #define __libutilxx__sella__algo__WRR_H__
12 #include "../../common.h"
20 template <
class T>
class WRR;
26 WRRNode(
const T & data,
double weight = 1.0)
throw ();
37 template <
class T>
class WRR {
41 void insert(const std::
string & identifier, const T & node,
double weight = 1.0) throw ();
42 void remove(const std::
string & identifier) throw ();
43 void update(const std::
string & identifier,
double weight = 1.0) throw ();
50 typename std::map<std::
string,
WRRNode<T> > node;
51 typename std::map<std::
string,
WRRNode<T> >::iterator iter;
56 template <class T> sella::algo::
WRR<T>::
WRR() throw () :
73 this->node.insert(std::make_pair(identifier, WRRNode<T>(node, weight)));
74 this->total += weight;
76 for (
auto iter = this->node.begin(); iter != this->node.end(); ++iter) {
77 iter->second.rate = iter->second.weight / this->total;
82 auto iter = this->node.find(identifier);
84 if (iter != this->node.end()) {
85 this->total -= iter->second.weight;
87 this->node.erase(iter);
90 for (iter = this->node.begin(); iter != this->node.end(); ++iter) {
91 iter->second.rate = iter->second.weight / this->total;
96 auto iter = this->node.find(identifier);
98 if (iter != this->node.end()) {
99 iter->weight = weight;
101 this->total -= iter->second.weight;
102 this->total += weight;
105 for (iter = this->node.begin(); iter != this->node.end(); ++iter) {
106 iter->second.rate = iter->second.weight / this->total;
112 while (this->iter != this->node.end()) {
113 auto item = this->iter;
117 item->second.points += item->second.rate;
119 if (item->second.points >= 1.0) {
120 item->second.points -= 1.0;
122 return item->second.data;
125 this->iter = this->node.begin();