vclass.h 3.18 KB
Newer Older
Robert Ricci's avatar
Robert Ricci committed
1
/*
Robert Ricci's avatar
Robert Ricci committed
2
 * Copyright (c) 2000-2007 University of Utah and the Flux Group.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 
 * {{{EMULAB-LICENSE
 * 
 * This file is part of the Emulab network testbed software.
 * 
 * This file is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * 
 * This file is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this file.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * }}}
Robert Ricci's avatar
Robert Ricci committed
22 23
 */

24 25 26
#ifndef __VCLASS_H
#define __VCLASS_H

27
#include "port.h"
28
#include "fstring.h"
29 30 31 32

#include <iostream>
using namespace std;

33 34 35 36 37 38 39 40 41 42
// tb_vclass represents a virtual equivalence class.  The main purpose
// of the code here is to monitor which members of the class are being
// used, what types that are being used as, and calculating the
// appropriate score changes.  Each vclass gives some contribution to
// the score and as nodes are assigned and unassigned this score changes.

// The membership of nodes is not stored here.  Rather each vnode
// stores which vclass it belongs to.  This class is more abstract and
// represents the scoring model.

43 44
class tb_vclass {
public:
45
  tb_vclass(fstring n,double w) : name(n), weight(w), score(0) {;}
46

47 48
  typedef hash_map<fstring,int> members_map;   
    
49
  void add_type(fstring type);	// Add a member of a certain type to the
50 51
				// vclass

52
  bool has_type(fstring type) const; // Does the vclass contain the given type?
53

54
  bool empty() const;                // True if no vnodes use this vlcass
55

56 57 58
  // The next two routines report the *change* in score.  The score
  // for the vclass as a whole is 0 if all nodes are of the dominant
  // type and the weight if they aren't.
59 60
  double assign_node(fstring type);
  double unassign_node(fstring type);
61 62

  // Semi randomly choose a type from a vclass.
63
  fstring choose_type() const;
64

65 66 67 68 69 70 71 72 73 74 75 76
  friend ostream &operator<<(ostream &o, const tb_vclass& c)
  {
    o << "vclass: " << c.name << " dominant=" << c.dominant <<
      " weight=" << c.weight << " score=" << c.score << endl;
    o << "  ";
    for (members_map::const_iterator dit=c.members.begin();
	 dit!=c.members.end();++dit) {
      o << (*dit).first << ":" << (*dit).second << " ";
    }
    o << endl;
    return o;
  }
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
  
  // Get the list of members
  const members_map &get_members() const {
    return(members);
  }
  
  // Just get the name
  fstring get_name() const {
    return(name);
  }
  
  // Get the name of the dominant type
  fstring get_dominant() const {
    return(dominant);
  }

private:
      
  fstring name;			// Name of the vclass

  members_map members;		// Maps type to number of members of that
				// type.
  
  double weight;		// Weight of class
  double score;			// the current score of the vclass
  
  fstring dominant;		// Current dominant type

105
};
106

107
typedef hash_map<fstring,tb_vclass*> name_vclass_map;
108
extern name_vclass_map vclass_map;
109

110
#endif