vclass.cc 2.06 KB
Newer Older
Robert Ricci's avatar
Robert Ricci committed
1 2
/*
 * EMULAB-COPYRIGHT
3
 * Copyright (c) 2000-2006 University of Utah and the Flux Group.
Robert Ricci's avatar
Robert Ricci committed
4 5 6
 * All rights reserved.
 */

7
#include "port.h"
Leigh B. Stoller's avatar
Leigh B. Stoller committed
8

9
#include <stdlib.h>
10 11 12 13 14 15 16 17

#include <boost/config.hpp>
#include <boost/utility.hpp>
#include <boost/property_map.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>

using namespace boost;
18 19 20

#include "common.h"
#include "vclass.h"
21 22
#include "delay.h"
#include "physical.h"
23 24
#include "virtual.h"

25 26
name_vclass_map vclass_map;

27
void tb_vclass::add_type(fstring type)
28
{
29
  members[type]=0;
30
  if (dominant.empty() ||
31
      (members[dominant] == 0)) {
32 33 34 35
    dominant = type;
  }
}

36
bool tb_vclass::has_type(fstring type) {
37 38 39 40
    return (members.find(type) != members.end());
}


41
double tb_vclass::assign_node(fstring type)
42 43
{
  double new_score = score;
44
  members[type] += 1;
45 46

  if (type != dominant) {
47
    if (members[dominant] != 0) {
48 49
      new_score = weight;
    }
50
    if (members[type] > members[dominant]) {
51 52 53 54 55 56 57 58 59
      dominant = type;
    }
  }

  double delta = new_score-score;
  score=new_score;
  return delta;
}

60
double tb_vclass::unassign_node(fstring type)
61 62
{
  double new_score = 0;
63
  members[type] -= 1;
64 65

  int curmax = 0;
66 67 68
  for (members_map::iterator dit=members.begin();
       dit != members.end();++dit) {
    int n = (*dit).second;
69 70 71 72 73
    if (n > curmax) {
      if (curmax != 0) {
	new_score = weight;
      }
      curmax = n;
74
      dominant = (*dit).first;
75 76 77 78 79 80 81 82
    }
  }
  
  double delta = new_score-score;
  score=new_score;
  return delta;
}

83
fstring tb_vclass::choose_type()
84
{
85 86
  // This may take some tweaking - i.e. might want to make more
  // efficient, although members is usually a very small hash.
87
  if (RANDOM()%2 == 0) {
88 89
    return dominant;
  }
90
  int r = RANDOM()%members.size();
91 92
  members_map::iterator dit;
  for (dit=members.begin();dit != members.end();++dit) {
93 94 95
    if (r == 0) break;
    r--;
  }
96
  return (*dit).first;
97 98
}

99
bool tb_vclass::empty() {
100 101 102 103 104 105 106
    members_map::iterator dit;
    for (dit=members.begin();dit != members.end();++dit) {
        if ((*dit).second > 0) {
            return false;
        }
    }
    return true;
107
}