<commit_before>// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "webkit/fileapi/file_system_usage_cache.h"

#include "base/file_path.h"
#include "base/file_util.h"
#include "base/pickle.h"

namespace fileapi {

const char FileSystemUsageCache::kUsageFileName[] = ".usage";
const char FileSystemUsageCache::kUsageFileHeader[] = "FSU0";
const int FileSystemUsageCache::kUsageFileHeaderSize = 4;
const int FileSystemUsageCache::kUsageFileSize =
    sizeof(Pickle::Header) +
    FileSystemUsageCache::kUsageFileHeaderSize +
    sizeof(int32) + sizeof(int64);

// static
int64 FileSystemUsageCache::GetUsage(const FilePath& usage_file_path) {
  uint32 dirty = 0;
  int64 fs_usage;
  fs_usage = Read(usage_file_path, &dirty);

  if (fs_usage < 0)
    return -1;

  return fs_usage;
}

// static
int32 FileSystemUsageCache::GetDirty(const FilePath& usage_file_path) {
  uint32 dirty = 0;
  int64 fs_usage;
  fs_usage = Read(usage_file_path, &dirty);

  if (fs_usage < 0)
    return -1;

  return static_cast<int32>(dirty);
}

// static
bool FileSystemUsageCache::IncrementDirty(const FilePath& usage_file_path) {
  uint32 dirty = 0;
  int64 fs_usage;
  fs_usage = Read(usage_file_path, &dirty);

  if (fs_usage < 0)
    return false;

  return Write(usage_file_path, dirty + 1, fs_usage) >= 0;
}

// static
bool FileSystemUsageCache::DecrementDirty(const FilePath& usage_file_path) {
  uint32 dirty = 0;
  int64 fs_usage;
  fs_usage = Read(usage_file_path, &dirty);

  if (fs_usage < 0 || dirty <= 0)
    return false;

  return Write(usage_file_path, dirty - 1, fs_usage) >= 0;
}

// static
int FileSystemUsageCache::AtomicUpdateUsageByDelta(
    const FilePath& usage_file_path, int64 delta) {
  uint32 dirty = 0;
  int64 fs_usage;
  fs_usage = Read(usage_file_path, &dirty);

  return Write(usage_file_path, dirty, fs_usage + delta);
}

// static
int FileSystemUsageCache::UpdateUsage(const FilePath& usage_file_path,
                                      int64 fs_usage) {
  return Write(usage_file_path, 0, fs_usage);
}

// static
bool FileSystemUsageCache::Exists(const FilePath& usage_file_path) {
  return file_util::PathExists(usage_file_path);
}

// static
bool FileSystemUsageCache::Delete(const FilePath& usage_file_path) {
  return file_util::Delete(usage_file_path, true);
}

// static
int64 FileSystemUsageCache::Read(const FilePath& usage_file_path,
                                 uint32* dirty) {
  char buffer[kUsageFileSize];
  const char *header;
  DCHECK(!usage_file_path.empty());
  if (kUsageFileSize !=
      file_util::ReadFile(usage_file_path, buffer, kUsageFileSize))
    return -1;
  Pickle read_pickle(buffer, kUsageFileSize);
  void* iter = NULL;
  int64 fs_usage;

  if (!read_pickle.ReadBytes(&iter, &header, kUsageFileHeaderSize) ||
      !read_pickle.ReadUInt32(&iter, dirty) ||
      !read_pickle.ReadInt64(&iter, &fs_usage))
    return -1;

  if (header[0] != kUsageFileHeader[0] ||
      header[1] != kUsageFileHeader[1] ||
      header[2] != kUsageFileHeader[2] ||
      header[3] != kUsageFileHeader[3])
    return -1;

  return fs_usage;
}

// static
int FileSystemUsageCache::Write(const FilePath& usage_file_path,
                                uint32 dirty, int64 fs_usage) {
  Pickle write_pickle;
  write_pickle.WriteBytes(kUsageFileHeader, kUsageFileHeaderSize);
  write_pickle.WriteUInt32(dirty);
  write_pickle.WriteInt64(fs_usage);

  DCHECK(!usage_file_path.empty());
  FilePath temporary_usage_file_path;
  file_util::CreateTemporaryFileInDir(usage_file_path.DirName(),
                                      &temporary_usage_file_path);
  int bytes_written = file_util::WriteFile(temporary_usage_file_path,
                                           (const char *)write_pickle.data(),
                                           write_pickle.size());
  if (bytes_written != kUsageFileSize)
    return -1;

  if (file_util::ReplaceFile(temporary_usage_file_path, usage_file_path))
    return bytes_written;
  else
    return -1;
}

}
<commit_msg>Force updating filesystem usage cache<commit_after>// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "webkit/fileapi/file_system_usage_cache.h"

#include "base/file_path.h"
#include "base/file_util.h"
#include "base/pickle.h"

namespace fileapi {

const char FileSystemUsageCache::kUsageFileName[] = ".usage";
const char FileSystemUsageCache::kUsageFileHeader[] = "FSU1";
const int FileSystemUsageCache::kUsageFileHeaderSize = 4;
const int FileSystemUsageCache::kUsageFileSize =
    sizeof(Pickle::Header) +
    FileSystemUsageCache::kUsageFileHeaderSize +
    sizeof(int32) + sizeof(int64);

// static
int64 FileSystemUsageCache::GetUsage(const FilePath& usage_file_path) {
  uint32 dirty = 0;
  int64 fs_usage;
  fs_usage = Read(usage_file_path, &dirty);

  if (fs_usage < 0)
    return -1;

  return fs_usage;
}

// static
int32 FileSystemUsageCache::GetDirty(const FilePath& usage_file_path) {
  uint32 dirty = 0;
  int64 fs_usage;
  fs_usage = Read(usage_file_path, &dirty);

  if (fs_usage < 0)
    return -1;

  return static_cast<int32>(dirty);
}

// static
bool FileSystemUsageCache::IncrementDirty(const FilePath& usage_file_path) {
  uint32 dirty = 0;
  int64 fs_usage;
  fs_usage = Read(usage_file_path, &dirty);

  if (fs_usage < 0)
    return false;

  return Write(usage_file_path, dirty + 1, fs_usage) >= 0;
}

// static
bool FileSystemUsageCache::DecrementDirty(const FilePath& usage_file_path) {
  uint32 dirty = 0;
  int64 fs_usage;
  fs_usage = Read(usage_file_path, &dirty);

  if (fs_usage < 0 || dirty <= 0)
    return false;

  return Write(usage_file_path, dirty - 1, fs_usage) >= 0;
}

// static
int FileSystemUsageCache::AtomicUpdateUsageByDelta(
    const FilePath& usage_file_path, int64 delta) {
  uint32 dirty = 0;
  int64 fs_usage;
  fs_usage = Read(usage_file_path, &dirty);

  return Write(usage_file_path, dirty, fs_usage + delta);
}

// static
int FileSystemUsageCache::UpdateUsage(const FilePath& usage_file_path,
                                      int64 fs_usage) {
  return Write(usage_file_path, 0, fs_usage);
}

// static
bool FileSystemUsageCache::Exists(const FilePath& usage_file_path) {
  return file_util::PathExists(usage_file_path);
}

// static
bool FileSystemUsageCache::Delete(const FilePath& usage_file_path) {
  return file_util::Delete(usage_file_path, true);
}

// static
int64 FileSystemUsageCache::Read(const FilePath& usage_file_path,
                                 uint32* dirty) {
  char buffer[kUsageFileSize];
  const char *header;
  DCHECK(!usage_file_path.empty());
  if (kUsageFileSize !=
      file_util::ReadFile(usage_file_path, buffer, kUsageFileSize))
    return -1;
  Pickle read_pickle(buffer, kUsageFileSize);
  void* iter = NULL;
  int64 fs_usage;

  if (!read_pickle.ReadBytes(&iter, &header, kUsageFileHeaderSize) ||
      !read_pickle.ReadUInt32(&iter, dirty) ||
      !read_pickle.ReadInt64(&iter, &fs_usage))
    return -1;

  if (header[0] != kUsageFileHeader[0] ||
      header[1] != kUsageFileHeader[1] ||
      header[2] != kUsageFileHeader[2] ||
      header[3] != kUsageFileHeader[3])
    return -1;

  return fs_usage;
}

// static
int FileSystemUsageCache::Write(const FilePath& usage_file_path,
                                uint32 dirty, int64 fs_usage) {
  Pickle write_pickle;
  write_pickle.WriteBytes(kUsageFileHeader, kUsageFileHeaderSize);
  write_pickle.WriteUInt32(dirty);
  write_pickle.WriteInt64(fs_usage);

  DCHECK(!usage_file_path.empty());
  FilePath temporary_usage_file_path;
  file_util::CreateTemporaryFileInDir(usage_file_path.DirName(),
                                      &temporary_usage_file_path);
  int bytes_written = file_util::WriteFile(temporary_usage_file_path,
                                           (const char *)write_pickle.data(),
                                           write_pickle.size());
  if (bytes_written != kUsageFileSize)
    return -1;

  if (file_util::ReplaceFile(temporary_usage_file_path, usage_file_path))
    return bytes_written;
  else
    return -1;
}

}
<|endoftext|>


<commit_before>#include <stdio.h>
#include <string.h>
#include <math.h>

#include "SGP4.h"
#include "Julian.h"
#include "Timespan.h"
#include "Tle.h"

#include "gtg.h"
#include "gtgutil.h"
#include "gtgtle.h"
#include "gtgshp.h"

#include "gtgtrace.h"

/*
	InitTime

	Parameters:
		desc, string to read time specification from. Accepts four formats:
				now - current time
				epoch - reference time of orbit info
				YYYY-MM-DD HH:MM:SS.SSSSSS UTC
				S - UNIX time (seconds since 1970-01-01 00:00:00)
		now, reference date to use if "now" time is specified
		tle, orbital elements to use if orbit "epoch" is specified

	Returns:
		A Julian date object initialized to time requested by desc
		Aborts if desc cannot be parsed.
*/
Julian InitTime(const char *desc, Julian now, Tle tle)
{
	Julian time;

	if (0 == strcmp("now", desc)) {
		time = now;
	} else if (0 == strcmp("epoch", desc)) {
		time = tle.Epoch();
	} else {
		int year, month, day, hour, minute;
		double second;
		if (6 == sscanf(desc, "%4d-%2d-%2d %2d:%2d:%9lf UTC", &year, &month, &day, &hour, &minute, &second)) {
			time = Julian(year, month, day, hour, minute, second);
		} else {
			double unixtime;
			if (1 == sscanf(desc, "%lf", &unixtime)) {
				time = Julian((time_t)unixtime);
			} else {
				Fail("cannot parse time: %s\n", desc);
			}
		}
	}

	return time;
}

/*
	InitInterval

	Parameters:
		units, type of unit used to specify interval length
		interval_length, length of each interval in given units

	Return:
		A Timespan object initialized to the specified interval
*/
Timespan InitInterval(enum interval_unit_type units, double interval_length)
{
	Timespan interval;

	switch (units) {
		case seconds:
			interval.AddSeconds(interval_length);
			break;
		case minutes:
			interval.AddSeconds(interval_length * 60.0);
			break;
		case hours:
			interval.AddSeconds(interval_length * 60.0 * 60.0);
			break;
		case days:
			interval.AddSeconds(interval_length * 60 * 60 * 24);
			break;
	}

	return interval;
}

void GenerateGroundTrack(Tle& tle, SGP4& model, Julian& now)
{
	int step = 0;
	Julian time, endtime;
	Timespan interval;
	Eci eci(now, 0, 0, 0);

	/* for line output mode */
	Eci prevEci(eci);
	int prevSet = 0;

	/* Initialize the step interval */
	interval = InitInterval(cfg.unit, cfg.interval);

	/* Initialize the starting timestamp; default to epoch */
	time = InitTime(cfg.start == NULL ? "epoch" : cfg.start, now, tle);
	Note("Start: %s\n", time.ToString().c_str());

	/* Initialize the ending timestamp, if needed */
	if (NULL != cfg.end) {

		endtime = InitTime(cfg.end, now, tle);

		/* Sanity check 1 */
		if (time >= endtime) {
			Fail("end time (%s) not after start time (%s)\n", endtime.ToString().c_str(), time.ToString().c_str());
		}

		/* Sanity check 2 */
		if (interval > endtime - time) {
			Fail("interval (%lf minutes) exceeds period between start time and end time (%lf minutes).\n", interval.GetTotalMinutes(), (endtime - time).GetTotalMinutes());
		}

		Note("End: %s\n", endtime.ToString().c_str());
	}

	// assemble base shapefile output from directory path and tle name
	// not robust
	std::string shpbase;
	if (NULL != cfg.basepath) {
		shpbase += cfg.basepath;
		if ('/' != shpbase[shpbase.length() - 1]) {
			shpbase += '/';
		}
	}
	if (NULL != cfg.prefix) {
		shpbase += cfg.prefix;
	}
	shpbase += tle.GetName();
	if (NULL != cfg.suffix) {
		shpbase += cfg.suffix;
	}

	ShapefileWriter shout(shpbase.c_str(), cfg.features, cfg.obslat, cfg.obslon, cfg.obsalt, cfg.prj);

	while (1) {

		/* where is the satellite now? */
		try {
			eci = model.FindPosition(time);
		} catch (SatelliteException &e) {
			Note("satellite exception (stopping at step %d): %s\n", step, e.what());
			break;
		} catch (DecayedException &e) {
			Note("satellite decayed (stopping at step %d).\n", step);
			break;
		}

		if (line == cfg.features) {
			if (prevSet) {
				shout.output(&prevEci, &eci, cfg.split);
				step++;
			} else {
				/* prevSet is only false on the first pass, which yields an
				   extra interval not counted against step count - needed
				   since line segments imply n+1 intervals for n steps */
				prevSet = 1;
			}

			prevEci = eci;

		} else {

			shout.output(&eci);

			step++;
		}

		/* increment time interval */
		time += interval;

		/* stop ground track once we've exceeded step count or end time */
		if ((0 != cfg.steps) && (step >= cfg.steps)) {
			break;
		} else if ((NULL != cfg.end) && (time >= endtime)) {
			break;
		}

	}

	shout.close();
}

void InitGroundTrace(Tle& tle, Julian& now)
{
	try {
		SGP4 model(tle);
		GenerateGroundTrack(tle, model, now);
	} catch (SatelliteException &e) {
		Fail("cannot initialize satellite model: %s\n", e.what());
	}
}
<commit_msg>Moved code that assembles basepath out of --output, --prefix, and --suffix to its own function<commit_after>#include <stdio.h>
#include <string.h>
#include <math.h>

#include "SGP4.h"
#include "Julian.h"
#include "Timespan.h"
#include "Tle.h"

#include "gtg.h"
#include "gtgutil.h"
#include "gtgtle.h"
#include "gtgshp.h"

#include "gtgtrace.h"

/*
	InitTime

	Parameters:
		desc, string to read time specification from. Accepts four formats:
				now - current time
				epoch - reference time of orbit info
				YYYY-MM-DD HH:MM:SS.SSSSSS UTC
				S - UNIX time (seconds since 1970-01-01 00:00:00)
		now, reference date to use if "now" time is specified
		tle, orbital elements to use if orbit "epoch" is specified

	Returns:
		A Julian date object initialized to time requested by desc
		Aborts if desc cannot be parsed.
*/
Julian InitTime(const char *desc, Julian now, Tle tle)
{
	Julian time;

	if (0 == strcmp("now", desc)) {
		time = now;
	} else if (0 == strcmp("epoch", desc)) {
		time = tle.Epoch();
	} else {
		int year, month, day, hour, minute;
		double second;
		if (6 == sscanf(desc, "%4d-%2d-%2d %2d:%2d:%9lf UTC", &year, &month, &day, &hour, &minute, &second)) {
			time = Julian(year, month, day, hour, minute, second);
		} else {
			double unixtime;
			if (1 == sscanf(desc, "%lf", &unixtime)) {
				time = Julian((time_t)unixtime);
			} else {
				Fail("cannot parse time: %s\n", desc);
			}
		}
	}

	return time;
}

/*
	InitInterval

	Parameters:
		units, type of unit used to specify interval length
		interval_length, length of each interval in given units

	Return:
		A Timespan object initialized to the specified interval
*/
Timespan InitInterval(enum interval_unit_type units, double interval_length)
{
	Timespan interval;

	switch (units) {
		case seconds:
			interval.AddSeconds(interval_length);
			break;
		case minutes:
			interval.AddSeconds(interval_length * 60.0);
			break;
		case hours:
			interval.AddSeconds(interval_length * 60.0 * 60.0);
			break;
		case days:
			interval.AddSeconds(interval_length * 60 * 60 * 24);
			break;
	}

	return interval;
}

std::string BuildBasepath(const std::string& rootname)
{
	std::string shpbase;

	if (NULL != cfg.basepath) {
		shpbase += cfg.basepath;
		if ('/' != shpbase[shpbase.length() - 1]) {
			shpbase += '/';
		}
	}

	if (NULL != cfg.prefix) {
		shpbase += cfg.prefix;
	}

	shpbase += rootname;

	if (NULL != cfg.suffix) {
		shpbase += cfg.suffix;
	}

	return shpbase;
}

void GenerateGroundTrack(Tle& tle, SGP4& model, Julian& now)
{
	int step = 0;
	Julian time, endtime;
	Timespan interval;
	Eci eci(now, 0, 0, 0);

	/* for line output mode */
	Eci prevEci(eci);
	int prevSet = 0;

	/* Initialize the step interval */
	interval = InitInterval(cfg.unit, cfg.interval);

	/* Initialize the starting timestamp; default to epoch */
	time = InitTime(cfg.start == NULL ? "epoch" : cfg.start, now, tle);
	Note("Start: %s\n", time.ToString().c_str());

	/* Initialize the ending timestamp, if needed */
	if (NULL != cfg.end) {

		endtime = InitTime(cfg.end, now, tle);

		/* Sanity check 1 */
		if (time >= endtime) {
			Fail("end time (%s) not after start time (%s)\n", endtime.ToString().c_str(), time.ToString().c_str());
		}

		/* Sanity check 2 */
		if (interval > endtime - time) {
			Fail("interval (%lf minutes) exceeds period between start time and end time (%lf minutes).\n", interval.GetTotalMinutes(), (endtime - time).GetTotalMinutes());
		}

		Note("End: %s\n", endtime.ToString().c_str());
	}

	std::string p(BuildBasepath(tle.GetName()));
	ShapefileWriter shout(p.c_str(), cfg.features, cfg.obslat, cfg.obslon, cfg.obsalt, cfg.prj);

	while (1) {

		/* where is the satellite now? */
		try {
			eci = model.FindPosition(time);
		} catch (SatelliteException &e) {
			Note("satellite exception (stopping at step %d): %s\n", step, e.what());
			break;
		} catch (DecayedException &e) {
			Note("satellite decayed (stopping at step %d).\n", step);
			break;
		}

		if (line == cfg.features) {
			if (prevSet) {
				shout.output(&prevEci, &eci, cfg.split);
				step++;
			} else {
				/* prevSet is only false on the first pass, which yields an
				   extra interval not counted against step count - needed
				   since line segments imply n+1 intervals for n steps */
				prevSet = 1;
			}

			prevEci = eci;

		} else {

			shout.output(&eci);

			step++;
		}

		/* increment time interval */
		time += interval;

		/* stop ground track once we've exceeded step count or end time */
		if ((0 != cfg.steps) && (step >= cfg.steps)) {
			break;
		} else if ((NULL != cfg.end) && (time >= endtime)) {
			break;
		}

	}

	shout.close();
}

void InitGroundTrace(Tle& tle, Julian& now)
{
	try {
		SGP4 model(tle);
		GenerateGroundTrack(tle, model, now);
	} catch (SatelliteException &e) {
		Fail("cannot initialize satellite model: %s\n", e.what());
	}
}
<|endoftext|>


<commit_before>//=======================================================================
// Copyright Baptiste Wicht 2011.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)
//=======================================================================

#include <algorithm>
#include <functional>

#include "Context.hpp"
#include "Utils.hpp"

using std::map;
using std::string;
using std::endl;
using std::vector;
using std::unordered_set;
using std::unordered_map;

using namespace eddic;

int Context::currentVariable = 0;

void Context::storeVariable(int index, std::shared_ptr<Variable> variable){
    m_stored[index] = variable;
}

bool Context::exists(const std::string& variable) const {
    bool found = m_visibles.find(variable) != m_visibles.end();

    if(!found){
        if(m_parent){
            return m_parent->exists(variable);
        }
    }

    return found;
}

std::shared_ptr<Variable> Context::getVariable(const std::string& variable) const {
    VisibleVariables::const_iterator iter = m_visibles.find(variable);

    if(iter == m_visibles.end()){
        return m_parent->getVariable(variable);
    }

    return getVariable(iter->second);
}

std::shared_ptr<Variable> Context::getVariable(int index) const {
    StoredVariables::const_iterator iter = m_stored.find(index);

    if(iter == m_stored.end()){
        return m_parent->getVariable(index);
    }

    return iter->second;
}

void FunctionContext::write(AssemblyFileWriter& writer){
    StoredVariables::const_iterator it = m_stored.begin();
    StoredVariables::const_iterator end = m_stored.end();

    int s = 0;

    for ( ; it != end; ++it) {
        s += size(it->second->type());
    }

    if(s > 0){
        writer.stream() << "subl $" << s << " , %esp" << std::endl;
    }
}

void FunctionContext::release(AssemblyFileWriter& writer){
    StoredVariables::const_iterator it = m_stored.begin();
    StoredVariables::const_iterator end = m_stored.end();

    int s = 0;

    for ( ; it != end; ++it) {
        s += size(it->second->type());
    }

    if(s > 0){
        writer.stream() << "addl $" << s << " , %esp" << std::endl;
    }
}

void GlobalContext::write(AssemblyFileWriter& writer){
    StoredVariables::const_iterator it = m_stored.begin();
    StoredVariables::const_iterator end = m_stored.end();

    for ( ; it != end; ++it) {
        if (it->second->type() == INT) {
            writer.stream() << ".comm VI" << it->second->position().name() << ",4,4" << endl;
        } else if (it->second->type() == STRING) {
            writer.stream() << ".comm VS" << it->second->position().name() << ",8,4" << endl;
        }
    }
}

std::shared_ptr<Variable> GlobalContext::addVariable(const std::string& variable, Type type){
    Position position(GLOBAL, variable);

    std::shared_ptr<Variable> v(new Variable(variable, type, position));

    m_visibles[variable] = currentVariable;

    storeVariable(currentVariable, v);

    currentVariable++;

    return v;
}

std::shared_ptr<Variable> FunctionContext::newParameter(const std::string& variable, Type type){
    Position position(PARAMETER, currentParameter);

    currentParameter += size(type);

    return std::shared_ptr<Variable>(new Variable(variable, type, position));
}

std::shared_ptr<Variable> FunctionContext::newVariable(const std::string& variable, Type type){
    Position position(STACK, currentPosition);

    currentPosition += size(type);

    return std::shared_ptr<Variable>(new Variable(variable, type, position));
}

std::shared_ptr<Variable> FunctionContext::addVariable(const std::string& variable, Type type){
    std::shared_ptr<Variable> v = newVariable(variable, type);

    m_visibles[variable] = currentVariable;

    storeVariable(currentVariable, v);

    currentVariable++;

    return v;
}

std::shared_ptr<Variable> FunctionContext::addParameter(const std::string& parameter, Type type){
   std::shared_ptr<Variable> v = newParameter(parameter, type);

   m_visibles[parameter] = currentVariable;

   storeVariable(currentVariable, v);

   currentVariable++;

   return v;
}

std::shared_ptr<Variable> BlockContext::addVariable(const std::string& variable, Type type){
    std::shared_ptr<Variable> v = m_functionContext->newVariable(variable, type);

    m_visibles[variable] = currentVariable;

    m_functionContext->storeVariable(currentVariable, v);

    currentVariable++;

    return v;
}

void Variable::moveToRegister(AssemblyFileWriter& writer, std::string reg){
    if(m_type == INT){
        if(m_position.isStack()){
            writer.stream() << "movl -" << m_position.offset() << "(%ebp), " << reg << endl;
        } else if(m_position.isParameter()){
            writer.stream() << "movl " << m_position.offset() << "(%ebp), " << reg << endl;
        } else if(m_position.isGlobal()){
            writer.stream() << "movl VI" << m_position.name() << ", " << reg << endl;
        }
   } else if (m_type == STRING){
       //TODO Should never be called
   }
}

void Variable::moveToRegister(AssemblyFileWriter& writer, std::string reg1, std::string reg2){
    if(m_type == INT){
       //TODO Should never be called
   } else if (m_type == STRING){
       if(m_position.isStack()){
           writer.stream() << "movl -" << m_position.offset() << "(%ebp), " << reg1 << endl;
           writer.stream() << "movl -" << (m_position.offset() + 4) << "(%ebp), " << reg2 << endl;
       } else if(m_position.isParameter()){
           writer.stream() << "movl " << m_position.offset() << "(%ebp), " << reg1 << endl;
           writer.stream() << "movl " << (m_position.offset() + 4) << "(%ebp), " << reg2 << endl;
       } else {
           writer.stream() << "movl VS" << m_position.name() << ", " << reg1 << endl;
           writer.stream() << "movl VS" << m_position.name() << "+4, " << reg2 << endl;
       }
   }
}

void Variable::moveFromRegister(AssemblyFileWriter& writer, std::string reg){
    if(m_type == INT){
        if(m_position.isStack()){
            writer.stream() << "movl " << reg << ", -" << m_position.offset() << "(%ebp)" << endl;
        } else if(m_position.isParameter()){
            writer.stream() << "movl " << reg << ", " << m_position.offset() << "(%ebp)" << endl;
        } else if(m_position.isGlobal()){
            writer.stream() << "movl " << reg << ", VI" << m_position.name()  << endl;
        }
   } else if (m_type == STRING){
       //TODO Should never be called
   }
}

void Variable::moveFromRegister(AssemblyFileWriter& writer, std::string reg1, std::string reg2){
    if(m_type == INT){
       //TODO Should never be called
   } else if (m_type == STRING){
       if(m_position.isStack()){
           writer.stream() << "movl " << reg1 << ", -" << m_position.offset() << "(%ebp)" << endl;
           writer.stream() << "movl " << reg2 << ", -" << (m_position.offset() + 4) << "(%ebp)" << endl;
       } else if(m_position.isParameter()){
           writer.stream() << "movl " << reg1 << ", " << m_position.offset() << "(%ebp)" << endl;
           writer.stream() << "movl " << reg2 << ", " << (m_position.offset() + 4) << "(%ebp)" << endl;
       } else {
           writer.stream() << "movl " << reg1 << ", VS" << m_position.name() << endl;
           writer.stream() << "movl " << reg2 << ", VS" << m_position.name() << "+4" << endl;
       }
   }
}

void Variable::pushToStack(AssemblyFileWriter& writer){
    switch (m_type) {
        case INT:
            if(m_position.isStack()){
                writer.stream() << "pushl -" << m_position.offset() << "(%ebp)" << std::endl;
            } else if(m_position.isParameter()){
                writer.stream() << "pushl " << m_position.offset() << "(%ebp)" << std::endl;
            } else if(m_position.isGlobal()){
                writer.stream() << "pushl VI" << m_position.name() << std::endl;
            }

            break;
        case STRING:
            if(m_position.isStack()){
                writer.stream() << "pushl -" << (m_position.offset() + 4) << "(%ebp)" << std::endl;
                writer.stream() << "pushl -" << m_position.offset() << "(%ebp)" << std::endl;
            } else if(m_position.isParameter()){
                writer.stream() << "pushl " << (m_position.offset() + 4) << "(%ebp)" << std::endl;
                writer.stream() << "pushl " << m_position.offset() << "(%ebp)" << std::endl;
            } else if(m_position.isGlobal()){
                writer.stream() << "pushl VS" << m_index << endl;
                writer.stream() << "pushl VS" << m_index << "+4" << std::endl;
            }

            break;
    }
}

void Variable::popFromStack(AssemblyFileWriter& writer){
    switch (m_type) {
        case INT:
            writer.stream() << "movl (%esp), %eax" << endl;
            writer.stream() << "addl $4, %esp" << endl;

            moveFromRegister(writer, "%eax");

            break;
        case STRING:
            writer.stream() << "movl (%esp), %eax" << endl;
            writer.stream() << "movl 4(%esp), %ebx" << endl;
            writer.stream() << "addl $8, %esp" << endl;

            moveFromRegister(writer, "%eax", "%ebx");

            break;
    }
}
<commit_msg>Use the new for_each to facilitate iterations<commit_after>//=======================================================================
// Copyright Baptiste Wicht 2011.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)
//=======================================================================

#include <algorithm>
#include <functional>

#include "Context.hpp"
#include "Utils.hpp"

using std::map;
using std::string;
using std::endl;
using std::vector;
using std::unordered_set;
using std::unordered_map;

using namespace eddic;

int Context::currentVariable = 0;

void Context::storeVariable(int index, std::shared_ptr<Variable> variable){
    m_stored[index] = variable;
}

bool Context::exists(const std::string& variable) const {
    bool found = m_visibles.find(variable) != m_visibles.end();

    if(!found){
        if(m_parent){
            return m_parent->exists(variable);
        }
    }

    return found;
}

std::shared_ptr<Variable> Context::getVariable(const std::string& variable) const {
    auto iter = m_visibles.find(variable);

    if(iter == m_visibles.end()){
        return m_parent->getVariable(variable);
    }

    return getVariable(iter->second);
}

std::shared_ptr<Variable> Context::getVariable(int index) const {
    auto iter = m_stored.find(index);

    if(iter == m_stored.end()){
        return m_parent->getVariable(index);
    }

    return iter->second;
}

void FunctionContext::write(AssemblyFileWriter& writer){
    int s = 0;
    for(auto it : m_stored){
        s += size(it.second->type());
    }

    if(s > 0){
        writer.stream() << "subl $" << s << " , %esp" << std::endl;
    }
}

void FunctionContext::release(AssemblyFileWriter& writer){
    int s = 0;
    for(auto it : m_stored){
        s += size(it.second->type());
    }

    if(s > 0){
        writer.stream() << "addl $" << s << " , %esp" << std::endl;
    }
}

void GlobalContext::write(AssemblyFileWriter& writer){
    for(auto it : m_stored){
        if (it.second->type() == INT) {
            writer.stream() << ".comm VI" << it.second->position().name() << ",4,4" << endl;
        } else if (it.second->type() == STRING) {
            writer.stream() << ".comm VS" << it.second->position().name() << ",8,4" << endl;
        }
    }
}

std::shared_ptr<Variable> GlobalContext::addVariable(const std::string& variable, Type type){
    Position position(GLOBAL, variable);

    std::shared_ptr<Variable> v(new Variable(variable, type, position));

    m_visibles[variable] = currentVariable;

    storeVariable(currentVariable, v);

    currentVariable++;

    return v;
}

std::shared_ptr<Variable> FunctionContext::newParameter(const std::string& variable, Type type){
    Position position(PARAMETER, currentParameter);

    currentParameter += size(type);

    return std::shared_ptr<Variable>(new Variable(variable, type, position));
}

std::shared_ptr<Variable> FunctionContext::newVariable(const std::string& variable, Type type){
    Position position(STACK, currentPosition);

    currentPosition += size(type);

    return std::shared_ptr<Variable>(new Variable(variable, type, position));
}

std::shared_ptr<Variable> FunctionContext::addVariable(const std::string& variable, Type type){
    std::shared_ptr<Variable> v = newVariable(variable, type);

    m_visibles[variable] = currentVariable;

    storeVariable(currentVariable, v);

    currentVariable++;

    return v;
}

std::shared_ptr<Variable> FunctionContext::addParameter(const std::string& parameter, Type type){
   std::shared_ptr<Variable> v = newParameter(parameter, type);

   m_visibles[parameter] = currentVariable;

   storeVariable(currentVariable, v);

   currentVariable++;

   return v;
}

std::shared_ptr<Variable> BlockContext::addVariable(const std::string& variable, Type type){
    std::shared_ptr<Variable> v = m_functionContext->newVariable(variable, type);

    m_visibles[variable] = currentVariable;

    m_functionContext->storeVariable(currentVariable, v);

    currentVariable++;

    return v;
}

void Variable::moveToRegister(AssemblyFileWriter& writer, std::string reg){
    if(m_type == INT){
        if(m_position.isStack()){
            writer.stream() << "movl -" << m_position.offset() << "(%ebp), " << reg << endl;
        } else if(m_position.isParameter()){
            writer.stream() << "movl " << m_position.offset() << "(%ebp), " << reg << endl;
        } else if(m_position.isGlobal()){
            writer.stream() << "movl VI" << m_position.name() << ", " << reg << endl;
        }
   } else if (m_type == STRING){
       //TODO Should never be called
   }
}

void Variable::moveToRegister(AssemblyFileWriter& writer, std::string reg1, std::string reg2){
    if(m_type == INT){
       //TODO Should never be called
   } else if (m_type == STRING){
       if(m_position.isStack()){
           writer.stream() << "movl -" << m_position.offset() << "(%ebp), " << reg1 << endl;
           writer.stream() << "movl -" << (m_position.offset() + 4) << "(%ebp), " << reg2 << endl;
       } else if(m_position.isParameter()){
           writer.stream() << "movl " << m_position.offset() << "(%ebp), " << reg1 << endl;
           writer.stream() << "movl " << (m_position.offset() + 4) << "(%ebp), " << reg2 << endl;
       } else {
           writer.stream() << "movl VS" << m_position.name() << ", " << reg1 << endl;
           writer.stream() << "movl VS" << m_position.name() << "+4, " << reg2 << endl;
       }
   }
}

void Variable::moveFromRegister(AssemblyFileWriter& writer, std::string reg){
    if(m_type == INT){
        if(m_position.isStack()){
            writer.stream() << "movl " << reg << ", -" << m_position.offset() << "(%ebp)" << endl;
        } else if(m_position.isParameter()){
            writer.stream() << "movl " << reg << ", " << m_position.offset() << "(%ebp)" << endl;
        } else if(m_position.isGlobal()){
            writer.stream() << "movl " << reg << ", VI" << m_position.name()  << endl;
        }
   } else if (m_type == STRING){
       //TODO Should never be called
   }
}

void Variable::moveFromRegister(AssemblyFileWriter& writer, std::string reg1, std::string reg2){
    if(m_type == INT){
       //TODO Should never be called
   } else if (m_type == STRING){
       if(m_position.isStack()){
           writer.stream() << "movl " << reg1 << ", -" << m_position.offset() << "(%ebp)" << endl;
           writer.stream() << "movl " << reg2 << ", -" << (m_position.offset() + 4) << "(%ebp)" << endl;
       } else if(m_position.isParameter()){
           writer.stream() << "movl " << reg1 << ", " << m_position.offset() << "(%ebp)" << endl;
           writer.stream() << "movl " << reg2 << ", " << (m_position.offset() + 4) << "(%ebp)" << endl;
       } else {
           writer.stream() << "movl " << reg1 << ", VS" << m_position.name() << endl;
           writer.stream() << "movl " << reg2 << ", VS" << m_position.name() << "+4" << endl;
       }
   }
}

void Variable::pushToStack(AssemblyFileWriter& writer){
    switch (m_type) {
        case INT:
            if(m_position.isStack()){
                writer.stream() << "pushl -" << m_position.offset() << "(%ebp)" << std::endl;
            } else if(m_position.isParameter()){
                writer.stream() << "pushl " << m_position.offset() << "(%ebp)" << std::endl;
            } else if(m_position.isGlobal()){
                writer.stream() << "pushl VI" << m_position.name() << std::endl;
            }

            break;
        case STRING:
            if(m_position.isStack()){
                writer.stream() << "pushl -" << (m_position.offset() + 4) << "(%ebp)" << std::endl;
                writer.stream() << "pushl -" << m_position.offset() << "(%ebp)" << std::endl;
            } else if(m_position.isParameter()){
                writer.stream() << "pushl " << (m_position.offset() + 4) << "(%ebp)" << std::endl;
                writer.stream() << "pushl " << m_position.offset() << "(%ebp)" << std::endl;
            } else if(m_position.isGlobal()){
                writer.stream() << "pushl VS" << m_index << endl;
                writer.stream() << "pushl VS" << m_index << "+4" << std::endl;
            }

            break;
    }
}

void Variable::popFromStack(AssemblyFileWriter& writer){
    switch (m_type) {
        case INT:
            writer.stream() << "movl (%esp), %eax" << endl;
            writer.stream() << "addl $4, %esp" << endl;

            moveFromRegister(writer, "%eax");

            break;
        case STRING:
            writer.stream() << "movl (%esp), %eax" << endl;
            writer.stream() << "movl 4(%esp), %ebx" << endl;
            writer.stream() << "addl $8, %esp" << endl;

            moveFromRegister(writer, "%eax", "%ebx");

            break;
    }
}
<|endoftext|>


<commit_before>#include "Context.h"

#include <cmath>
#include <iostream>

#ifdef OPENGL
#include <glm/glm.hpp>
#endif

using namespace std;
using namespace canvas;

void
Context::resize(unsigned int _width, unsigned int _height) {
   getDefaultSurface().resize(_width, _height, (unsigned int)(_width * getDisplayScale()), (unsigned int)(_height * getDisplayScale()), getDefaultSurface().hasAlpha());
}

void
Context::rect(double x, double y, double w, double h) {
  moveTo(x, y);
  lineTo(x + w, y);
  lineTo(x + w, y + h);
  lineTo(x, y + h);
  closePath();
}

void
Context::fillRect(double x, double y, double w, double h) {
  beginPath();
  rect(x, y, w, h);
  fill();
}

void
Context::strokeRect(double x, double y, double w, double h) {
  beginPath();
  rect(x, y, w, h);
  stroke();
}

void
Context::renderText(RenderMode mode, const Style & style, const std::string & text, double x, double y) {
  if (hasShadow()) {
    float b = shadowBlur, bs = shadowBlur * getDisplayScale();
    int bi = int(ceil(b));
    auto shadow = createSurface(getDefaultSurface().getLogicalWidth() + 2 * bi, getDefaultSurface().getLogicalHeight() + 2 * bi, true);
    Style shadow_style = shadowColor;
    float shadow_alpha = shadowColor.alpha;
    shadow_style.color.alpha = 1.0f;
    shadow->renderText(mode, font, shadow_style, textBaseline, textAlign, text, x + shadowOffsetX + bi, y + shadowOffsetY + bi, lineWidth, getDisplayScale());
    shadow->gaussianBlur(bs, bs, shadow_alpha);
    getDefaultSurface().drawImage(*shadow, -bi, -bi, shadow->getActualWidth(), shadow->getActualHeight());
  }
  getDefaultSurface().renderText(mode, font, style, textBaseline, textAlign, text, x, y, lineWidth, getDisplayScale());
}

void
Context::renderPath(RenderMode mode, const Style & style) {
  if (hasShadow()) {
    float b = shadowBlur, bs = shadowBlur * getDisplayScale();
    float bi = int(ceil(b));
    auto shadow = createSurface(getDefaultSurface().getLogicalWidth() + 2 * bi, getDefaultSurface().getLogicalHeight() + 2 * bi, true);
    Style shadow_style = shadowColor;
    Path tmp_path = current_path;
    tmp_path.offset(shadowOffsetX + bi, shadowOffsetY + bi);

    shadow->renderPath(mode, tmp_path, shadow_style, lineWidth, getDisplayScale());
    shadow->gaussianBlur(bs, bs);

    getDefaultSurface().drawImage(*shadow, -bi, -bi, shadow->getActualWidth(), shadow->getActualHeight());
  }
  getDefaultSurface().renderPath(mode, current_path, style, getDisplayScale(), getDisplayScale());
}

void
Context::drawImage(Surface & img, double x, double y, double w, double h) {
  if (hasShadow()) {
    float b = shadowBlur, bs = shadowBlur * getDisplayScale();
    float bi = int(ceil(b));
    auto shadow = createSurface(getDefaultSurface().getLogicalWidth() + 2 * bi, getDefaultSurface().getLogicalHeight() + 2 * bi, true);

    shadow->drawImage(img, (x + bi + shadowOffsetX) * getDisplayScale(), (y + bi + shadowOffsetY) * getDisplayScale(), w * getDisplayScale(), h * getDisplayScale(), globalAlpha, imageSmoothingEnabled);
    shadow->colorFill(shadowColor);
    shadow->gaussianBlur(bs, bs);

    getDefaultSurface().drawImage(*shadow, -bi, -bi, shadow->getActualWidth(), shadow->getActualHeight());
  }
  getDefaultSurface().drawImage(img, x * getDisplayScale(), y * getDisplayScale(), w * getDisplayScale(), h * getDisplayScale(), globalAlpha, imageSmoothingEnabled);
}

void
Context::save() {
  restore_stack.push_back(SavedContext());
  auto & data = restore_stack.back();
  data.globalAlpha = globalAlpha;
  data.current_path = current_path;
  getDefaultSurface().save();
}

void
Context::restore() {
  if (!restore_stack.empty()) {
    auto & data = restore_stack.back();
    globalAlpha = data.globalAlpha;
    current_path = data.current_path;
  }
  getDefaultSurface().restore();
}

void
Context::clearRect(double x, double y, double w, double h) {
  beginPath();
  rect(x, y, w, h);
  renderPath(FILL, fillStyle, COPY);
}
<commit_msg>add operator to renderPath<commit_after>#include "Context.h"

#include <cmath>
#include <iostream>

#ifdef OPENGL
#include <glm/glm.hpp>
#endif

using namespace std;
using namespace canvas;

void
Context::resize(unsigned int _width, unsigned int _height) {
   getDefaultSurface().resize(_width, _height, (unsigned int)(_width * getDisplayScale()), (unsigned int)(_height * getDisplayScale()), getDefaultSurface().hasAlpha());
}

void
Context::rect(double x, double y, double w, double h) {
  moveTo(x, y);
  lineTo(x + w, y);
  lineTo(x + w, y + h);
  lineTo(x, y + h);
  closePath();
}

void
Context::fillRect(double x, double y, double w, double h) {
  beginPath();
  rect(x, y, w, h);
  fill();
}

void
Context::strokeRect(double x, double y, double w, double h) {
  beginPath();
  rect(x, y, w, h);
  stroke();
}

void
Context::renderText(RenderMode mode, const Style & style, const std::string & text, double x, double y) {
  if (hasShadow()) {
    float b = shadowBlur, bs = shadowBlur * getDisplayScale();
    int bi = int(ceil(b));
    auto shadow = createSurface(getDefaultSurface().getLogicalWidth() + 2 * bi, getDefaultSurface().getLogicalHeight() + 2 * bi, true);
    Style shadow_style = shadowColor;
    float shadow_alpha = shadowColor.alpha;
    shadow_style.color.alpha = 1.0f;
    shadow->renderText(mode, font, shadow_style, textBaseline, textAlign, text, x + shadowOffsetX + bi, y + shadowOffsetY + bi, lineWidth, getDisplayScale());
    shadow->gaussianBlur(bs, bs, shadow_alpha);
    getDefaultSurface().drawImage(*shadow, -bi, -bi, shadow->getActualWidth(), shadow->getActualHeight());
  }
  getDefaultSurface().renderText(mode, font, style, textBaseline, textAlign, text, x, y, lineWidth, getDisplayScale());
}

void
Context::renderPath(RenderMode mode, const Style & style, Operator op) {
  if (hasShadow()) {
    float b = shadowBlur, bs = shadowBlur * getDisplayScale();
    float bi = int(ceil(b));
    auto shadow = createSurface(getDefaultSurface().getLogicalWidth() + 2 * bi, getDefaultSurface().getLogicalHeight() + 2 * bi, true);
    Style shadow_style = shadowColor;
    Path tmp_path = current_path;
    tmp_path.offset(shadowOffsetX + bi, shadowOffsetY + bi);

    shadow->renderPath(mode, tmp_path, shadow_style, lineWidth, op, getDisplayScale());
    shadow->gaussianBlur(bs, bs);

    getDefaultSurface().drawImage(*shadow, -bi, -bi, shadow->getActualWidth(), shadow->getActualHeight());
  }
  getDefaultSurface().renderPath(mode, current_path, style, getDisplayScale(), op, getDisplayScale());
}

void
Context::drawImage(Surface & img, double x, double y, double w, double h) {
  if (hasShadow()) {
    float b = shadowBlur, bs = shadowBlur * getDisplayScale();
    float bi = int(ceil(b));
    auto shadow = createSurface(getDefaultSurface().getLogicalWidth() + 2 * bi, getDefaultSurface().getLogicalHeight() + 2 * bi, true);

    shadow->drawImage(img, (x + bi + shadowOffsetX) * getDisplayScale(), (y + bi + shadowOffsetY) * getDisplayScale(), w * getDisplayScale(), h * getDisplayScale(), globalAlpha, imageSmoothingEnabled);
    shadow->colorFill(shadowColor);
    shadow->gaussianBlur(bs, bs);

    getDefaultSurface().drawImage(*shadow, -bi, -bi, shadow->getActualWidth(), shadow->getActualHeight());
  }
  getDefaultSurface().drawImage(img, x * getDisplayScale(), y * getDisplayScale(), w * getDisplayScale(), h * getDisplayScale(), globalAlpha, imageSmoothingEnabled);
}

void
Context::save() {
  restore_stack.push_back(SavedContext());
  auto & data = restore_stack.back();
  data.globalAlpha = globalAlpha;
  data.current_path = current_path;
  getDefaultSurface().save();
}

void
Context::restore() {
  if (!restore_stack.empty()) {
    auto & data = restore_stack.back();
    globalAlpha = data.globalAlpha;
    current_path = data.current_path;
  }
  getDefaultSurface().restore();
}

void
Context::clearRect(double x, double y, double w, double h) {
  beginPath();
  rect(x, y, w, h);
  renderPath(FILL, fillStyle, COPY);
}
<|endoftext|>


<commit_before>/*
    Copyright 2005-2007 Adobe Systems Incorporated
    Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
    or a copy at http://stlab.adobe.com/licenses.html)
*/

#include "platform_display_text.hpp"

#include <GG/adobe/string.hpp>
#include <GG/adobe/future/widgets/headers/display.hpp>
#include <GG/adobe/future/widgets/headers/widget_utils.hpp>
#include <GG/adobe/future/widgets/headers/platform_metrics.hpp>

#include <GG/TextControl.h>
#include <GG/GUI.h>
#include <GG/StyleFactory.h>

#include <string>
#include <cassert>
#include <sstream>

namespace {

    std::string field_text(const std::string& label, adobe::any_regular_t value, GG::Clr label_color)
    {
        std::stringstream result;
        if (!label.empty())
            result << GG::RgbaTag(label_color) << label << "</rbga> ";
        if (value != adobe::any_regular_t()) {
            adobe::type_info_t type(value.type_info());
            if (type == adobe::type_info<double>() ||
                type == adobe::type_info<bool>() ||
                type == adobe::type_info<adobe::name_t>()) {
                result << value;
            } else {
                result << value.cast<adobe::string_t>();
            }
        }
        return result.str();
    }

}

namespace adobe {

    display_text_t::display_text_t(const std::string& name,
                                   const std::string& alt_text,
                                   int characters,
                                   GG::Clr color,
                                   GG::Clr label_color) :
        name_m(name),
        alt_text_m(alt_text),
        characters_m(characters),
        color_m(color),
        label_color_m(label_color)
    {}

    void display_text_t::place(const place_data_t& place_data)
    { implementation::set_control_bounds(window_m, place_data); }

    void display_text_t::display(const model_type& value)
    {
        assert(window_m);
        window_m->SetText(field_text(name_m, value, label_color_m));
    }

    void display_text_t::measure(extents_t& result)
    {
        assert(window_m);

        boost::shared_ptr<GG::Font> font = implementation::DefaultFont();

        extents_t space_extents(metrics::measure_text(std::string(" "), font));
        extents_t label_extents(metrics::measure_text(name_m, font));
        extents_t characters_extents(metrics::measure_text(std::string(characters_m, '0'), font));

        // set up default settings (baseline, etc.)
        result = space_extents;

        // set the width to the label width (if any)
        result.width() = label_extents.width();

        // add a guide for the label
        result.horizontal().guide_set_m.push_back(label_extents.width());

        // if there's a label, add space for a space
        if (label_extents.width() != 0)
            result.width() += space_extents.width();

        // append the character extents (if any)
        result.width() += characters_extents.width();

        // if there are character extents, add space for a space
        if (characters_extents.width() != 0)
            result.width() += space_extents.width();

        assert(result.horizontal().length_m);
    }

    void display_text_t::measure_vertical(extents_t& calculated_horizontal, const place_data_t& placed_horizontal)
    {
        assert(window_m);

        extents_t::slice_t& vert = calculated_horizontal.vertical();
        vert.length_m = Value(implementation::DefaultFont()->Lineskip());
        vert.guide_set_m.push_back(Value(implementation::DefaultFont()->Ascent()));
    }

    template <>
    platform_display_type insert<display_text_t>(display_t& display,
                                                 platform_display_type& parent,
                                                 display_text_t& element)
    {
        element.window_m =
            implementation::Factory().NewTextControl(GG::X0, GG::Y0, GG::X1, GG::Y1,
                                                     element.name_m, implementation::DefaultFont(),
                                                     element.color_m, GG::FORMAT_NONE, GG::INTERACTIVE);

        if (!element.alt_text_m.empty())
            implementation::set_control_alt_text(element.window_m, element.alt_text_m);

        return display.insert(parent, get_display(element));
    }

}
<commit_msg></rbga> -> </rgba><commit_after>/*
    Copyright 2005-2007 Adobe Systems Incorporated
    Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
    or a copy at http://stlab.adobe.com/licenses.html)
*/

#include "platform_display_text.hpp"

#include <GG/adobe/string.hpp>
#include <GG/adobe/future/widgets/headers/display.hpp>
#include <GG/adobe/future/widgets/headers/widget_utils.hpp>
#include <GG/adobe/future/widgets/headers/platform_metrics.hpp>

#include <GG/TextControl.h>
#include <GG/GUI.h>
#include <GG/StyleFactory.h>

#include <string>
#include <cassert>
#include <sstream>

namespace {

    std::string field_text(const std::string& label, adobe::any_regular_t value, GG::Clr label_color)
    {
        std::stringstream result;
        if (!label.empty())
            result << GG::RgbaTag(label_color) << label << "</rgba> ";
        if (value != adobe::any_regular_t()) {
            adobe::type_info_t type(value.type_info());
            if (type == adobe::type_info<double>() ||
                type == adobe::type_info<bool>() ||
                type == adobe::type_info<adobe::name_t>()) {
                result << value;
            } else {
                result << value.cast<adobe::string_t>();
            }
        }
        return result.str();
    }

}

namespace adobe {

    display_text_t::display_text_t(const std::string& name,
                                   const std::string& alt_text,
                                   int characters,
                                   GG::Clr color,
                                   GG::Clr label_color) :
        name_m(name),
        alt_text_m(alt_text),
        characters_m(characters),
        color_m(color),
        label_color_m(label_color)
    {}

    void display_text_t::place(const place_data_t& place_data)
    { implementation::set_control_bounds(window_m, place_data); }

    void display_text_t::display(const model_type& value)
    {
        assert(window_m);
        window_m->SetText(field_text(name_m, value, label_color_m));
    }

    void display_text_t::measure(extents_t& result)
    {
        assert(window_m);

        boost::shared_ptr<GG::Font> font = implementation::DefaultFont();

        extents_t space_extents(metrics::measure_text(std::string(" "), font));
        extents_t label_extents(metrics::measure_text(name_m, font));
        extents_t characters_extents(metrics::measure_text(std::string(characters_m, '0'), font));

        // set up default settings (baseline, etc.)
        result = space_extents;

        // set the width to the label width (if any)
        result.width() = label_extents.width();

        // add a guide for the label
        result.horizontal().guide_set_m.push_back(label_extents.width());

        // if there's a label, add space for a space
        if (label_extents.width() != 0)
            result.width() += space_extents.width();

        // append the character extents (if any)
        result.width() += characters_extents.width();

        // if there are character extents, add space for a space
        if (characters_extents.width() != 0)
            result.width() += space_extents.width();

        assert(result.horizontal().length_m);
    }

    void display_text_t::measure_vertical(extents_t& calculated_horizontal, const place_data_t& placed_horizontal)
    {
        assert(window_m);

        extents_t::slice_t& vert = calculated_horizontal.vertical();
        vert.length_m = Value(implementation::DefaultFont()->Lineskip());
        vert.guide_set_m.push_back(Value(implementation::DefaultFont()->Ascent()));
    }

    template <>
    platform_display_type insert<display_text_t>(display_t& display,
                                                 platform_display_type& parent,
                                                 display_text_t& element)
    {
        element.window_m =
            implementation::Factory().NewTextControl(GG::X0, GG::Y0, GG::X1, GG::Y1,
                                                     element.name_m, implementation::DefaultFont(),
                                                     element.color_m, GG::FORMAT_NONE, GG::INTERACTIVE);

        if (!element.alt_text_m.empty())
            implementation::set_control_alt_text(element.window_m, element.alt_text_m);

        return display.insert(parent, get_display(element));
    }

}
<|endoftext|>


<commit_before>/***************************************************/
/*! \class Stk
    \brief STK base class

    Nearly all STK classes inherit from this class.
    The global sample rate can be queried and
    modified via Stk.  In addition, this class
    provides error handling and byte-swapping
    functions.

    The Synthesis ToolKit in C++ (STK) is a set of open source audio
    signal processing and algorithmic synthesis classes written in the
    C++ programming language. STK was designed to facilitate rapid
    development of music synthesis and audio processing software, with
    an emphasis on cross-platform functionality, realtime control,
    ease of use, and educational example code.  STK currently runs
    with realtime support (audio and MIDI) on Linux, Macintosh OS X,
    and Windows computer platforms. Generic, non-realtime support has
    been tested under NeXTStep, Sun, and other platforms and should
    work with any standard C++ compiler.

    STK WWW site: http://ccrma.stanford.edu/software/stk/

    The Synthesis ToolKit in C++ (STK)
    Copyright (c) 1995--2016 Perry R. Cook and Gary P. Scavone

    Permission is hereby granted, free of charge, to any person
    obtaining a copy of this software and associated documentation files
    (the "Software"), to deal in the Software without restriction,
    including without limitation the rights to use, copy, modify, merge,
    publish, distribute, sublicense, and/or sell copies of the Software,
    and to permit persons to whom the Software is furnished to do so,
    subject to the following conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    Any person wishing to distribute modifications to the Software is
    asked to send the modifications to the original developer so that
    they can be incorporated into the canonical version.  This is,
    however, not a binding provision of this license.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
    ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
    CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
    WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/***************************************************/

#include "Stk.h"
#include <stdlib.h>

namespace stk {

StkFloat Stk::srate_ = (StkFloat)SRATE;
std::string Stk::rawwavepath_ = RAWWAVE_PATH;
const Stk::StkFormat Stk::STK_SINT8 = 0x1;
const Stk::StkFormat Stk::STK_SINT16 = 0x2;
const Stk::StkFormat Stk::STK_SINT24 = 0x4;
const Stk::StkFormat Stk::STK_SINT32 = 0x8;
const Stk::StkFormat Stk::STK_FLOAT32 = 0x10;
const Stk::StkFormat Stk::STK_FLOAT64 = 0x20;
bool Stk::showWarnings_ = true;
bool Stk::printErrors_ = true;
std::vector<Stk *> Stk::alertList_;
std::ostringstream Stk::oStream_;

Stk::Stk(void) : ignoreSampleRateChange_(false) {}

Stk::~Stk() {}

void Stk::setSampleRate(StkFloat rate) {
  if (rate > 0.0 && rate != srate_) {
    StkFloat oldRate = srate_;
    srate_ = rate;

    for (unsigned int i = 0; i < alertList_.size(); i++)
      alertList_[i]->sampleRateChanged(srate_, oldRate);
  }
}

void Stk::sampleRateChanged(StkFloat /*newRate*/, StkFloat /*oldRate*/) {
  // This function should be reimplemented in classes that need to
  // make internal variable adjustments in response to a global sample
  // rate change.
}

void Stk::addSampleRateAlert(Stk *ptr) {
  for (unsigned int i = 0; i < alertList_.size(); i++)
    if (alertList_[i] == ptr)
      return;

  alertList_.push_back(ptr);
}

void Stk::removeSampleRateAlert(Stk *ptr) {
  for (unsigned int i = 0; i < alertList_.size(); i++) {
    if (alertList_[i] == ptr) {
      alertList_.erase(alertList_.begin() + i);
      return;
    }
  }
}

void Stk::setRawwavePath(std::string path) {
  if (!path.empty())
    rawwavepath_ = path;

  // Make sure the path includes a "/"
  if (rawwavepath_[rawwavepath_.length() - 1] != '/')
    rawwavepath_ += "/";
}

void Stk::swap16(unsigned char *ptr) {
  unsigned char val;

  // Swap 1st and 2nd bytes
  val = *(ptr);
  *(ptr) = *(ptr + 1);
  *(ptr + 1) = val;
}

void Stk::swap32(unsigned char *ptr) {
  unsigned char val;

  // Swap 1st and 4th bytes
  val = *(ptr);
  *(ptr) = *(ptr + 3);
  *(ptr + 3) = val;

  // Swap 2nd and 3rd bytes
  ptr += 1;
  val = *(ptr);
  *(ptr) = *(ptr + 1);
  *(ptr + 1) = val;
}

void Stk::swap64(unsigned char *ptr) {
  unsigned char val;

  // Swap 1st and 8th bytes
  val = *(ptr);
  *(ptr) = *(ptr + 7);
  *(ptr + 7) = val;

  // Swap 2nd and 7th bytes
  ptr += 1;
  val = *(ptr);
  *(ptr) = *(ptr + 5);
  *(ptr + 5) = val;

  // Swap 3rd and 6th bytes
  ptr += 1;
  val = *(ptr);
  *(ptr) = *(ptr + 3);
  *(ptr + 3) = val;

  // Swap 4th and 5th bytes
  ptr += 1;
  val = *(ptr);
  *(ptr) = *(ptr + 1);
  *(ptr + 1) = val;
}

#if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))
#include <unistd.h>
#elif defined(__OS_WINDOWS__)
#include <windows.h>
#endif

void Stk::sleep(unsigned long milliseconds) {
#if defined(__OS_WINDOWS__)
  Sleep((DWORD)milliseconds);
#elif (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))
  usleep((unsigned long)(milliseconds * 1000.0));
#endif
}

void Stk::handleError(StkError::Type type) const {
  handleError(oStream_.str(), type);
  oStream_.str(std::string()); // reset the ostringstream buffer
}

void Stk::handleError(const char *message, StkError::Type type) {
  std::string msg(message);
  handleError(msg, type);
}

void Stk::handleError(std::string message, StkError::Type type) {
  if (type == StkError::WARNING || type == StkError::STATUS) {
    if (!showWarnings_)
      return;
    std::cerr << '\n' << message << '\n' << std::endl;
  } else if (type == StkError::DEBUG_PRINT) {
#if defined(_STK_DEBUG_)
    std::cerr << '\n' << message << '\n' << std::endl;
#endif
  } else {
    if (printErrors_) {
      // Print error message before throwing.
      std::cerr << '\n' << message << '\n' << std::endl;
    }
    throw StkError(message, type);
  }
}

//
// StkFrames definitions
//

StkFrames::StkFrames(unsigned int nFrames, unsigned int nChannels)
    : data_(0), nFrames_(nFrames), nChannels_(nChannels) {
  size_ = nFrames_ * nChannels_;
  bufferSize_ = size_;

  if (size_ > 0) {
    data_ = (StkFloat *)calloc(size_, sizeof(StkFloat));
#if defined(_STK_DEBUG_)
    if (data_ == NULL) {
      std::string error = "StkFrames: memory allocation error in constructor!";
      Stk::handleError(error, StkError::MEMORY_ALLOCATION);
    }
#endif
  }

  dataRate_ = Stk::sampleRate();
}

StkFrames::StkFrames(const StkFloat &value, unsigned int nFrames,
                      unsigned int nChannels)
    : data_(0), nFrames_(nFrames), nChannels_(nChannels) {
  size_ = nFrames_ * nChannels_;
  bufferSize_ = size_;
  if (size_ > 0) {
    data_ = (StkFloat *)malloc(size_ * sizeof(StkFloat));
#if defined(_STK_DEBUG_)
    if (data_ == NULL) {
      std::string error = "StkFrames: memory allocation error in constructor!";
      Stk::handleError(error, StkError::MEMORY_ALLOCATION);
    }
#endif
    for (long i = 0; i < (long)size_; i++)
      data_[i] = value;
  }

  dataRate_ = Stk::sampleRate();
}

StkFrames::~StkFrames() {
  if (data_)
    free(data_);
}

StkFrames::StkFrames(const StkFrames &f) : data_(0), size_(0), bufferSize_(0) {
  resize(f.frames(), f.channels());
  dataRate_ = Stk::sampleRate();
  for (unsigned int i = 0; i < size_; i++)
    data_[i] = f[i];
}

StkFrames &StkFrames::operator=(const StkFrames &f) {
  if (data_)
    free(data_);
  data_ = 0;
  size_ = 0;
  bufferSize_ = 0;
  resize(f.frames(), f.channels());
  dataRate_ = Stk::sampleRate();
  for (unsigned int i = 0; i < size_; i++)
    data_[i] = f[i];
  return *this;
}

void StkFrames::resize(size_t nFrames, unsigned int nChannels) {
  nFrames_ = nFrames;
  nChannels_ = nChannels;

  size_ = nFrames_ * nChannels_;
  if (size_ > bufferSize_) {
    if (data_)
      free(data_);
    data_ = (StkFloat *)malloc(size_ * sizeof(StkFloat));
#if defined(_STK_DEBUG_)
    if (data_ == NULL) {
      std::string error = "StkFrames::resize: memory allocation error!";
      Stk::handleError(error, StkError::MEMORY_ALLOCATION);
    }
#endif
    bufferSize_ = size_;
  }
}

void StkFrames::resize(size_t nFrames, unsigned int nChannels,
                        StkFloat value) {
  this->resize(nFrames, nChannels);

  for (size_t i = 0; i < size_; i++)
    data_[i] = value;
}

StkFrames &StkFrames::getChannel(unsigned int sourceChannel,
                                 StkFrames &destinationFrames,
                                 unsigned int destinationChannel) const {
#if defined(_STK_DEBUG_)
  if (sourceChannel > channels() - 1) {
    std::ostringstream error;
    error << "StkFrames::getChannel invalid sourceChannel (" << sourceChannel
          << ")";
    Stk::handleError(error.str(), StkError::FUNCTION_ARGUMENT);
  }
  if (destinationChannel > destinationFrames.channels() - 1) {
    std::ostringstream error;
    error << "StkFrames::getChannel invalid destinationChannel ("
          << destinationChannel << ")";
    Stk::handleError(error.str(), StkError::FUNCTION_ARGUMENT);
  }
  if (destinationFrames.frames() < frames()) {
    std::ostringstream error;
    error << "StkFrames::getChannel destination.frames() < frames()";
    Stk::handleError(error.str(), StkError::MEMORY_ACCESS);
  }
#endif
  int sourceHop = nChannels_;
  int destinationHop = destinationFrames.nChannels_;
  for (int i = sourceChannel, j = destinationChannel; i < nFrames_ * nChannels_;
       i += sourceHop, j += destinationHop) {
    destinationFrames[j] = data_[i];
  }
  return destinationFrames;
}

void StkFrames::setChannel(unsigned int destinationChannel,
                           const stk::StkFrames &sourceFrames,
                           unsigned int sourceChannel) {
#if defined(_STK_DEBUG_)
  if (sourceChannel > sourceFrames.channels() - 1) {
    std::ostringstream error;
    error << "StkFrames::setChannel invalid sourceChannel (" << sourceChannel
          << ")";
    Stk::handleError(error.str(), StkError::FUNCTION_ARGUMENT);
  }
  if (destinationChannel > channels() - 1) {
    std::ostringstream error;
    error << "StkFrames::setChannel invalid channel (" << destinationChannel
          << ")";
    Stk::handleError(error.str(), StkError::FUNCTION_ARGUMENT);
  }
  if (sourceFrames.frames() != frames()) {
    std::ostringstream error;
    error << "StkFrames::setChannel sourceFrames.frames() != frames()";
    Stk::handleError(error.str(), StkError::MEMORY_ACCESS);
  }
#endif
  unsigned int sourceHop = sourceFrames.nChannels_;
  unsigned int destinationHop = nChannels_;
  for (int i = destinationChannel, j = sourceChannel; i < nFrames_ * nChannels_;
       i += destinationHop, j += sourceHop) {
    data_[i] = sourceFrames[j];
  }
}

StkFloat StkFrames::interpolate(StkFloat frame, unsigned int channel) const {
#if defined(_STK_DEBUG_)
  if (frame < 0.0 || frame > (StkFloat)(nFrames_ - 1) ||
      channel >= nChannels_) {
    std::ostringstream error;
    error << "StkFrames::interpolate: invalid frame (" << frame
          << ") or channel (" << channel << ") value!";
    Stk::handleError(error.str(), StkError::MEMORY_ACCESS);
  }
#endif

  size_t iIndex = (size_t)frame;                     // integer part of index
  StkFloat output, alpha = frame - (StkFloat)iIndex; // fractional part of index

  iIndex = iIndex * nChannels_ + channel;
  output = data_[iIndex];
  if (alpha > 0.0)
    output += (alpha * (data_[iIndex + nChannels_] - output));

  return output;
}

}
<commit_msg>Fix static analyzer issue.<commit_after>/***************************************************/
/*! \class Stk
    \brief STK base class

    Nearly all STK classes inherit from this class.
    The global sample rate can be queried and
    modified via Stk.  In addition, this class
    provides error handling and byte-swapping
    functions.

    The Synthesis ToolKit in C++ (STK) is a set of open source audio
    signal processing and algorithmic synthesis classes written in the
    C++ programming language. STK was designed to facilitate rapid
    development of music synthesis and audio processing software, with
    an emphasis on cross-platform functionality, realtime control,
    ease of use, and educational example code.  STK currently runs
    with realtime support (audio and MIDI) on Linux, Macintosh OS X,
    and Windows computer platforms. Generic, non-realtime support has
    been tested under NeXTStep, Sun, and other platforms and should
    work with any standard C++ compiler.

    STK WWW site: http://ccrma.stanford.edu/software/stk/

    The Synthesis ToolKit in C++ (STK)
    Copyright (c) 1995--2016 Perry R. Cook and Gary P. Scavone

    Permission is hereby granted, free of charge, to any person
    obtaining a copy of this software and associated documentation files
    (the "Software"), to deal in the Software without restriction,
    including without limitation the rights to use, copy, modify, merge,
    publish, distribute, sublicense, and/or sell copies of the Software,
    and to permit persons to whom the Software is furnished to do so,
    subject to the following conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    Any person wishing to distribute modifications to the Software is
    asked to send the modifications to the original developer so that
    they can be incorporated into the canonical version.  This is,
    however, not a binding provision of this license.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
    ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
    CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
    WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/***************************************************/

#include "Stk.h"
#include <stdlib.h>

namespace stk {

StkFloat Stk::srate_ = (StkFloat)SRATE;
std::string Stk::rawwavepath_ = RAWWAVE_PATH;
const Stk::StkFormat Stk::STK_SINT8 = 0x1;
const Stk::StkFormat Stk::STK_SINT16 = 0x2;
const Stk::StkFormat Stk::STK_SINT24 = 0x4;
const Stk::StkFormat Stk::STK_SINT32 = 0x8;
const Stk::StkFormat Stk::STK_FLOAT32 = 0x10;
const Stk::StkFormat Stk::STK_FLOAT64 = 0x20;
bool Stk::showWarnings_ = true;
bool Stk::printErrors_ = true;
std::vector<Stk *> Stk::alertList_;
std::ostringstream Stk::oStream_;

Stk::Stk(void) : ignoreSampleRateChange_(false) {}

Stk::~Stk() {}

void Stk::setSampleRate(StkFloat rate) {
  if (rate > 0.0 && rate != srate_) {
    StkFloat oldRate = srate_;
    srate_ = rate;

    for (unsigned int i = 0; i < alertList_.size(); i++)
      alertList_[i]->sampleRateChanged(srate_, oldRate);
  }
}

void Stk::sampleRateChanged(StkFloat /*newRate*/, StkFloat /*oldRate*/) {
  // This function should be reimplemented in classes that need to
  // make internal variable adjustments in response to a global sample
  // rate change.
}

void Stk::addSampleRateAlert(Stk *ptr) {
  for (unsigned int i = 0; i < alertList_.size(); i++)
    if (alertList_[i] == ptr)
      return;

  alertList_.push_back(ptr);
}

void Stk::removeSampleRateAlert(Stk *ptr) {
  for (unsigned int i = 0; i < alertList_.size(); i++) {
    if (alertList_[i] == ptr) {
      alertList_.erase(alertList_.begin() + i);
      return;
    }
  }
}

void Stk::setRawwavePath(std::string path) {
  if (!path.empty())
    rawwavepath_ = path;

  // Make sure the path includes a "/"
  if (rawwavepath_[rawwavepath_.length() - 1] != '/')
    rawwavepath_ += "/";
}

void Stk::swap16(unsigned char *ptr) {
  unsigned char val;

  // Swap 1st and 2nd bytes
  val = *(ptr);
  *(ptr) = *(ptr + 1);
  *(ptr + 1) = val;
}

void Stk::swap32(unsigned char *ptr) {
  unsigned char val;

  // Swap 1st and 4th bytes
  val = *(ptr);
  *(ptr) = *(ptr + 3);
  *(ptr + 3) = val;

  // Swap 2nd and 3rd bytes
  ptr += 1;
  val = *(ptr);
  *(ptr) = *(ptr + 1);
  *(ptr + 1) = val;
}

void Stk::swap64(unsigned char *ptr) {
  unsigned char val;

  // Swap 1st and 8th bytes
  val = *(ptr);
  *(ptr) = *(ptr + 7);
  *(ptr + 7) = val;

  // Swap 2nd and 7th bytes
  ptr += 1;
  val = *(ptr);
  *(ptr) = *(ptr + 5);
  *(ptr + 5) = val;

  // Swap 3rd and 6th bytes
  ptr += 1;
  val = *(ptr);
  *(ptr) = *(ptr + 3);
  *(ptr + 3) = val;

  // Swap 4th and 5th bytes
  ptr += 1;
  val = *(ptr);
  *(ptr) = *(ptr + 1);
  *(ptr + 1) = val;
}

#if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))
#include <unistd.h>
#elif defined(__OS_WINDOWS__)
#include <windows.h>
#endif

void Stk::sleep(unsigned long milliseconds) {
#if defined(__OS_WINDOWS__)
  Sleep((DWORD)milliseconds);
#elif (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))
  usleep((unsigned long)(milliseconds * 1000.0));
#endif
}

void Stk::handleError(StkError::Type type) const {
  handleError(oStream_.str(), type);
  oStream_.str(std::string()); // reset the ostringstream buffer
}

void Stk::handleError(const char *message, StkError::Type type) {
  std::string msg(message);
  handleError(msg, type);
}

void Stk::handleError(std::string message, StkError::Type type) {
  if (type == StkError::WARNING || type == StkError::STATUS) {
    if (!showWarnings_)
      return;
    std::cerr << '\n' << message << '\n' << std::endl;
  } else if (type == StkError::DEBUG_PRINT) {
#if defined(_STK_DEBUG_)
    std::cerr << '\n' << message << '\n' << std::endl;
#endif
  } else {
    if (printErrors_) {
      // Print error message before throwing.
      std::cerr << '\n' << message << '\n' << std::endl;
    }
    throw StkError(message, type);
  }
}

//
// StkFrames definitions
//

StkFrames::StkFrames(unsigned int nFrames, unsigned int nChannels)
    : data_(0), nFrames_(nFrames), nChannels_(nChannels) {
  size_ = nFrames_ * nChannels_;
  bufferSize_ = size_;

  if (size_ > 0) {
    data_ = (StkFloat *)calloc(size_, sizeof(StkFloat));
#if defined(_STK_DEBUG_)
    if (data_ == NULL) {
      std::string error = "StkFrames: memory allocation error in constructor!";
      Stk::handleError(error, StkError::MEMORY_ALLOCATION);
    }
#endif
  }

  dataRate_ = Stk::sampleRate();
}

StkFrames::StkFrames(const StkFloat &value, unsigned int nFrames,
                      unsigned int nChannels)
    : data_(0), nFrames_(nFrames), nChannels_(nChannels) {
  size_ = nFrames_ * nChannels_;
  bufferSize_ = size_;
  if (size_ > 0) {
    data_ = (StkFloat *)malloc(size_ * sizeof(StkFloat));
#if defined(_STK_DEBUG_)
    if (data_ == NULL) {
      std::string error = "StkFrames: memory allocation error in constructor!";
      Stk::handleError(error, StkError::MEMORY_ALLOCATION);
    }
#endif
    for (long i = 0; i < (long)size_; i++)
      data_[i] = value;
  }

  dataRate_ = Stk::sampleRate();
}

StkFrames::~StkFrames() {
  if (data_)
    free(data_);
}

StkFrames::StkFrames(const StkFrames &f) : data_(0), size_(0), bufferSize_(0) {
  resize(f.frames(), f.channels());
  dataRate_ = Stk::sampleRate();
  for (unsigned int i = 0; i < size_; i++)
    data_[i] = f[i];
}

StkFrames &StkFrames::operator=(const StkFrames &f) {
  if(&f == this) { return *this; }
  if (data_)
    free(data_);
  data_ = 0;
  size_ = 0;
  bufferSize_ = 0;
  resize(f.frames(), f.channels());
  dataRate_ = Stk::sampleRate();
  for (unsigned int i = 0; i < size_; i++)
    data_[i] = f[i];
  return *this;
}

void StkFrames::resize(size_t nFrames, unsigned int nChannels) {
  nFrames_ = nFrames;
  nChannels_ = nChannels;

  size_ = nFrames_ * nChannels_;
  if (size_ > bufferSize_) {
    if (data_)
      free(data_);
    data_ = (StkFloat *)malloc(size_ * sizeof(StkFloat));
#if defined(_STK_DEBUG_)
    if (data_ == NULL) {
      std::string error = "StkFrames::resize: memory allocation error!";
      Stk::handleError(error, StkError::MEMORY_ALLOCATION);
    }
#endif
    bufferSize_ = size_;
  }
}

void StkFrames::resize(size_t nFrames, unsigned int nChannels,
                        StkFloat value) {
  this->resize(nFrames, nChannels);

  for (size_t i = 0; i < size_; i++)
    data_[i] = value;
}

StkFrames &StkFrames::getChannel(unsigned int sourceChannel,
                                 StkFrames &destinationFrames,
                                 unsigned int destinationChannel) const {
#if defined(_STK_DEBUG_)
  if (sourceChannel > channels() - 1) {
    std::ostringstream error;
    error << "StkFrames::getChannel invalid sourceChannel (" << sourceChannel
          << ")";
    Stk::handleError(error.str(), StkError::FUNCTION_ARGUMENT);
  }
  if (destinationChannel > destinationFrames.channels() - 1) {
    std::ostringstream error;
    error << "StkFrames::getChannel invalid destinationChannel ("
          << destinationChannel << ")";
    Stk::handleError(error.str(), StkError::FUNCTION_ARGUMENT);
  }
  if (destinationFrames.frames() < frames()) {
    std::ostringstream error;
    error << "StkFrames::getChannel destination.frames() < frames()";
    Stk::handleError(error.str(), StkError::MEMORY_ACCESS);
  }
#endif
  int sourceHop = nChannels_;
  int destinationHop = destinationFrames.nChannels_;
  for (int i = sourceChannel, j = destinationChannel; i < nFrames_ * nChannels_;
       i += sourceHop, j += destinationHop) {
    destinationFrames[j] = data_[i];
  }
  return destinationFrames;
}

void StkFrames::setChannel(unsigned int destinationChannel,
                           const stk::StkFrames &sourceFrames,
                           unsigned int sourceChannel) {
#if defined(_STK_DEBUG_)
  if (sourceChannel > sourceFrames.channels() - 1) {
    std::ostringstream error;
    error << "StkFrames::setChannel invalid sourceChannel (" << sourceChannel
          << ")";
    Stk::handleError(error.str(), StkError::FUNCTION_ARGUMENT);
  }
  if (destinationChannel > channels() - 1) {
    std::ostringstream error;
    error << "StkFrames::setChannel invalid channel (" << destinationChannel
          << ")";
    Stk::handleError(error.str(), StkError::FUNCTION_ARGUMENT);
  }
  if (sourceFrames.frames() != frames()) {
    std::ostringstream error;
    error << "StkFrames::setChannel sourceFrames.frames() != frames()";
    Stk::handleError(error.str(), StkError::MEMORY_ACCESS);
  }
#endif
  unsigned int sourceHop = sourceFrames.nChannels_;
  unsigned int destinationHop = nChannels_;
  for (int i = destinationChannel, j = sourceChannel; i < nFrames_ * nChannels_;
       i += destinationHop, j += sourceHop) {
    data_[i] = sourceFrames[j];
  }
}

StkFloat StkFrames::interpolate(StkFloat frame, unsigned int channel) const {
#if defined(_STK_DEBUG_)
  if (frame < 0.0 || frame > (StkFloat)(nFrames_ - 1) ||
      channel >= nChannels_) {
    std::ostringstream error;
    error << "StkFrames::interpolate: invalid frame (" << frame
          << ") or channel (" << channel << ") value!";
    Stk::handleError(error.str(), StkError::MEMORY_ACCESS);
  }
#endif

  size_t iIndex = (size_t)frame;                     // integer part of index
  StkFloat output, alpha = frame - (StkFloat)iIndex; // fractional part of index

  iIndex = iIndex * nChannels_ + channel;
  output = data_[iIndex];
  if (alpha > 0.0)
    output += (alpha * (data_[iIndex + nChannels_] - output));

  return output;
}

}
<|endoftext|>


<commit_before>#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

#include <popt.h>

#include "defs.hh"
#include "StringSet.hh"
#include "FactorEncoder.hh"
#include "Unigrams.hh"

using namespace std;

void assert_single_chars(map<string, flt_type> &vocab,
                         const map<string, flt_type> &chars,
                         flt_type val)
{
    for (auto it = chars.cbegin(); it != chars.cend(); ++it)
        if (vocab.find(it->first) == vocab.end())
            vocab[it->first] = val;
}

int main(int argc, char* argv[]) {

    float cutoff_value = 0.0;
    int n_candidates_per_iter = 5000;
    int max_removals_per_iter = 5000;
    int min_removals_per_iter = 0;
    float threshold = -25.0;
    float threshold_decrease = 25.0;
    int target_vocab_size = 50000;
    bool enable_forward_backward = false;
    bool random_candidates = false;
    flt_type one_char_min_lp = -25.0;
    string vocab_fname;
    string wordlist_fname;

    // Popt documentation:
    // http://linux.die.net/man/3/popt
    // http://privatemisc.blogspot.fi/2012/12/popt-basic-example.html
    poptContext pc;
    struct poptOption po[] = {
        {"cutoff", 'u', POPT_ARG_FLOAT, &cutoff_value, 11001, NULL, "Cutoff value for each iteration"},
        {"candidates", 'c', POPT_ARG_INT, &n_candidates_per_iter, 11002, NULL, "Number of candidate subwords to try to remove per iteration"},
        {"max_removals", 'a', POPT_ARG_INT, &max_removals_per_iter, 11003, NULL, "Maximum number of removals per iteration"},
        {"min_removals", 'i', POPT_ARG_INT, &min_removals_per_iter, 11004, NULL, "Minimum number of removals per iteration (stopping criterion)"},
        {"threshold", 't', POPT_ARG_FLOAT, &threshold, 11005, NULL, "Likelihood threshold for removals"},
        {"threshold_decrease", 'd', POPT_ARG_FLOAT, &threshold_decrease, 11006, NULL, "Threshold decrease between iterations"},
        {"vocab_size", 'g', POPT_ARG_INT, &target_vocab_size, 11007, NULL, "Target vocabulary size (stopping criterion)"},
        {"forward_backward", 'f', POPT_ARG_NONE, &enable_forward_backward, 11007, "Use Forward-backward segmentation instead of Viterbi", NULL},
        {"random_init", 'r', POPT_ARG_NONE, &random_candidates, 11007, "Select removal candidates by random", NULL},
        POPT_AUTOHELP
        {NULL}
    };

    pc = poptGetContext(NULL, argc, (const char **)argv, po, 0);
    poptSetOtherOptionHelp(pc, "[INITIAL VOCABULARY] [WORDLIST]");

    int val;
    while ((val = poptGetNextOpt(pc)) >= 0)
        continue;

    // poptGetNextOpt returns -1 when the final argument has been parsed
    // otherwise an error occured
    if (val != -1) {
        switch (val) {
        case POPT_ERROR_NOARG:
            cerr << "Argument missing for an option" << endl;
            exit(1);
        case POPT_ERROR_BADOPT:
            cerr << "Option's argument could not be parsed" << endl;
            exit(1);
        case POPT_ERROR_BADNUMBER:
        case POPT_ERROR_OVERFLOW:
            cerr << "Option could not be converted to number" << endl;
            exit(1);
        default:
            cerr << "Unknown error in option processing" << endl;
            exit(1);
        }
    }

    // Handle ARG part of command line
    if (poptPeekArg(pc) != NULL)
        vocab_fname.assign((char*)poptGetArg(pc));
    else {
        cerr << "Initial vocabulary file not set" << endl;
        exit(1);
    }

    if (poptPeekArg(pc) != NULL)
        wordlist_fname.assign((char*)poptGetArg(pc));
    else {
        cerr << "Wordlist file not set" << endl;
        exit(1);
    }

    cerr << "parameters, initial vocabulary: " << vocab_fname << endl;
    cerr << "parameters, wordlist: " << wordlist_fname << endl;
    cerr << "parameters, cutoff: " << setprecision(15) << cutoff_value << endl;
    cerr << "parameters, candidates per iteration: " << n_candidates_per_iter << endl;
    cerr << "parameters, removals per iteration: " << max_removals_per_iter << endl;
    cerr << "parameters, threshold: " << threshold << endl;
    cerr << "parameters, threshold decrease per iteration: " << threshold_decrease << endl;
    cerr << "parameters, min removals per iteration: " << min_removals_per_iter << endl;
    cerr << "parameters, target vocab size: " << target_vocab_size << endl;
    cerr << "parameters, use forward-backward: " << enable_forward_backward << endl;
    cerr << "parameters, random candidates: " << random_candidates << endl;

    int maxlen, word_maxlen;
    map<string, flt_type> all_chars;
    map<string, flt_type> vocab;
    map<string, flt_type> freqs;
    map<string, flt_type> words;

    cerr << "Reading vocabulary " << vocab_fname << endl;
    int retval = Unigrams::read_vocab(vocab_fname, vocab, maxlen);
    if (retval < 0) {
        cerr << "something went wrong reading vocabulary" << endl;
        exit(0);
    }
    cerr << "\t" << "size: " << vocab.size() << endl;
    cerr << "\t" << "maximum string length: " << maxlen << endl;
    for (auto it = vocab.cbegin(); it != vocab.end(); ++it)
        if (it->first.length() == 1)
            all_chars[it->first] = 0.0;

    cerr << "Reading word list " << wordlist_fname << endl;
    retval = Unigrams::read_vocab(wordlist_fname, words, word_maxlen);
    if (retval < 0) {
        cerr << "something went wrong reading word list" << endl;
        exit(0);
    }
    cerr << "\t" << "wordlist size: " << words.size() << endl;
    cerr << "\t" << "maximum word length: " << word_maxlen << endl;

    Unigrams gg;
    if (enable_forward_backward)
        gg.set_segmentation_method(forward_backward);
    else
        gg.set_segmentation_method(viterbi);

    cerr << "Initial cutoff" << endl;
    gg.resegment_words(words, vocab, freqs);
    flt_type densum = gg.get_sum(freqs);
    flt_type cost = gg.get_cost(freqs, densum);
    cerr << "cost: " << cost << endl;

    gg.cutoff(freqs, (flt_type)cutoff_value);
    cerr << "\tcutoff: " << cutoff_value << "\t" << "vocabulary size: " << freqs.size() << endl;
    vocab = freqs;
    densum = gg.get_sum(vocab);
    gg.freqs_to_logprobs(vocab, densum);
    assert_single_chars(vocab, all_chars, one_char_min_lp);

    cerr << "Removing subwords one by one" << endl;
    int itern = 1;
    while (true) {

        cerr << "iteration " << itern << endl;

        cerr << "collecting candidate subwords for removal" << endl;
        set<string> diffs;
        if ((int)vocab.size()-n_candidates_per_iter < target_vocab_size) n_candidates_per_iter = (int)vocab.size()-target_vocab_size;
        if (random_candidates)
            gg.init_removal_candidates_by_random(n_candidates_per_iter, words, vocab, diffs);
        else
            gg.init_removal_candidates(n_candidates_per_iter, words, vocab, diffs);

        cerr << "ranking candidate subwords" << endl;
        vector<pair<string, flt_type> > removal_scores;
        gg.rank_removal_candidates(words, vocab, diffs, freqs, removal_scores);

        // Perform removals one by one if likelihood change above threshold
        flt_type curr_densum = gg.get_sum(freqs);
        flt_type curr_cost = gg.get_cost(freqs, curr_densum);
        map<string, map<string, flt_type> > backpointers;
        gg.get_backpointers(words, vocab, backpointers);

        cerr << "starting cost before removing subwords one by one: " << curr_cost << endl;

        unsigned int n_removals = 0;
        for (unsigned int i=0; i<removal_scores.size(); i++) {

            if (removal_scores[i].first.length() == 1) continue;
            // Score most probably went to zero already
            if (vocab.find(removal_scores[i].first) == vocab.end()) continue;

            cout << removal_scores[i].first << "\t" << "expected ll diff: " << removal_scores[i].second << endl;

            map<string, flt_type> freq_diffs;
            map<string, map<string, flt_type> > backpointers_to_remove;
            map<string, map<string, flt_type> > backpointers_to_add;
            StringSet stringset_vocab(vocab);
            gg.hypo_removal(stringset_vocab, removal_scores[i].first, backpointers,
                            backpointers_to_remove, backpointers_to_add, freq_diffs);

            flt_type hypo_densum = gg.get_sum(freqs, freq_diffs);
            flt_type hypo_cost = gg.get_cost(freqs, freq_diffs, hypo_densum);

            cout << removal_scores[i].first << "\t" << "change in likelihood: " << (hypo_cost-curr_cost);
            if ((hypo_cost-curr_cost) < threshold) {
                cout << " was below threshold " << threshold << endl;
                continue;
            }
            cout << " removed, was above threshold " << threshold << endl;

            gg.apply_freq_diffs(freqs, freq_diffs);
            freqs.erase(removal_scores[i].first);
            gg.apply_backpointer_changes(backpointers, backpointers_to_remove, backpointers_to_add);
            backpointers.erase(removal_scores[i].first);

            curr_densum = hypo_densum;
            curr_cost = hypo_cost;
            vocab = freqs;
            gg.freqs_to_logprobs(vocab, hypo_densum);
            assert_single_chars(vocab, all_chars, one_char_min_lp);

            n_removals++;

            if (vocab.size() % 5000 == 0) {
                ostringstream vocabfname;
                vocabfname << "iter" << itern << "_" << vocab.size() << ".vocab";
                Unigrams::write_vocab(vocabfname.str(), vocab);
            }

            if (n_removals >= max_removals_per_iter) break;
            if (vocab.size() <= target_vocab_size) break;
        }

        int n_cutoff = gg.cutoff(freqs, cutoff_value);
        flt_type co_densum = gg.get_sum(freqs);
        vocab = freqs;
        gg.freqs_to_logprobs(vocab, co_densum);
        assert_single_chars(vocab, all_chars, one_char_min_lp);
        gg.resegment_words(words, vocab, freqs);
        curr_densum = gg.get_sum(freqs);
        curr_cost = gg.get_cost(freqs, curr_densum);

        cerr << "subwords removed in this iteration: " << n_removals << endl;
        cerr << "subwords removed with cutoff this iteration: " << n_cutoff << endl;
        cerr << "current vocabulary size: " << vocab.size() << endl;
        cerr << "likelihood after the removals: " << curr_cost << endl;

        ostringstream vocabfname;
        vocabfname << "iter" << itern << ".vocab";
        Unigrams::write_vocab(vocabfname.str(), vocab);

        itern++;
        threshold -= threshold_decrease;

        if (n_removals < min_removals_per_iter) {
            cerr << "stopping by min_removals_per_iter." << endl;
            break;
        }

        if (vocab.size() <= target_vocab_size) {
            cerr << "stopping by min_vocab_size." << endl;
            break;
        }
    }

    exit(1);
}

<commit_msg>New version of G1G.<commit_after>#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

#include <popt.h>

#include "defs.hh"
#include "StringSet.hh"
#include "FactorEncoder.hh"
#include "Unigrams.hh"

using namespace std;

void assert_single_chars(map<string, flt_type> &vocab,
                         const map<string, flt_type> &chars,
                         flt_type val)
{
    for (auto it = chars.cbegin(); it != chars.cend(); ++it)
        if (vocab.find(it->first) == vocab.end())
            vocab[it->first] = val;
}

int main(int argc, char* argv[]) {

    float cutoff_value = 0.0;
    int n_candidates_per_iter = 5000;
    int max_removals_per_iter = 5000;
    int min_removals_per_iter = 0;
    int target_vocab_size = 50000;
    bool enable_forward_backward = false;
    flt_type one_char_min_lp = -25.0;
    string vocab_fname;
    string wordlist_fname;

    // Popt documentation:
    // http://linux.die.net/man/3/popt
    // http://privatemisc.blogspot.fi/2012/12/popt-basic-example.html
    poptContext pc;
    struct poptOption po[] = {
        {"cutoff", 'u', POPT_ARG_FLOAT, &cutoff_value, 11001, NULL, "Cutoff value for each iteration"},
        {"candidates", 'c', POPT_ARG_INT, &n_candidates_per_iter, 11002, NULL, "Number of candidate subwords to try to remove per iteration"},
        {"max_removals", 'a', POPT_ARG_INT, &max_removals_per_iter, 11003, NULL, "Maximum number of removals per iteration"},
        {"min_removals", 'i', POPT_ARG_INT, &min_removals_per_iter, 11004, NULL, "Minimum number of removals per iteration (stopping criterion)"},
        {"vocab_size", 'g', POPT_ARG_INT, &target_vocab_size, 11007, NULL, "Target vocabulary size (stopping criterion)"},
        {"forward_backward", 'f', POPT_ARG_NONE, &enable_forward_backward, 11007, "Use Forward-backward segmentation instead of Viterbi", NULL},
        POPT_AUTOHELP
        {NULL}
    };

    pc = poptGetContext(NULL, argc, (const char **)argv, po, 0);
    poptSetOtherOptionHelp(pc, "[INITIAL VOCABULARY] [WORDLIST]");

    int val;
    while ((val = poptGetNextOpt(pc)) >= 0)
        continue;

    // poptGetNextOpt returns -1 when the final argument has been parsed
    // otherwise an error occured
    if (val != -1) {
        switch (val) {
        case POPT_ERROR_NOARG:
            cerr << "Argument missing for an option" << endl;
            exit(1);
        case POPT_ERROR_BADOPT:
            cerr << "Option's argument could not be parsed" << endl;
            exit(1);
        case POPT_ERROR_BADNUMBER:
        case POPT_ERROR_OVERFLOW:
            cerr << "Option could not be converted to number" << endl;
            exit(1);
        default:
            cerr << "Unknown error in option processing" << endl;
            exit(1);
        }
    }

    // Handle ARG part of command line
    if (poptPeekArg(pc) != NULL)
        vocab_fname.assign((char*)poptGetArg(pc));
    else {
        cerr << "Initial vocabulary file not set" << endl;
        exit(1);
    }

    if (poptPeekArg(pc) != NULL)
        wordlist_fname.assign((char*)poptGetArg(pc));
    else {
        cerr << "Wordlist file not set" << endl;
        exit(1);
    }

    cerr << "parameters, initial vocabulary: " << vocab_fname << endl;
    cerr << "parameters, wordlist: " << wordlist_fname << endl;
    cerr << "parameters, cutoff: " << setprecision(15) << cutoff_value << endl;
    cerr << "parameters, candidates per iteration: " << n_candidates_per_iter << endl;
    cerr << "parameters, removals per iteration: " << max_removals_per_iter << endl;
    cerr << "below 100k.." << endl;
    cerr << "parameters, removals per iteration: 500" << endl;
    cerr << "parameters, min removals per iteration: " << min_removals_per_iter << endl;
    cerr << "parameters, target vocab size: " << target_vocab_size << endl;
    cerr << "parameters, use forward-backward: " << enable_forward_backward << endl;

    int maxlen, word_maxlen;
    map<string, flt_type> all_chars;
    map<string, flt_type> vocab;
    map<string, flt_type> freqs;
    map<string, flt_type> words;

    cerr << "Reading vocabulary " << vocab_fname << endl;
    int retval = Unigrams::read_vocab(vocab_fname, vocab, maxlen);
    if (retval < 0) {
        cerr << "something went wrong reading vocabulary" << endl;
        exit(0);
    }
    cerr << "\t" << "size: " << vocab.size() << endl;
    cerr << "\t" << "maximum string length: " << maxlen << endl;
    for (auto it = vocab.cbegin(); it != vocab.end(); ++it)
        if (it->first.length() == 1)
            all_chars[it->first] = 0.0;

    cerr << "Reading word list " << wordlist_fname << endl;
    retval = Unigrams::read_vocab(wordlist_fname, words, word_maxlen);
    if (retval < 0) {
        cerr << "something went wrong reading word list" << endl;
        exit(0);
    }
    cerr << "\t" << "wordlist size: " << words.size() << endl;
    cerr << "\t" << "maximum word length: " << word_maxlen << endl;

    Unigrams gg;
    if (enable_forward_backward)
        gg.set_segmentation_method(forward_backward);
    else
        gg.set_segmentation_method(viterbi);

    cerr << "Initial cutoff" << endl;
    gg.resegment_words(words, vocab, freqs);
    flt_type densum = gg.get_sum(freqs);
    flt_type cost = gg.get_cost(freqs, densum);
    cerr << "cost: " << cost << endl;

    flt_type temp_cutoff = 5.0;
    while (true) {
        gg.cutoff(freqs, temp_cutoff);
        cerr << "\tcutoff: " << temp_cutoff << "\t" << "vocabulary size: " << freqs.size() << endl;
        vocab = freqs;
        densum = gg.get_sum(vocab);
        cost = gg.get_cost(vocab, densum);
        cerr << "cost: " << cost << endl;
        gg.freqs_to_logprobs(vocab, densum);
        assert_single_chars(vocab, all_chars, one_char_min_lp);
        temp_cutoff += 2.5;
        if (temp_cutoff > (flt_type)cutoff_value) break;
        gg.resegment_words(words, vocab, freqs);
    }

    cerr << "Removing subwords one by one" << endl;
    int itern = 1;
    while (true) {

        cerr << "iteration " << itern << endl;

        if (vocab.size() <= 100000) {
            max_removals_per_iter = 500;
        }

        cerr << "collecting candidate subwords for removal" << endl;
        set<string> candidates;
        if ((int)vocab.size()-n_candidates_per_iter < target_vocab_size) n_candidates_per_iter = (int)vocab.size()-target_vocab_size;
        gg.init_removal_candidates(n_candidates_per_iter, words, vocab, candidates);

        cerr << "ranking candidate subwords" << endl;
        vector<pair<string, flt_type> > removal_scores;
        gg.rank_removal_candidates(words, vocab, candidates, freqs, removal_scores);

        densum = gg.get_sum(freqs);
        cost = gg.get_cost(freqs, densum);
        cerr << "starting cost before removing subwords one by one: " << cost << endl;

        // Perform removals one by one
        unsigned int n_removals = 0;
        for (unsigned int i=0; i<removal_scores.size(); i++) {

            if (removal_scores[i].first.length() == 1) continue;
            // Score most probably went to zero already
            if (vocab.find(removal_scores[i].first) == vocab.end()) continue;

            freqs.erase(removal_scores[i].first);
            vocab = freqs;
            densum = gg.get_sum(freqs);
            gg.freqs_to_logprobs(vocab, densum);
            assert_single_chars(vocab, all_chars, one_char_min_lp);

            n_removals++;

            if (vocab.size() % 5000 == 0) {
                ostringstream vocabfname;
                vocabfname << "iter" << itern << "_" << vocab.size() << ".vocab";
                Unigrams::write_vocab(vocabfname.str(), vocab);
            }

            if (n_removals >= max_removals_per_iter) break;
            if (vocab.size() <= target_vocab_size) break;
        }

        int n_cutoff = gg.cutoff(freqs, cutoff_value);
        densum = gg.get_sum(freqs);
        vocab = freqs;
        gg.freqs_to_logprobs(vocab, densum);
        assert_single_chars(vocab, all_chars, one_char_min_lp);
        cost = gg.iterate(words, vocab, 2);
        assert_single_chars(vocab, all_chars, one_char_min_lp);

        cerr << "subwords removed in this iteration: " << n_removals << endl;
        cerr << "subwords removed with cutoff this iteration: " << n_cutoff << endl;
        cerr << "current vocabulary size: " << vocab.size() << endl;
        cerr << "likelihood after the removals: " << cost << endl;

        ostringstream vocabfname;
        vocabfname << "iter" << itern << ".vocab";
        Unigrams::write_vocab(vocabfname.str(), vocab);

        itern++;

        if (n_removals < min_removals_per_iter) {
            cerr << "stopping by min_removals_per_iter." << endl;
            break;
        }

        if (vocab.size() <= target_vocab_size) {
            cerr << "stopping by min_vocab_size." << endl;
            break;
        }
    }

    exit(1);
}

<|endoftext|>