vclass.h 2.68 KB
Newer Older
Robert Ricci's avatar
Robert Ricci committed
1 2
/*
 * EMULAB-COPYRIGHT
Robert Ricci's avatar
Robert Ricci committed
3
 * Copyright (c) 2000-2007 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 42
  typedef hash_map<fstring,int> members_map;   
    
43
  void add_type(fstring type);	// Add a member of a certain type to the
44 45
				// vclass

46
  bool has_type(fstring type) const; // Does the vclass contain the given type?
47

48
  bool empty() const;                // True if no vnodes use this vlcass
49

50 51 52
  // 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.
53 54
  double assign_node(fstring type);
  double unassign_node(fstring type);
55 56

  // Semi randomly choose a type from a vclass.
57
  fstring choose_type() const;
58

59 60 61 62 63 64 65 66 67 68 69 70
  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;
  }
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
  
  // 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

99
};
100

101
typedef hash_map<fstring,tb_vclass*> name_vclass_map;
102
extern name_vclass_map vclass_map;
103

104
#endif