00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00035 #include "rolelist.h"
00036 #include "pathenvironment.h"
00037
00038 RoleList *RoleList::instance = NULL;
00039
00040 void RoleList::initInstance(PathEnvironment * _penv) {
00041 instance = new RoleList(_penv);
00042 }
00043
00044 RoleList *RoleList::getInstance() {
00045 return instance;
00046 }
00047
00048 RoleList::RoleList(PathEnvironment * _penv):
00049 penv(_penv), max_role_id(0), max_role_id_computed(false) {
00050 }
00051
00052 RoleList::~RoleList() {
00053 for (unsigned i = 0; i < roles.size(); i++) {
00054 delete roles[i];
00055 }
00056 }
00057
00058 Role *RoleList::getFreshRole(unsigned basing_var, ROLE_TYPE type,
00059 PathExpression * from_var) {
00060 unsigned basing_fsa = FSAMap::getInstance()->getFSA(basing_var);
00061 PathExpression *path_between = penv->getPathBetween(basing_var, basing_fsa);
00062
00063 Role *role =
00064 new Role(roles.size(), type, basing_var, basing_fsa, path_between,
00065 from_var);
00066
00067 roles.push_back(role);
00068
00069 return role;
00070 }
00071
00072 unsigned RoleList::getMaxRoleId() {
00073 if (!max_role_id_computed) {
00074 computeMaxRoleId();
00075 }
00076
00077 return max_role_id;
00078 }
00079
00080 bool RoleList::removeRole(Role * role) {
00081 if (role) {
00082 for (unsigned i = 0; i < roles.size(); i++) {
00083 if (roles[i] == role) {
00084 delete roles[i];
00085
00086 roles.erase(roles.begin() + i);
00087 return true;
00088 }
00089 }
00090 }
00091
00092 return false;
00093 }
00094
00095 void RoleList::sortRolesForSignOffs() {
00096 if (roles.size() > 0) {
00097
00098
00099
00100 unsigned sort_count = 0;
00101 Role *role_tmp;
00102
00103
00104 for (unsigned i = 0; i < roles.size(); i++) {
00105 for (unsigned j = 0; j < (roles.size() - (i + 1)); j++) {
00106
00107
00108 if (roles[j]->getBasingFSA() < roles[j + 1]->getBasingFSA()) {
00109 role_tmp = roles[j];
00110 roles[j] = roles[j + 1];
00111 roles[j + 1] = role_tmp;
00112 sort_count++;
00113 }
00114 }
00115 if (sort_count == 0) {
00116 break;
00117 } else {
00118 sort_count = 0;
00119 }
00120 }
00121
00122 sort_count = 0;
00123
00124 for (unsigned i = 0; i < roles.size(); i++) {
00125 for (unsigned j = 0; j < (roles.size() - (i + 1)); j++) {
00126
00127 if ((roles[j]->getBasingFSA() == roles[j + 1]->getBasingFSA())
00128 && (roles[j]->getConcatenationPath()->getWeight() <
00129 roles[j + 1]->getConcatenationPath()->getWeight())) {
00130 role_tmp = roles[j];
00131 roles[j] = roles[j + 1];
00132 roles[j + 1] = role_tmp;
00133 sort_count++;
00134 }
00135 }
00136 if (sort_count == 0) {
00137 break;
00138 } else {
00139 sort_count = 0;
00140 }
00141 }
00142 }
00143 }
00144
00145 void RoleList::computeMaxRoleId() {
00146 for (unsigned i = 0; i < roles.size(); i++) {
00147 if (roles[i]->getId() > max_role_id)
00148 max_role_id = roles[i]->getId();
00149 }
00150
00151 max_role_id_computed = true;
00152 }