vclass.h 2.37 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 8 9
#ifndef __VCLASS_H
#define __VCLASS_H

10
#include "port.h"
11
#include "fstring.h"
12 13 14 15 16

/*
 * We have to do these includes differently depending on which version of gcc
 * we're compiling with
 */
17
#ifdef NEW_GCC
18 19 20 21 22 23 24 25 26
#include <ext/hash_map>
using namespace __gnu_cxx;
#else
#include <hash_map>
#endif

#include <iostream>
using namespace std;

27 28 29 30 31 32 33 34 35 36
// 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.

37 38
class tb_vclass {
public:
39
  tb_vclass(fstring n,double w) : name(n), weight(w), score(0) {;}
40

41
  fstring name;			// Name of the vclass
42

43
  typedef hash_map<fstring,int> members_map; 
44 45 46 47
  members_map members;		// Maps type to number of members of that
				// type.
  
  double weight;		// Weight of class
48 49
  double score;			// the current score of the vclass
  
50
  void add_type(fstring type);	// Add a member of a certain type to the
51 52
				// vclass

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

55
  fstring dominant;		// Current dominant type
56

57 58
  bool empty();                 // True if no vnodes use this vlcass

59 60 61
  // 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.
62 63
  double assign_node(fstring type);
  double unassign_node(fstring type);
64 65

  // Semi randomly choose a type from a vclass.
66
  fstring choose_type();
67

68 69 70 71 72 73 74 75 76 77 78 79
  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;
  }
80
};
81

82
typedef hash_map<fstring,tb_vclass*> name_vclass_map;
83
extern name_vclass_map vclass_map;
84

85
#endif