Exemplo n.º 1
0
 //! Returns the number of variable values for which both finite_assignments
 //! agree.
 inline size_t assignment_agreement(const finite_assignment &fa1, 
                                    const finite_assignment &fa2) {
   finite_domain allvars = set_intersect(keys(fa1), keys(fa2));
   size_t count = 0;
   foreach(finite_variable *v, allvars) {
     count += (safe_get(fa1, v) == safe_get(fa2, v));
   }
   return count;
 }
Exemplo n.º 2
0
 double squared_error<finite_assignment>
 (const finite_assignment& a, const finite_assignment& b,
  const finite_domain& vars) {
   double val(0);
   foreach(finite_variable* v, vars)
     if (safe_get(a, v) != safe_get(b, v))
       ++val;
   return val;
 }
Exemplo n.º 3
0
 double squared_error<vector_assignment>
 (const vector_assignment& a, const vector_assignment& b,
  const vector_domain& vars) {
   double val(0);
   foreach(vector_variable* v, vars) {
     vec diff(safe_get(a, v) - safe_get(b, v));
     double sdiff = norm(diff,2);
     val += sdiff * sdiff;
   }
Exemplo n.º 4
0
 void remap(std::map<Key, T>& map,
            const std::map<T, T>& val_map) {
   typedef std::pair<Key, T> pair_type;
   for(pair_type& pair: map) {
     pair.second = safe_get(val_map, pair.second);
   }
 }
Exemplo n.º 5
0
 std::vector<T> values(const std::map<Key, T>& m, 
                       const std::vector<Key>& keys) {
   std::vector<T> output;
   for(const Key &i: keys) {
     output.push_back(safe_get(m, i));
   }
   return output;
 }
Exemplo n.º 6
0
 std::map<Key, NewT>
 remap(const std::map<Key, OldT>& map,
       const std::map<OldT, NewT>& val_map) {
   std::map<Key, NewT> output;
   typedef std::pair<Key, OldT> pair_type;
   for(const pair_type& pair: map) {
     output[pair.first] = safe_get(val_map, pair.second);
   }
   return output;
 }
Exemplo n.º 7
0
 std::map<NewKey, T>
 rekey(const std::map<OldKey, T>& map,
       const std::map<OldKey, NewKey>& key_map) {
   std::map<NewKey, T> output;
   typedef std::pair<OldKey, T> pair_type;
   for(const pair_type& pair: map) {
     output[safe_get(key_map, pair.first)] = pair.second;
   }
   return output;
 }
Exemplo n.º 8
0
int main()
{
    std::ios::sync_with_stdio(false);

    std::map<std::string, int> map;
    std::cout << "put" << std::endl;

    safe_get(map, "big+str+in+heap+area+always");

    return 0;
}
Exemplo n.º 9
0
bool GraphicManager::loadSpriteData(const FileName& datafile, wxString& error, wxArrayString& warnings)
{
	FileReadHandle fh(nstr(datafile.GetFullPath()));

	if(!fh.isOk()) {
		error = "Failed to open file for reading";
		return false;
	}

#define safe_get(func, ...) do {\
		if(!fh.get##func(__VA_ARGS__)) {\
			error = wxstr(fh.getErrorMessage()); \
			return false; \
		} \
	} while(false)


	uint32_t sprSignature;
	safe_get(U32, sprSignature);

	uint32_t total_pics = 0;
	if(is_extended) {
		safe_get(U32, total_pics);
	} else {
		uint16_t u16 = 0;
		safe_get(U16, u16);
		total_pics = u16;
	}

	if(!g_settings.getInteger(Config::USE_MEMCACHED_SPRITES)) {
		spritefile = nstr(datafile.GetFullPath());
		unloaded = false;
		return true;
	}

	std::vector<uint32_t> sprite_indexes;
	for(uint32_t i = 0; i < total_pics; ++i) {
		uint32_t index;
		safe_get(U32, index);
		sprite_indexes.push_back(index);
	}

	// Now read individual sprites
	int id = 1;
	for(std::vector<uint32_t>::iterator sprite_iter = sprite_indexes.begin(); sprite_iter != sprite_indexes.end(); ++sprite_iter, ++id) {
		uint32_t index = *sprite_iter + 3;
		fh.seek(index);
		uint16_t size;
		safe_get(U16, size);

		ImageMap::iterator it = image_space.find(id);
		if(it != image_space.end()) {
			GameSprite::NormalImage* spr = dynamic_cast<GameSprite::NormalImage*>(it->second);
			if(spr && size > 0) {
				if(spr->size > 0) {
					wxString ss;
					ss << "items.spr: Duplicate GameSprite id " << id;
					warnings.push_back(ss);
					fh.seekRelative(size);
				} else {
					spr->id = id;
					spr->size = size;
					spr->dump = newd uint8_t[size];
					if(!fh.getRAW(spr->dump, size)) {
						error = wxstr(fh.getErrorMessage()); \
						return false;
					}
				}
			}
		} else {
			fh.seekRelative(size);
		}
	}
#undef safe_get
	unloaded = false;
	return true;
}
Exemplo n.º 10
0
bool ItemDatabase::loadFromOtb(const FileName& datafile, wxString& error, wxArrayString& warnings)
{
	std::string filename = nstr((datafile.GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR) + datafile.GetFullName()));
	DiskNodeFileReadHandle f(filename, StringVector(1, "OTBI"));

	if(!f.isOk()) {
		error = wxT("Couldn't open file \"") + wxstr(filename) + wxT("\":") + wxstr(f.getErrorMessage());
		return false;
	}

	BinaryNode* root = f.getRootNode();

#define safe_get(node, func, ...) do {\
		if(!node->get##func(__VA_ARGS__)) {\
			error = wxstr(f.getErrorMessage()); \
			return false; \
		} \
	} while(false)

	// Read root flags
	root->skip(1); // Type info
	//uint32_t flags =

	root->skip(4); // Unused?

	uint8_t attr;
	safe_get(root, U8, attr);
	if(attr == ROOT_ATTR_VERSION) {
		uint16_t datalen;
		if(!root->getU16(datalen) || datalen != 4 + 4 + 4 + 1*128) {
			error = wxT("items.otb: Size of version header is invalid, updated .otb version?");
			return false;
		}
		safe_get(root, U32, MajorVersion);	// items otb format file version
		safe_get(root, U32, MinorVersion);	// client version
		safe_get(root, U32, BuildNumber);	// revision
		std::string csd;
		csd.resize(128);
		
		if(!root->getRAW((uint8_t*)csd.data(), 128)) { // CSDVersion ??
			error = wxstr(f.getErrorMessage());
			return false;
		}
	} else {
		error = wxT("Expected ROOT_ATTR_VERSION as first node of items.otb!");
	}

	if(settings.getInteger(Config::CHECK_SIGNATURES)) {
		if(gui.GetCurrentVersion().getOTBVersion().format_version != MajorVersion) {
			error = wxT("Unsupported items.otb version (version ") + i2ws(MajorVersion) + wxT(")");
			return false;
		}
	}

	BinaryNode* itemNode = root->getChild();
	switch(MajorVersion) {
		case 1: return loadFromOtbVer1(itemNode, error, warnings);
		case 2: return loadFromOtbVer2(itemNode, error, warnings);
		case 3: return loadFromOtbVer3(itemNode, error, warnings);
	}
	return true;
}
Exemplo n.º 11
0
void translate(Table& envT, Table& userT)
{

  enum QueueType { UNKNOWN = -1, SLURM = 1, SGE, SLURM_TACC, PBS, LSF };
  QueueType queueType = UNKNOWN;

  // Pick type of queuing system.

  if (envT.count("SGE_ACCOUNT"))
    queueType = SGE;
  else if (envT.count("SLURM_TACC_ACCOUNT") || envT.count("SLURM_TACC_JOBNAME"))
    queueType = SLURM_TACC;
  else if (envT.count("SBATCH_ACCOUNT"))
    queueType = SLURM;
  else if (envT.count("PBS_JOBID"))
    queueType = PBS;
  else if (envT.count("LSF_VERSION"))
    queueType = LSF;
  

  // Now fill in num_cores, num_nodes, account, job_id, queue, submit_host in userT from the environment
  if (queueType == SGE)
    {
      userT["num_cores"]   = safe_get(userT, "num_tasks",   "1");
      userT["num_nodes"]   = safe_get(envT,  "NHOSTS",      "1");
      userT["account"]     = safe_get(envT,  "SGE_ACCOUNT", "unknown");
      userT["job_id"]      = safe_get(envT,  "JOB_ID",      "unknown");
      userT["queue"]       = safe_get(envT,  "QUEUE",       "unknown");
      userT["submit_host"] = "unknown";
    }
  else if (queueType == SLURM_TACC || queueType == SLURM )
    {
      userT["num_cores"]   = safe_get(userT, "num_tasks",           "1");
      userT["num_nodes"]   = safe_get(envT,  "SLURM_NNODES",        "1"); 
      userT["job_id"]      = safe_get(envT,  "SLURM_JOB_ID",        "unknown");
      userT["queue"]       = safe_get(envT,  "SLURM_QUEUE",         "unknown");
      userT["submit_host"] = safe_get(envT,  "SLURM_SUBMIT_HOST",   "unknown");
      if (queueType == SLURM_TACC)
        userT["account"]   = safe_get(envT,  "SLURM_TACC_ACCOUNT",  "unknown");
    }
  else if (queueType == PBS)
    {
      userT["num_cores"]   = safe_get(userT, "num_tasks",     "1");
      userT["num_nodes"]   = safe_get(envT,  "PBS_NUM_NODES", "1"); 
      userT["job_id"]      = safe_get(envT,  "PBS_JOBID",     "unknown");
      userT["queue"]       = safe_get(envT,  "PBS_QUEUE",     "unknown");
      userT["submit_host"] = safe_get(envT,  "PBS_O_HOST",    "unknown");
      userT["account"]     = safe_get(envT,  "PBS_ACCOUNT",   "unknown");
    }
  else if (queueType == LSF)
    {
      // We must count the number of "words" in mcpuA.
      // We find the number of words by counting space blocks and add 1;
      // then divide by 2. then convert to a string.
      std::string mcpuA    = safe_get(envT,  "LSB_MCPU_HOSTS",  "a 1");
      std::string::size_type idx;
      int count = 1;
      idx = 0;
      while (1)
        {
          idx = mcpuA.find(" ",idx);
          if (idx == std::string::npos)
            break;
          count++;
          idx = mcpuA.find_first_not_of(" ",idx+1);
        }
      count /= 2;
      std::ostringstream sstream;
      sstream << count; 
      
      userT["num_cores"]   = safe_get(userT, "num_tasks",        "1");
      userT["num_nodes"]   = sstream.str();
      userT["job_id"]      = safe_get(envT,  "LSB_JOBID",        "unknown");
      userT["queue"]       = safe_get(envT,  "LSB_QUEUE",        "unknown");
      userT["submit_host"] = safe_get(envT,  "LSB_EXEC_CLUSTER", "unknown");
      userT["account"]     = "unknown";
    }
  else
    {
      userT["num_cores"]   = "1";
      userT["num_nodes"]   = "1";
      userT["job_id"]      = "unknown";
      userT["queue"]       = "unknown";
      userT["submit_host"] = "unknown";
      userT["account"]     = "unknown";
    }
}
Exemplo n.º 12
0
void translate(Table& envT, Table& userT, DTable& userDT)
{
  enum QueueType { UNKNOWN = -1, SLURM = 1, SGE, PBS, LSF };
  QueueType queueType = UNKNOWN;

  // Pick type of queuing system.


  if (envT.count("SGE_ACCOUNT"))
    queueType = SGE;
  else if (envT.count("SLURM_JOB_ID"))
    queueType = SLURM;
  else if (envT.count("PBS_JOBID"))
    queueType = PBS;
  else if (envT.count("LSF_VERSION"))
    queueType = LSF;

  // userDT["num_tasks"] has a safe default value of 1 if not overridden by the run.
  userDT["num_cores"]  = userDT["num_tasks"]; 

  // Now fill in num_cores, num_nodes, account, job_id, queue, submit_host in userT from the environment
  if (queueType == SGE)
    {
      userT["account"]     = safe_get(envT,        "SGE_ACCOUNT", "unknown");
      userT["job_id"]      = safe_get(envT,        "JOB_ID",      "unknown");
      userT["queue"]       = safe_get(envT,        "QUEUE",       "unknown");
      userT["submit_host"] = "unknown";
      userDT["num_nodes"]  = strtod(safe_get(envT, "NHOSTS",      "1"),        (char **) NULL);
    }
  else if (queueType == SLURM )
    {
      userT["job_id"]      = safe_get(envT,        "SLURM_JOB_ID",        "unknown");
      userT["queue"]       = safe_get(envT,        "SLURM_JOB_PARTITION", "unknown");
      userT["submit_host"] = safe_get(envT,        "SLURM_SUBMIT_HOST",   "unknown");
      userT["account"]     = safe_get(envT,        "SLURM_JOB_ACCOUNT",   "unknown");
      userDT["num_nodes"]  = strtod(safe_get(envT, "SLURM_NNODES",        "1"),       (char **) NULL);
    }
  else if (queueType == PBS)
    {
      std::string job_id   = safe_get(envT,        "PBS_JOBID",     "unknown");
      std::size_t idx      = job_id.find_first_not_of("0123456789[]");
      userT["job_id"]      = job_id.substr(0,idx);
      userT["queue"]       = safe_get(envT,        "PBS_QUEUE",     "unknown");
      userT["submit_host"] = safe_get(envT,        "PBS_O_HOST",    "unknown");
      userT["account"]     = safe_get(envT,        "PBS_ACCOUNT",   "unknown");
      userDT["num_nodes"]  = strtod(safe_get(envT, "PBS_NUM_NODES", "1"),       (char **) NULL);;
    }
  else if (queueType == LSF)
    {
      // We must count the number of "words" in mcpuA.
      // We find the number of words by counting space blocks and add 1;
      // then divide by 2. then convert to a string.
      std::string mcpuA    = safe_get(envT,  "LSB_MCPU_HOSTS",  "a 1");
      std::string::size_type idx;
      int count = 1;
      idx = 0;
      while (1)
        {
          idx = mcpuA.find(" ",idx);
          if (idx == std::string::npos)
            break;
          count++;
          idx = mcpuA.find_first_not_of(" ",idx+1);
        }
      count /= 2;

      userT["job_id"]      = safe_get(envT,  "LSB_JOBID",        "unknown");
      userT["queue"]       = safe_get(envT,  "LSB_QUEUE",        "unknown");
      userT["submit_host"] = safe_get(envT,  "LSB_EXEC_CLUSTER", "unknown");
      userT["account"]     = "unknown";
      userDT["num_nodes"]  = (double) count;
    }
  else
    {
      userT["job_id"]      = "unknown";
      userT["queue"]       = "unknown";
      userT["submit_host"] = "unknown";
      userT["account"]     = "unknown";
      userDT["num_nodes"]  = 1.0;
    }
}