<commit_before>/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: calendar_gregorian.hxx,v $
 *
 *  $Revision: 1.10 $
 *
 *  last change: $Author: rt $ $Date: 2005-09-07 16:48:54 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/
#ifndef _I18N_CALENDAR_GREGORIAN_HXX_
#define _I18N_CALENDAR_GREGORIAN_HXX_

#include "calendarImpl.hxx"
#include "nativenumbersupplier.hxx"
#include "unicode/calendar.h"

//  ----------------------------------------------------
//  class Calendar_gregorian
//  ----------------------------------------------------

namespace com { namespace sun { namespace star { namespace i18n {

struct Era {
    sal_Int32 year;
    sal_Int32 month;
    sal_Int32 day;
};

class Calendar_gregorian : public CalendarImpl
{
public:

    // Constructors
    Calendar_gregorian();
    Calendar_gregorian(Era *_eraArray);

    /**
    * Destructor
    */
    ~Calendar_gregorian();

    // Methods
    virtual void SAL_CALL loadCalendar(const rtl::OUString& uniqueID, const com::sun::star::lang::Locale& rLocale) throw(com::sun::star::uno::RuntimeException);
    virtual void SAL_CALL setDateTime(double nTimeInDays) throw(com::sun::star::uno::RuntimeException);
    virtual double SAL_CALL getDateTime() throw(com::sun::star::uno::RuntimeException);
    virtual void SAL_CALL setValue( sal_Int16 nFieldIndex, sal_Int16 nValue ) throw(com::sun::star::uno::RuntimeException);
    virtual sal_Int16 SAL_CALL getValue(sal_Int16 nFieldIndex) throw(com::sun::star::uno::RuntimeException);
    virtual void SAL_CALL addValue(sal_Int16 nFieldIndex, sal_Int32 nAmount) throw(com::sun::star::uno::RuntimeException);
    virtual sal_Bool SAL_CALL isValid() throw (com::sun::star::uno::RuntimeException);
    virtual Calendar SAL_CALL getLoadedCalendar() throw(com::sun::star::uno::RuntimeException);
    virtual rtl::OUString SAL_CALL getUniqueID() throw(com::sun::star::uno::RuntimeException);
    virtual sal_Int16 SAL_CALL getFirstDayOfWeek() throw(com::sun::star::uno::RuntimeException);
    virtual void SAL_CALL setFirstDayOfWeek(sal_Int16 nDay) throw(com::sun::star::uno::RuntimeException);
    virtual void SAL_CALL setMinimumNumberOfDaysForFirstWeek(sal_Int16 nDays) throw(com::sun::star::uno::RuntimeException);
    virtual sal_Int16 SAL_CALL getMinimumNumberOfDaysForFirstWeek() throw(com::sun::star::uno::RuntimeException);
    virtual sal_Int16 SAL_CALL getNumberOfMonthsInYear() throw(com::sun::star::uno::RuntimeException);
    virtual sal_Int16 SAL_CALL getNumberOfDaysInWeek() throw(com::sun::star::uno::RuntimeException);
    virtual com::sun::star::uno::Sequence < CalendarItem > SAL_CALL getMonths() throw(com::sun::star::uno::RuntimeException);
    virtual com::sun::star::uno::Sequence < CalendarItem > SAL_CALL getDays() throw(com::sun::star::uno::RuntimeException);
    virtual rtl::OUString SAL_CALL getDisplayName(sal_Int16 nCalendarDisplayIndex, sal_Int16 nIdx, sal_Int16 nNameType) throw(com::sun::star::uno::RuntimeException);

    // Methods in XExtendedCalendar
    virtual rtl::OUString SAL_CALL getDisplayString( sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode ) throw (com::sun::star::uno::RuntimeException);

    //XServiceInfo
    virtual rtl::OUString SAL_CALL getImplementationName() throw(com::sun::star::uno::RuntimeException);
    virtual sal_Bool SAL_CALL supportsService(const rtl::OUString& ServiceName) throw(com::sun::star::uno::RuntimeException);
    virtual com::sun::star::uno::Sequence < rtl::OUString > SAL_CALL getSupportedServiceNames() throw(com::sun::star::uno::RuntimeException);

protected:
    Era *eraArray;
    icu::Calendar *body;
    NativeNumberSupplier aNatNum;
    const sal_Char* cCalendar;
    com::sun::star::lang::Locale aLocale;
    sal_uInt32 fieldSet;
    sal_Int16 fieldValue[CalendarFieldIndex::FIELD_COUNT];
    sal_Int16 fieldSetValue[CalendarFieldIndex::FIELD_COUNT];
    virtual void SAL_CALL mapToGregorian() throw(com::sun::star::uno::RuntimeException);
    virtual void SAL_CALL mapFromGregorian() throw(com::sun::star::uno::RuntimeException);
    void SAL_CALL getValue() throw(com::sun::star::uno::RuntimeException);
private:
    // submit fieldValue array according to fieldSet, plus YMDhms if >=0
    void SAL_CALL submitValues( sal_Int32 nYear, sal_Int32 nMonth, sal_Int32 nDay, sal_Int32 nHour, sal_Int32 nMinute, sal_Int32 nSecond, sal_Int32 nMilliSecond) throw(com::sun::star::uno::RuntimeException);
    void SAL_CALL setValue() throw(com::sun::star::uno::RuntimeException);
    void SAL_CALL init(Era *_eraArray) throw(com::sun::star::uno::RuntimeException);
    Calendar aCalendar;
    sal_Int16 aStartOfWeek;
};

//  ----------------------------------------------------
//  class Calendar_hanja
//  ----------------------------------------------------
class Calendar_hanja : public Calendar_gregorian
{
public:
    // Constructors
    Calendar_hanja();
    virtual void SAL_CALL loadCalendar(const rtl::OUString& uniqueID, const com::sun::star::lang::Locale& rLocale) throw(com::sun::star::uno::RuntimeException);
    virtual rtl::OUString SAL_CALL getDisplayName(sal_Int16 nCalendarDisplayIndex, sal_Int16 nIdx, sal_Int16 nNameType) throw(com::sun::star::uno::RuntimeException);
};

//  ----------------------------------------------------
//  class Calendar_gengou
//  ----------------------------------------------------
class Calendar_gengou : public Calendar_gregorian
{
public:
    // Constructors
    Calendar_gengou();
};

//  ----------------------------------------------------
//  class Calendar_ROC
//  ----------------------------------------------------
class Calendar_ROC : public Calendar_gregorian
{
public:
    // Constructors
    Calendar_ROC();
};

//  ----------------------------------------------------
//  class Calendar_buddhist
//  ----------------------------------------------------
class Calendar_buddhist : public Calendar_gregorian
{
public:
    // Constructors
    Calendar_buddhist();

    // Methods in XExtendedCalendar
    virtual rtl::OUString SAL_CALL getDisplayString( sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode ) throw (com::sun::star::uno::RuntimeException);
};

} } } }

#endif
<commit_msg>INTEGRATION: CWS warnings01 (1.10.14); FILE MERGED 2006/03/08 13:19:07 nn 1.10.14.1: #i53898# warning-free code<commit_after>/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: calendar_gregorian.hxx,v $
 *
 *  $Revision: 1.11 $
 *
 *  last change: $Author: hr $ $Date: 2006-06-20 04:40:26 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/
#ifndef _I18N_CALENDAR_GREGORIAN_HXX_
#define _I18N_CALENDAR_GREGORIAN_HXX_

#include "calendarImpl.hxx"
#include "nativenumbersupplier.hxx"

// External unicode includes (from icu) cause warning C4668 on Windows.
// We want to minimize the patches to external headers, so the warnings are
// disabled here instead of in the header file itself.
#ifdef _MSC_VER
#pragma warning(push, 1)
#endif
#include "unicode/calendar.h"
#ifdef _MSC_VER
#pragma warning(pop)
#endif

//  ----------------------------------------------------
//  class Calendar_gregorian
//  ----------------------------------------------------

namespace com { namespace sun { namespace star { namespace i18n {

struct Era {
    sal_Int32 year;
    sal_Int32 month;
    sal_Int32 day;
};

class Calendar_gregorian : public CalendarImpl
{
public:

    // Constructors
    Calendar_gregorian();
    Calendar_gregorian(Era *_eraArray);

    /**
    * Destructor
    */
    ~Calendar_gregorian();

    // Methods
    virtual void SAL_CALL loadCalendar(const rtl::OUString& uniqueID, const com::sun::star::lang::Locale& rLocale) throw(com::sun::star::uno::RuntimeException);
    virtual void SAL_CALL setDateTime(double nTimeInDays) throw(com::sun::star::uno::RuntimeException);
    virtual double SAL_CALL getDateTime() throw(com::sun::star::uno::RuntimeException);
    virtual void SAL_CALL setValue( sal_Int16 nFieldIndex, sal_Int16 nValue ) throw(com::sun::star::uno::RuntimeException);
    virtual sal_Int16 SAL_CALL getValue(sal_Int16 nFieldIndex) throw(com::sun::star::uno::RuntimeException);
    virtual void SAL_CALL addValue(sal_Int16 nFieldIndex, sal_Int32 nAmount) throw(com::sun::star::uno::RuntimeException);
    virtual sal_Bool SAL_CALL isValid() throw (com::sun::star::uno::RuntimeException);
    virtual Calendar SAL_CALL getLoadedCalendar() throw(com::sun::star::uno::RuntimeException);
    virtual rtl::OUString SAL_CALL getUniqueID() throw(com::sun::star::uno::RuntimeException);
    virtual sal_Int16 SAL_CALL getFirstDayOfWeek() throw(com::sun::star::uno::RuntimeException);
    virtual void SAL_CALL setFirstDayOfWeek(sal_Int16 nDay) throw(com::sun::star::uno::RuntimeException);
    virtual void SAL_CALL setMinimumNumberOfDaysForFirstWeek(sal_Int16 nDays) throw(com::sun::star::uno::RuntimeException);
    virtual sal_Int16 SAL_CALL getMinimumNumberOfDaysForFirstWeek() throw(com::sun::star::uno::RuntimeException);
    virtual sal_Int16 SAL_CALL getNumberOfMonthsInYear() throw(com::sun::star::uno::RuntimeException);
    virtual sal_Int16 SAL_CALL getNumberOfDaysInWeek() throw(com::sun::star::uno::RuntimeException);
    virtual com::sun::star::uno::Sequence < CalendarItem > SAL_CALL getMonths() throw(com::sun::star::uno::RuntimeException);
    virtual com::sun::star::uno::Sequence < CalendarItem > SAL_CALL getDays() throw(com::sun::star::uno::RuntimeException);
    virtual rtl::OUString SAL_CALL getDisplayName(sal_Int16 nCalendarDisplayIndex, sal_Int16 nIdx, sal_Int16 nNameType) throw(com::sun::star::uno::RuntimeException);

    // Methods in XExtendedCalendar
    virtual rtl::OUString SAL_CALL getDisplayString( sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode ) throw (com::sun::star::uno::RuntimeException);

    //XServiceInfo
    virtual rtl::OUString SAL_CALL getImplementationName() throw(com::sun::star::uno::RuntimeException);
    virtual sal_Bool SAL_CALL supportsService(const rtl::OUString& ServiceName) throw(com::sun::star::uno::RuntimeException);
    virtual com::sun::star::uno::Sequence < rtl::OUString > SAL_CALL getSupportedServiceNames() throw(com::sun::star::uno::RuntimeException);

protected:
    Era *eraArray;
    icu::Calendar *body;
    NativeNumberSupplier aNatNum;
    const sal_Char* cCalendar;
    com::sun::star::lang::Locale aLocale;
    sal_uInt32 fieldSet;
    sal_Int16 fieldValue[CalendarFieldIndex::FIELD_COUNT];
    sal_Int16 fieldSetValue[CalendarFieldIndex::FIELD_COUNT];
    virtual void SAL_CALL mapToGregorian() throw(com::sun::star::uno::RuntimeException);
    virtual void SAL_CALL mapFromGregorian() throw(com::sun::star::uno::RuntimeException);
    void SAL_CALL getValue() throw(com::sun::star::uno::RuntimeException);
private:
    // submit fieldValue array according to fieldSet, plus YMDhms if >=0
    void SAL_CALL submitValues( sal_Int32 nYear, sal_Int32 nMonth, sal_Int32 nDay, sal_Int32 nHour, sal_Int32 nMinute, sal_Int32 nSecond, sal_Int32 nMilliSecond) throw(com::sun::star::uno::RuntimeException);
    void SAL_CALL setValue() throw(com::sun::star::uno::RuntimeException);
    void SAL_CALL init(Era *_eraArray) throw(com::sun::star::uno::RuntimeException);
    Calendar aCalendar;
    sal_Int16 aStartOfWeek;
};

//  ----------------------------------------------------
//  class Calendar_hanja
//  ----------------------------------------------------
class Calendar_hanja : public Calendar_gregorian
{
public:
    // Constructors
    Calendar_hanja();
    virtual void SAL_CALL loadCalendar(const rtl::OUString& uniqueID, const com::sun::star::lang::Locale& rLocale) throw(com::sun::star::uno::RuntimeException);
    virtual rtl::OUString SAL_CALL getDisplayName(sal_Int16 nCalendarDisplayIndex, sal_Int16 nIdx, sal_Int16 nNameType) throw(com::sun::star::uno::RuntimeException);
};

//  ----------------------------------------------------
//  class Calendar_gengou
//  ----------------------------------------------------
class Calendar_gengou : public Calendar_gregorian
{
public:
    // Constructors
    Calendar_gengou();
};

//  ----------------------------------------------------
//  class Calendar_ROC
//  ----------------------------------------------------
class Calendar_ROC : public Calendar_gregorian
{
public:
    // Constructors
    Calendar_ROC();
};

//  ----------------------------------------------------
//  class Calendar_buddhist
//  ----------------------------------------------------
class Calendar_buddhist : public Calendar_gregorian
{
public:
    // Constructors
    Calendar_buddhist();

    // Methods in XExtendedCalendar
    virtual rtl::OUString SAL_CALL getDisplayString( sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode ) throw (com::sun::star::uno::RuntimeException);
};

} } } }

#endif
<|endoftext|>


<commit_before>#include "GS.h"
#include <QtWidgets/QApplication>
#include <QNetworkProxyFactory>

boost::asio::io_service s_async_io_service(4);

int main(int argc, char *argv[])
{
    q::logging::add_logger(q::logging::Logger_uptr(new q::logging::Console_Logger()));
    q::logging::set_decorations(q::logging::Decorations(q::logging::Decoration::TIMESTAMP, q::logging::Decoration::LEVEL, q::logging::Decoration::TOPIC));

    boost::shared_ptr<boost::asio::io_service::work> work(new boost::asio::io_service::work(s_async_io_service));
    boost::thread_group worker_threads;
    for(int x = 0; x < 4; ++x)
    {
        worker_threads.create_thread(boost::bind(&boost::asio::io_service::run, &s_async_io_service));
    }

    Q_INIT_RESOURCE(res);

    QApplication a(argc, argv);
//    QApplication::setStyle("fusion");

    QNetworkProxyFactory::setUseSystemConfiguration(true);

    QCoreApplication::setOrganizationName("Silkopter");
    QCoreApplication::setOrganizationDomain("silkopter.com");
    QCoreApplication::setApplicationName("Silkopter");

    a.setQuitOnLastWindowClosed(true);

    GS w;
    w.show();
    auto res = a.exec();

    work.reset();
    worker_threads.join_all();

    return res;
}
<commit_msg>Fixed the locale<commit_after>#include "GS.h"
#include <QtWidgets/QApplication>
#include <QNetworkProxyFactory>

boost::asio::io_service s_async_io_service(4);

int main(int argc, char *argv[])
{
    q::logging::add_logger(q::logging::Logger_uptr(new q::logging::Console_Logger()));
    q::logging::set_decorations(q::logging::Decorations(q::logging::Decoration::TIMESTAMP, q::logging::Decoration::LEVEL, q::logging::Decoration::TOPIC));

    boost::shared_ptr<boost::asio::io_service::work> work(new boost::asio::io_service::work(s_async_io_service));
    boost::thread_group worker_threads;
    for(int x = 0; x < 4; ++x)
    {
        worker_threads.create_thread(boost::bind(&boost::asio::io_service::run, &s_async_io_service));
    }

    Q_INIT_RESOURCE(res);

    QApplication a(argc, argv);
//    QApplication::setStyle("fusion");

    QNetworkProxyFactory::setUseSystemConfiguration(true);

    QCoreApplication::setOrganizationName("Silkopter");
    QCoreApplication::setOrganizationDomain("silkopter.com");
    QCoreApplication::setApplicationName("Silkopter");

    a.setQuitOnLastWindowClosed(true);

    QLOGI("Current locale is '{}', changing to 'C'", setlocale(LC_ALL, "C"));

    GS w;
    w.show();
    auto res = a.exec();

    work.reset();
    worker_threads.join_all();

    return res;
}
<|endoftext|>


<commit_before>//===----- CompileOnDemandLayer.cpp - Lazily emit IR on first call --------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h"
#include "llvm/IR/Mangler.h"
#include "llvm/IR/Module.h"

using namespace llvm;
using namespace llvm::orc;

static void extractAliases(MaterializationResponsibility &R, Module &M,
                           MangleAndInterner &Mangle) {
  SymbolAliasMap Aliases;

  std::vector<GlobalAlias *> ModAliases;
  for (auto &A : M.aliases())
    ModAliases.push_back(&A);

  for (auto *A : ModAliases) {
    Constant *Aliasee = A->getAliasee();
    assert(A->hasName() && "Anonymous alias?");
    assert(Aliasee->hasName() && "Anonymous aliasee");
    std::string AliasName = A->getName();

    Aliases[Mangle(AliasName)] = SymbolAliasMapEntry(
        {Mangle(Aliasee->getName()), JITSymbolFlags::fromGlobalValue(*A)});

    if (isa<Function>(Aliasee)) {
      auto *F = cloneFunctionDecl(M, *cast<Function>(Aliasee));
      A->replaceAllUsesWith(F);
      A->eraseFromParent();
      F->setName(AliasName);
    } else if (isa<GlobalValue>(Aliasee)) {
      auto *G = cloneGlobalVariableDecl(M, *cast<GlobalVariable>(Aliasee));
      A->replaceAllUsesWith(G);
      A->eraseFromParent();
      G->setName(AliasName);
    }
  }

  R.replace(symbolAliases(std::move(Aliases)));
}

static ThreadSafeModule extractAndClone(ThreadSafeModule &TSM, StringRef Suffix,
                                        GVPredicate ShouldCloneDefinition) {

  auto DeleteClonedDefsAndPromoteDeclLinkages = [](GlobalValue &GV) {
    // Delete the definition and bump the linkage in the source module.
    if (isa<Function>(GV)) {
      auto &F = cast<Function>(GV);
      F.deleteBody();
      F.setPersonalityFn(nullptr);
    } else if (isa<GlobalVariable>(GV)) {
      cast<GlobalVariable>(GV).setInitializer(nullptr);
    } else
      llvm_unreachable("Unsupported global type");

    GV.setLinkage(GlobalValue::ExternalLinkage);
  };

  auto NewTSMod = cloneToNewContext(TSM, ShouldCloneDefinition,
                                    DeleteClonedDefsAndPromoteDeclLinkages);
  auto &M = *NewTSMod.getModule();
  M.setModuleIdentifier((M.getModuleIdentifier() + Suffix).str());

  return NewTSMod;
}

static ThreadSafeModule extractGlobals(ThreadSafeModule &TSM) {
  return extractAndClone(TSM, ".globals", [](const GlobalValue &GV) {
    return isa<GlobalVariable>(GV);
  });
}

namespace llvm {
namespace orc {

class ExtractingIRMaterializationUnit : public IRMaterializationUnit {
public:
  ExtractingIRMaterializationUnit(ExecutionSession &ES,
                                  CompileOnDemandLayer2 &Parent,
                                  ThreadSafeModule TSM)
      : IRMaterializationUnit(ES, std::move(TSM)), Parent(Parent) {}

  ExtractingIRMaterializationUnit(ThreadSafeModule TSM,
                                  SymbolFlagsMap SymbolFlags,
                                  SymbolNameToDefinitionMap SymbolToDefinition,
                                  CompileOnDemandLayer2 &Parent)
      : IRMaterializationUnit(std::move(TSM), std::move(SymbolFlags),
                              std::move(SymbolToDefinition)),
        Parent(Parent) {}

private:
  void materialize(MaterializationResponsibility R) override {
    // FIXME: Need a 'notify lazy-extracting/emitting' callback to tie the
    //        extracted module key, extracted module, and source module key
    //        together. This could be used, for example, to provide a specific
    //        memory manager instance to the linking layer.

    auto RequestedSymbols = R.getRequestedSymbols();

    // Extract the requested functions into a new module.
    ThreadSafeModule ExtractedFunctionsModule;
    if (!RequestedSymbols.empty()) {
      std::string Suffix;
      std::set<const GlobalValue *> FunctionsToClone;
      for (auto &Name : RequestedSymbols) {
        auto I = SymbolToDefinition.find(Name);
        assert(I != SymbolToDefinition.end() && I->second != nullptr &&
               "Should have a non-null definition");
        FunctionsToClone.insert(I->second);
        Suffix += ".";
        Suffix += *Name;
      }

      std::lock_guard<std::mutex> Lock(SourceModuleMutex);
      ExtractedFunctionsModule =
          extractAndClone(TSM, Suffix, [&](const GlobalValue &GV) -> bool {
            return FunctionsToClone.count(&GV);
          });
    }

    // Build a new ExtractingIRMaterializationUnit to delegate the unrequested
    // symbols to.
    SymbolFlagsMap DelegatedSymbolFlags;
    IRMaterializationUnit::SymbolNameToDefinitionMap
        DelegatedSymbolToDefinition;
    for (auto &KV : SymbolToDefinition) {
      if (RequestedSymbols.count(KV.first))
        continue;
      DelegatedSymbolFlags[KV.first] =
          JITSymbolFlags::fromGlobalValue(*KV.second);
      DelegatedSymbolToDefinition[KV.first] = KV.second;
    }

    if (!DelegatedSymbolFlags.empty()) {
      assert(DelegatedSymbolFlags.size() ==
                 DelegatedSymbolToDefinition.size() &&
             "SymbolFlags and SymbolToDefinition should have the same number "
             "of entries");
      R.replace(llvm::make_unique<ExtractingIRMaterializationUnit>(
          std::move(TSM), std::move(DelegatedSymbolFlags),
          std::move(DelegatedSymbolToDefinition), Parent));
    }

    if (ExtractedFunctionsModule)
      Parent.emitExtractedFunctionsModule(std::move(R),
                                          std::move(ExtractedFunctionsModule));
  }

  void discard(const JITDylib &V, SymbolStringPtr Name) override {
    // All original symbols were materialized by the CODLayer and should be
    // final. The function bodies provided by M should never be overridden.
    llvm_unreachable("Discard should never be called on an "
                     "ExtractingIRMaterializationUnit");
  }

  mutable std::mutex SourceModuleMutex;
  CompileOnDemandLayer2 &Parent;
};

CompileOnDemandLayer2::CompileOnDemandLayer2(
    ExecutionSession &ES, IRLayer &BaseLayer, LazyCallThroughManager &LCTMgr,
    IndirectStubsManagerBuilder BuildIndirectStubsManager)
    : IRLayer(ES), BaseLayer(BaseLayer), LCTMgr(LCTMgr),
      BuildIndirectStubsManager(std::move(BuildIndirectStubsManager)) {}

Error CompileOnDemandLayer2::add(JITDylib &V, VModuleKey K,
                                 ThreadSafeModule TSM) {
  return IRLayer::add(V, K, std::move(TSM));
}

void CompileOnDemandLayer2::emit(MaterializationResponsibility R, VModuleKey K,
                                 ThreadSafeModule TSM) {
  auto &ES = getExecutionSession();
  assert(TSM && "M should not be null");
  auto &M = *TSM.getModule();

  for (auto &GV : M.global_values())
    if (GV.hasWeakLinkage())
      GV.setLinkage(GlobalValue::ExternalLinkage);

  MangleAndInterner Mangle(ES, M.getDataLayout());

  extractAliases(R, *TSM.getModule(), Mangle);

  auto GlobalsModule = extractGlobals(TSM);

  // Delete the bodies of any available externally functions and build the
  // lazy reexports alias map.
  std::map<SymbolStringPtr, std::pair<JITTargetAddress, JITSymbolFlags>>
      StubCallbacksAndLinkages;
  auto &TargetJD = R.getTargetJITDylib();
  auto &Resources = getPerDylibResources(TargetJD);
  auto &ImplD = Resources.getImplDylib();

  SymbolAliasMap LazyReexports;
  for (auto &F : M.functions()) {
    if (F.isDeclaration())
      continue;

    if (F.hasAvailableExternallyLinkage()) {
      F.deleteBody();
      F.setPersonalityFn(nullptr);
      continue;
    }

    auto Flags = JITSymbolFlags::fromGlobalValue(F);
    assert(Flags.isCallable() && "Non-callable definition in functions module");

    auto MangledName = Mangle(F.getName());
    LazyReexports[MangledName] = SymbolAliasMapEntry(MangledName, Flags);
  }

  // Add the functions module to the implementation dylib using an extracting
  // materialization unit.
  if (auto Err =
          ImplD.define(llvm::make_unique<ExtractingIRMaterializationUnit>(
              ES, *this, std::move(TSM)))) {
    ES.reportError(std::move(Err));
    R.failMaterialization();
    return;
  }

  // Handle responsibility for function symbols by returning lazy reexports.
  auto &ISMgr = Resources.getISManager();
  R.replace(lazyReexports(LCTMgr, ISMgr, ImplD, LazyReexports));

  BaseLayer.emit(std::move(R), std::move(K), std::move(GlobalsModule));
}

CompileOnDemandLayer2::PerDylibResources &
CompileOnDemandLayer2::getPerDylibResources(JITDylib &TargetD) {
  auto I = DylibResources.find(&TargetD);
  if (I == DylibResources.end()) {
    auto &ImplD =
        getExecutionSession().createJITDylib(TargetD.getName() + ".impl");
    TargetD.withSearchOrderDo([&](const JITDylibList &TargetSearchOrder) {
      ImplD.setSearchOrder(TargetSearchOrder, false);
    });
    PerDylibResources PDR(ImplD, BuildIndirectStubsManager());
    I = DylibResources.insert(std::make_pair(&TargetD, std::move(PDR))).first;
  }

  return I->second;
}

void CompileOnDemandLayer2::emitExtractedFunctionsModule(
    MaterializationResponsibility R, ThreadSafeModule TSM) {
  auto K = getExecutionSession().allocateVModule();
  BaseLayer.emit(std::move(R), std::move(K), std::move(TSM));
}

} // end namespace orc
} // end namespace llvm
<commit_msg>[ORC] Narrow a cast: the block guarded by the condition only handles GlobalVariables, not all GlobalValues.<commit_after>//===----- CompileOnDemandLayer.cpp - Lazily emit IR on first call --------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h"
#include "llvm/IR/Mangler.h"
#include "llvm/IR/Module.h"

using namespace llvm;
using namespace llvm::orc;

static void extractAliases(MaterializationResponsibility &R, Module &M,
                           MangleAndInterner &Mangle) {
  SymbolAliasMap Aliases;

  std::vector<GlobalAlias *> ModAliases;
  for (auto &A : M.aliases())
    ModAliases.push_back(&A);

  for (auto *A : ModAliases) {
    Constant *Aliasee = A->getAliasee();
    assert(A->hasName() && "Anonymous alias?");
    assert(Aliasee->hasName() && "Anonymous aliasee");
    std::string AliasName = A->getName();

    Aliases[Mangle(AliasName)] = SymbolAliasMapEntry(
        {Mangle(Aliasee->getName()), JITSymbolFlags::fromGlobalValue(*A)});

    if (isa<Function>(Aliasee)) {
      auto *F = cloneFunctionDecl(M, *cast<Function>(Aliasee));
      A->replaceAllUsesWith(F);
      A->eraseFromParent();
      F->setName(AliasName);
    } else if (isa<GlobalVariable>(Aliasee)) {
      auto *G = cloneGlobalVariableDecl(M, *cast<GlobalVariable>(Aliasee));
      A->replaceAllUsesWith(G);
      A->eraseFromParent();
      G->setName(AliasName);
    }
  }

  R.replace(symbolAliases(std::move(Aliases)));
}

static ThreadSafeModule extractAndClone(ThreadSafeModule &TSM, StringRef Suffix,
                                        GVPredicate ShouldCloneDefinition) {

  auto DeleteClonedDefsAndPromoteDeclLinkages = [](GlobalValue &GV) {
    // Delete the definition and bump the linkage in the source module.
    if (isa<Function>(GV)) {
      auto &F = cast<Function>(GV);
      F.deleteBody();
      F.setPersonalityFn(nullptr);
    } else if (isa<GlobalVariable>(GV)) {
      cast<GlobalVariable>(GV).setInitializer(nullptr);
    } else
      llvm_unreachable("Unsupported global type");

    GV.setLinkage(GlobalValue::ExternalLinkage);
  };

  auto NewTSMod = cloneToNewContext(TSM, ShouldCloneDefinition,
                                    DeleteClonedDefsAndPromoteDeclLinkages);
  auto &M = *NewTSMod.getModule();
  M.setModuleIdentifier((M.getModuleIdentifier() + Suffix).str());

  return NewTSMod;
}

static ThreadSafeModule extractGlobals(ThreadSafeModule &TSM) {
  return extractAndClone(TSM, ".globals", [](const GlobalValue &GV) {
    return isa<GlobalVariable>(GV);
  });
}

namespace llvm {
namespace orc {

class ExtractingIRMaterializationUnit : public IRMaterializationUnit {
public:
  ExtractingIRMaterializationUnit(ExecutionSession &ES,
                                  CompileOnDemandLayer2 &Parent,
                                  ThreadSafeModule TSM)
      : IRMaterializationUnit(ES, std::move(TSM)), Parent(Parent) {}

  ExtractingIRMaterializationUnit(ThreadSafeModule TSM,
                                  SymbolFlagsMap SymbolFlags,
                                  SymbolNameToDefinitionMap SymbolToDefinition,
                                  CompileOnDemandLayer2 &Parent)
      : IRMaterializationUnit(std::move(TSM), std::move(SymbolFlags),
                              std::move(SymbolToDefinition)),
        Parent(Parent) {}

private:
  void materialize(MaterializationResponsibility R) override {
    // FIXME: Need a 'notify lazy-extracting/emitting' callback to tie the
    //        extracted module key, extracted module, and source module key
    //        together. This could be used, for example, to provide a specific
    //        memory manager instance to the linking layer.

    auto RequestedSymbols = R.getRequestedSymbols();

    // Extract the requested functions into a new module.
    ThreadSafeModule ExtractedFunctionsModule;
    if (!RequestedSymbols.empty()) {
      std::string Suffix;
      std::set<const GlobalValue *> FunctionsToClone;
      for (auto &Name : RequestedSymbols) {
        auto I = SymbolToDefinition.find(Name);
        assert(I != SymbolToDefinition.end() && I->second != nullptr &&
               "Should have a non-null definition");
        FunctionsToClone.insert(I->second);
        Suffix += ".";
        Suffix += *Name;
      }

      std::lock_guard<std::mutex> Lock(SourceModuleMutex);
      ExtractedFunctionsModule =
          extractAndClone(TSM, Suffix, [&](const GlobalValue &GV) -> bool {
            return FunctionsToClone.count(&GV);
          });
    }

    // Build a new ExtractingIRMaterializationUnit to delegate the unrequested
    // symbols to.
    SymbolFlagsMap DelegatedSymbolFlags;
    IRMaterializationUnit::SymbolNameToDefinitionMap
        DelegatedSymbolToDefinition;
    for (auto &KV : SymbolToDefinition) {
      if (RequestedSymbols.count(KV.first))
        continue;
      DelegatedSymbolFlags[KV.first] =
          JITSymbolFlags::fromGlobalValue(*KV.second);
      DelegatedSymbolToDefinition[KV.first] = KV.second;
    }

    if (!DelegatedSymbolFlags.empty()) {
      assert(DelegatedSymbolFlags.size() ==
                 DelegatedSymbolToDefinition.size() &&
             "SymbolFlags and SymbolToDefinition should have the same number "
             "of entries");
      R.replace(llvm::make_unique<ExtractingIRMaterializationUnit>(
          std::move(TSM), std::move(DelegatedSymbolFlags),
          std::move(DelegatedSymbolToDefinition), Parent));
    }

    if (ExtractedFunctionsModule)
      Parent.emitExtractedFunctionsModule(std::move(R),
                                          std::move(ExtractedFunctionsModule));
  }

  void discard(const JITDylib &V, SymbolStringPtr Name) override {
    // All original symbols were materialized by the CODLayer and should be
    // final. The function bodies provided by M should never be overridden.
    llvm_unreachable("Discard should never be called on an "
                     "ExtractingIRMaterializationUnit");
  }

  mutable std::mutex SourceModuleMutex;
  CompileOnDemandLayer2 &Parent;
};

CompileOnDemandLayer2::CompileOnDemandLayer2(
    ExecutionSession &ES, IRLayer &BaseLayer, LazyCallThroughManager &LCTMgr,
    IndirectStubsManagerBuilder BuildIndirectStubsManager)
    : IRLayer(ES), BaseLayer(BaseLayer), LCTMgr(LCTMgr),
      BuildIndirectStubsManager(std::move(BuildIndirectStubsManager)) {}

Error CompileOnDemandLayer2::add(JITDylib &V, VModuleKey K,
                                 ThreadSafeModule TSM) {
  return IRLayer::add(V, K, std::move(TSM));
}

void CompileOnDemandLayer2::emit(MaterializationResponsibility R, VModuleKey K,
                                 ThreadSafeModule TSM) {
  auto &ES = getExecutionSession();
  assert(TSM && "M should not be null");
  auto &M = *TSM.getModule();

  for (auto &GV : M.global_values())
    if (GV.hasWeakLinkage())
      GV.setLinkage(GlobalValue::ExternalLinkage);

  MangleAndInterner Mangle(ES, M.getDataLayout());

  extractAliases(R, *TSM.getModule(), Mangle);

  auto GlobalsModule = extractGlobals(TSM);

  // Delete the bodies of any available externally functions and build the
  // lazy reexports alias map.
  std::map<SymbolStringPtr, std::pair<JITTargetAddress, JITSymbolFlags>>
      StubCallbacksAndLinkages;
  auto &TargetJD = R.getTargetJITDylib();
  auto &Resources = getPerDylibResources(TargetJD);
  auto &ImplD = Resources.getImplDylib();

  SymbolAliasMap LazyReexports;
  for (auto &F : M.functions()) {
    if (F.isDeclaration())
      continue;

    if (F.hasAvailableExternallyLinkage()) {
      F.deleteBody();
      F.setPersonalityFn(nullptr);
      continue;
    }

    auto Flags = JITSymbolFlags::fromGlobalValue(F);
    assert(Flags.isCallable() && "Non-callable definition in functions module");

    auto MangledName = Mangle(F.getName());
    LazyReexports[MangledName] = SymbolAliasMapEntry(MangledName, Flags);
  }

  // Add the functions module to the implementation dylib using an extracting
  // materialization unit.
  if (auto Err =
          ImplD.define(llvm::make_unique<ExtractingIRMaterializationUnit>(
              ES, *this, std::move(TSM)))) {
    ES.reportError(std::move(Err));
    R.failMaterialization();
    return;
  }

  // Handle responsibility for function symbols by returning lazy reexports.
  auto &ISMgr = Resources.getISManager();
  R.replace(lazyReexports(LCTMgr, ISMgr, ImplD, LazyReexports));

  BaseLayer.emit(std::move(R), std::move(K), std::move(GlobalsModule));
}

CompileOnDemandLayer2::PerDylibResources &
CompileOnDemandLayer2::getPerDylibResources(JITDylib &TargetD) {
  auto I = DylibResources.find(&TargetD);
  if (I == DylibResources.end()) {
    auto &ImplD =
        getExecutionSession().createJITDylib(TargetD.getName() + ".impl");
    TargetD.withSearchOrderDo([&](const JITDylibList &TargetSearchOrder) {
      ImplD.setSearchOrder(TargetSearchOrder, false);
    });
    PerDylibResources PDR(ImplD, BuildIndirectStubsManager());
    I = DylibResources.insert(std::make_pair(&TargetD, std::move(PDR))).first;
  }

  return I->second;
}

void CompileOnDemandLayer2::emitExtractedFunctionsModule(
    MaterializationResponsibility R, ThreadSafeModule TSM) {
  auto K = getExecutionSession().allocateVModule();
  BaseLayer.emit(std::move(R), std::move(K), std::move(TSM));
}

} // end namespace orc
} // end namespace llvm
<|endoftext|>


<commit_before>//===-- PPCMCAsmInfo.cpp - PPC asm properties -----------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file contains the declarations of the MCAsmInfoDarwin properties.
//
//===----------------------------------------------------------------------===//

#include "PPCMCAsmInfo.h"
#include "llvm/ADT/Triple.h"

using namespace llvm;

void PPCMCAsmInfoDarwin::anchor() { }

PPCMCAsmInfoDarwin::PPCMCAsmInfoDarwin(bool is64Bit, const Triple& T) {
  if (is64Bit) {
    PointerSize = CalleeSaveStackSlotSize = 8;
  }
  IsLittleEndian = false;

  CommentString = ";";
  ExceptionsType = ExceptionHandling::DwarfCFI;

  if (!is64Bit)
    Data64bitsDirective = 0;      // We can't emit a 64-bit unit in PPC32 mode.

  AssemblerDialect = 1;           // New-Style mnemonics.
  SupportsDebugInformation= true; // Debug information.

  // old assembler lacks some directives
  // FIXME: this should really be a check on the assembler characteristics
  // rather than OS version
  if (T.isMacOSX() && T.isMacOSXVersionLT(10, 6))
    HasWeakDefCanBeHiddenDirective = false;
}

void PPCLinuxMCAsmInfo::anchor() { }

PPCLinuxMCAsmInfo::PPCLinuxMCAsmInfo(bool is64Bit) {
  if (is64Bit) {
    PointerSize = CalleeSaveStackSlotSize = 8;
  }
  IsLittleEndian = false;

  // ".comm align is in bytes but .align is pow-2."
  AlignmentIsInBytes = false;

  CommentString = "#";

  // Uses '.section' before '.bss' directive
  UsesELFSectionDirectiveForBSS = true;

  // Debug Information
  SupportsDebugInformation = true;

  DollarIsPC = true;

  // Set up DWARF directives
  HasLEB128 = true;  // Target asm supports leb128 directives (little-endian)
  MinInstAlignment = 4;

  // Exceptions handling
  ExceptionsType = ExceptionHandling::DwarfCFI;

  ZeroDirective = "\t.space\t";
  Data64bitsDirective = is64Bit ? "\t.quad\t" : 0;
  AssemblerDialect = 1;           // New-Style mnemonics.
}

<commit_msg>test commit. Amend a comment.<commit_after>//===-- PPCMCAsmInfo.cpp - PPC asm properties -----------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file contains the declarations of the MCAsmInfoDarwin properties.
//
//===----------------------------------------------------------------------===//

#include "PPCMCAsmInfo.h"
#include "llvm/ADT/Triple.h"

using namespace llvm;

void PPCMCAsmInfoDarwin::anchor() { }

PPCMCAsmInfoDarwin::PPCMCAsmInfoDarwin(bool is64Bit, const Triple& T) {
  if (is64Bit) {
    PointerSize = CalleeSaveStackSlotSize = 8;
  }
  IsLittleEndian = false;

  CommentString = ";";
  ExceptionsType = ExceptionHandling::DwarfCFI;

  if (!is64Bit)
    Data64bitsDirective = 0;      // We can't emit a 64-bit unit in PPC32 mode.

  AssemblerDialect = 1;           // New-Style mnemonics.
  SupportsDebugInformation= true; // Debug information.

  // The installed assembler for OSX < 10.6 lacks some directives.
  // FIXME: this should really be a check on the assembler characteristics
  // rather than OS version
  if (T.isMacOSX() && T.isMacOSXVersionLT(10, 6))
    HasWeakDefCanBeHiddenDirective = false;
}

void PPCLinuxMCAsmInfo::anchor() { }

PPCLinuxMCAsmInfo::PPCLinuxMCAsmInfo(bool is64Bit) {
  if (is64Bit) {
    PointerSize = CalleeSaveStackSlotSize = 8;
  }
  IsLittleEndian = false;

  // ".comm align is in bytes but .align is pow-2."
  AlignmentIsInBytes = false;

  CommentString = "#";

  // Uses '.section' before '.bss' directive
  UsesELFSectionDirectiveForBSS = true;

  // Debug Information
  SupportsDebugInformation = true;

  DollarIsPC = true;

  // Set up DWARF directives
  HasLEB128 = true;  // Target asm supports leb128 directives (little-endian)
  MinInstAlignment = 4;

  // Exceptions handling
  ExceptionsType = ExceptionHandling::DwarfCFI;

  ZeroDirective = "\t.space\t";
  Data64bitsDirective = is64Bit ? "\t.quad\t" : 0;
  AssemblerDialect = 1;           // New-Style mnemonics.
}

<|endoftext|>


<commit_before>/** @file

  A brief file description

  @section license License

  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 */

#include <cstdarg>
#include "tscore/ink_platform.h"
#include "tscore/ink_memory.h"
#include "tscore/TextBuffer.h"

/****************************************************************************
 *
 *  TextBuffer.cc - A self-expanding buffer, primarily meant for strings
 *
 *
 *
 ****************************************************************************/

TextBuffer::TextBuffer(int size)
{
  bufferStart = nullptr;
  nextAdd     = nullptr;
  currentSize = spaceLeft = 0;
  if (size > 0) {
    // Institute a minimum size
    if (size < 1024) {
      size = 1024;
    }

    bufferStart = (char *)ats_malloc(size);
    nextAdd     = bufferStart;
    currentSize = size;
    spaceLeft   = size - 1; // Leave room for a terminator;
    nextAdd[0]  = '\0';
  }
}

TextBuffer::~TextBuffer()
{
  ats_free(bufferStart);
}

char *
TextBuffer::release()
{
  char *ret = bufferStart;

  bufferStart = nextAdd = nullptr;
  currentSize = spaceLeft = 0;

  return ret;
}

// void TextBuffer::reUse()
//
//   Sets the text buffer for reuse by repositioning the
//     ptrs to beginning of buffer.  The buffer space is
//     reused
void
TextBuffer::reUse()
{
  if (bufferStart != nullptr) {
    nextAdd    = bufferStart;
    spaceLeft  = currentSize - 1;
    nextAdd[0] = '\0';
  }
}

// int TextBuffer::copyFrom(void*,int num_bytes)
//
//
//  Copy N bytes (determined by num_bytes) on to the
//  end of the buffer.
//
//  Returns the number of bytes copies or
//  -1 if there was insufficient memory
int
TextBuffer::copyFrom(const void *source, unsigned num_bytes)
{
  // Get more space if necessary
  if (spaceLeft < num_bytes) {
    if (enlargeBuffer(num_bytes) == -1) {
      return -1;
    }
  }

  memcpy(nextAdd, source, num_bytes);
  spaceLeft -= num_bytes;

  nextAdd += num_bytes;
  nextAdd[0] = '\0';

  return num_bytes;
}

//  TextBuffer::enlargeBuffer(int n)
//
//  Enlarge the buffer so at least at N
//    bytes are free in the buffer.
//
//  Always enlarges by a power of two.
//
//  Returns -1 if insufficient memory,
//    zero otherwise
int
TextBuffer::enlargeBuffer(unsigned N)
{
  unsigned addedSize = 0;
  unsigned newSize   = (currentSize ? currentSize : 1) * 2;
  char *newSpace;

  if (spaceLeft < N) {
    while ((newSize - currentSize) < N) {
      newSize *= 2;
    }

    addedSize = newSize - currentSize;

    newSpace = (char *)ats_realloc(bufferStart, newSize);
    if (newSpace != nullptr) {
      nextAdd     = newSpace + (unsigned)(nextAdd - bufferStart);
      bufferStart = newSpace;
      spaceLeft += addedSize;
      currentSize = newSize;
    } else {
      // Out of Memory, Sigh
      return -1;
    }
  }

  return 0;
}

// int TextBuffer::rawReadFromFile
//
// - Issues a single read command on the file descriptor or handle
//   passed in and reads in raw data (not assumed to be text, no
//   string terminators added).
// - Cannot read from file descriptor on win32 because the win32
//   read() function replaces CR-LF with LF if the file is not
//   opened in binary mode.
int
TextBuffer::rawReadFromFile(int fd)
{
  int readSize;

  // Check to see if we have got a reasonable amount of space left in our
  //   buffer, if not try to get some more
  if (spaceLeft < 4096) {
    if (enlargeBuffer(4096) == -1) {
      return -1;
    }
  }

  readSize = read(fd, nextAdd, spaceLeft - 1);

  if (readSize == 0) { // EOF
    return 0;
  } else if (readSize < 0) {
    // Error on read
    return readSize;
  } else {
    nextAdd = nextAdd + readSize;
    spaceLeft -= readSize;
    return readSize;
  }
}

// Read the entire contents of the given file descriptor.
void
TextBuffer::slurp(int fd)
{
  int nbytes;

  do {
    nbytes = readFromFD(fd);
  } while (nbytes > 0);
}

// int TextBuffer::readFromFD(int fd)
//
// Issues a single read command on the file
// descriptor passed in.  Attempts to read a minimum of
// 512 bytes from file descriptor passed.
int
TextBuffer::readFromFD(int fd)
{
  int readSize;

  // Check to see if we have got a reasonable amount of space left in our
  //   buffer, if not try to get some more
  if (spaceLeft < 512) {
    if (enlargeBuffer(512) == -1) {
      return -1;
    }
  }

  readSize = read(fd, nextAdd, spaceLeft - 1);

  if (readSize == 0) {
    // Socket is empty so we are done
    return 0;
  } else if (readSize < 0) {
    // Error on read
    return readSize;
  } else {
    nextAdd    = nextAdd + readSize;
    nextAdd[0] = '\0';
    spaceLeft -= readSize + 1;
    return readSize;
  }
}

void
TextBuffer::vformat(const char *fmt, va_list ap)
{
  for (bool done = false; !done;) {
    int num;

    // Copy the args in case the buffer isn't big enough and we need to
    // try again. Vsnprintf modifies the va_list on each pass.
    va_list args;
    va_copy(args, ap);

    num = vsnprintf(this->nextAdd, this->spaceLeft, fmt, args);

    va_end(args);

    if ((unsigned)num < this->spaceLeft) {
      // We had enough space to format including the NUL. Since the returned character
      // count does not include the NUL, we can just increment and the next format will
      // overwrite the previous NUL.
      this->spaceLeft -= num;
      this->nextAdd += num;
      done = true;
    } else {
      if (enlargeBuffer(num + 1) == -1) {
        return;
      }
    }
  }
}

void
TextBuffer::format(const char *fmt, ...)
{
  va_list ap;

  va_start(ap, fmt);
  vformat(fmt, ap);
  va_end(ap);
}

void
TextBuffer::chomp()
{
  while ((nextAdd > bufferStart) && (nextAdd[-1] == '\n')) {
    --nextAdd;
    ++spaceLeft;
    *nextAdd = '\0';
  }
}
<commit_msg>Add cstdio in TextBuffer for vsnprintf<commit_after>/** @file

  A brief file description

  @section license License

  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 */

#include <cstdarg>
#include <cstdio>
#include "tscore/ink_platform.h"
#include "tscore/ink_memory.h"
#include "tscore/TextBuffer.h"

/****************************************************************************
 *
 *  TextBuffer.cc - A self-expanding buffer, primarily meant for strings
 *
 *
 *
 ****************************************************************************/

TextBuffer::TextBuffer(int size)
{
  bufferStart = nullptr;
  nextAdd     = nullptr;
  currentSize = spaceLeft = 0;
  if (size > 0) {
    // Institute a minimum size
    if (size < 1024) {
      size = 1024;
    }

    bufferStart = (char *)ats_malloc(size);
    nextAdd     = bufferStart;
    currentSize = size;
    spaceLeft   = size - 1; // Leave room for a terminator;
    nextAdd[0]  = '\0';
  }
}

TextBuffer::~TextBuffer()
{
  ats_free(bufferStart);
}

char *
TextBuffer::release()
{
  char *ret = bufferStart;

  bufferStart = nextAdd = nullptr;
  currentSize = spaceLeft = 0;

  return ret;
}

// void TextBuffer::reUse()
//
//   Sets the text buffer for reuse by repositioning the
//     ptrs to beginning of buffer.  The buffer space is
//     reused
void
TextBuffer::reUse()
{
  if (bufferStart != nullptr) {
    nextAdd    = bufferStart;
    spaceLeft  = currentSize - 1;
    nextAdd[0] = '\0';
  }
}

// int TextBuffer::copyFrom(void*,int num_bytes)
//
//
//  Copy N bytes (determined by num_bytes) on to the
//  end of the buffer.
//
//  Returns the number of bytes copies or
//  -1 if there was insufficient memory
int
TextBuffer::copyFrom(const void *source, unsigned num_bytes)
{
  // Get more space if necessary
  if (spaceLeft < num_bytes) {
    if (enlargeBuffer(num_bytes) == -1) {
      return -1;
    }
  }

  memcpy(nextAdd, source, num_bytes);
  spaceLeft -= num_bytes;

  nextAdd += num_bytes;
  nextAdd[0] = '\0';

  return num_bytes;
}

//  TextBuffer::enlargeBuffer(int n)
//
//  Enlarge the buffer so at least at N
//    bytes are free in the buffer.
//
//  Always enlarges by a power of two.
//
//  Returns -1 if insufficient memory,
//    zero otherwise
int
TextBuffer::enlargeBuffer(unsigned N)
{
  unsigned addedSize = 0;
  unsigned newSize   = (currentSize ? currentSize : 1) * 2;
  char *newSpace;

  if (spaceLeft < N) {
    while ((newSize - currentSize) < N) {
      newSize *= 2;
    }

    addedSize = newSize - currentSize;

    newSpace = (char *)ats_realloc(bufferStart, newSize);
    if (newSpace != nullptr) {
      nextAdd     = newSpace + (unsigned)(nextAdd - bufferStart);
      bufferStart = newSpace;
      spaceLeft += addedSize;
      currentSize = newSize;
    } else {
      // Out of Memory, Sigh
      return -1;
    }
  }

  return 0;
}

// int TextBuffer::rawReadFromFile
//
// - Issues a single read command on the file descriptor or handle
//   passed in and reads in raw data (not assumed to be text, no
//   string terminators added).
// - Cannot read from file descriptor on win32 because the win32
//   read() function replaces CR-LF with LF if the file is not
//   opened in binary mode.
int
TextBuffer::rawReadFromFile(int fd)
{
  int readSize;

  // Check to see if we have got a reasonable amount of space left in our
  //   buffer, if not try to get some more
  if (spaceLeft < 4096) {
    if (enlargeBuffer(4096) == -1) {
      return -1;
    }
  }

  readSize = read(fd, nextAdd, spaceLeft - 1);

  if (readSize == 0) { // EOF
    return 0;
  } else if (readSize < 0) {
    // Error on read
    return readSize;
  } else {
    nextAdd = nextAdd + readSize;
    spaceLeft -= readSize;
    return readSize;
  }
}

// Read the entire contents of the given file descriptor.
void
TextBuffer::slurp(int fd)
{
  int nbytes;

  do {
    nbytes = readFromFD(fd);
  } while (nbytes > 0);
}

// int TextBuffer::readFromFD(int fd)
//
// Issues a single read command on the file
// descriptor passed in.  Attempts to read a minimum of
// 512 bytes from file descriptor passed.
int
TextBuffer::readFromFD(int fd)
{
  int readSize;

  // Check to see if we have got a reasonable amount of space left in our
  //   buffer, if not try to get some more
  if (spaceLeft < 512) {
    if (enlargeBuffer(512) == -1) {
      return -1;
    }
  }

  readSize = read(fd, nextAdd, spaceLeft - 1);

  if (readSize == 0) {
    // Socket is empty so we are done
    return 0;
  } else if (readSize < 0) {
    // Error on read
    return readSize;
  } else {
    nextAdd    = nextAdd + readSize;
    nextAdd[0] = '\0';
    spaceLeft -= readSize + 1;
    return readSize;
  }
}

void
TextBuffer::vformat(const char *fmt, va_list ap)
{
  for (bool done = false; !done;) {
    int num;

    // Copy the args in case the buffer isn't big enough and we need to
    // try again. Vsnprintf modifies the va_list on each pass.
    va_list args;
    va_copy(args, ap);

    num = vsnprintf(this->nextAdd, this->spaceLeft, fmt, args);

    va_end(args);

    if ((unsigned)num < this->spaceLeft) {
      // We had enough space to format including the NUL. Since the returned character
      // count does not include the NUL, we can just increment and the next format will
      // overwrite the previous NUL.
      this->spaceLeft -= num;
      this->nextAdd += num;
      done = true;
    } else {
      if (enlargeBuffer(num + 1) == -1) {
        return;
      }
    }
  }
}

void
TextBuffer::format(const char *fmt, ...)
{
  va_list ap;

  va_start(ap, fmt);
  vformat(fmt, ap);
  va_end(ap);
}

void
TextBuffer::chomp()
{
  while ((nextAdd > bufferStart) && (nextAdd[-1] == '\n')) {
    --nextAdd;
    ++spaceLeft;
    *nextAdd = '\0';
  }
}
<|endoftext|>


<commit_before>#ifndef Bull_Os_hpp
#define Bull_Os_hpp

#if defined _WIN32
    #define BULL_OS_WINDOWS

    /// Prevent conflict with other libs with which would redefined _WIN32_WINNT
    #if defined BULL_BUILD
        #ifndef WIN32_LEAN_AND_MEAN
            #define WIN32_LEAN_AND_MEAN 1
        #endif

        #ifndef NOMINMAX
            #define NOMINMAX
        #endif

        #if defined BULL_WINDOWS_8 || defined BULL_WINDOWS_BLUE
            #define BULL_WINNT 0x0602
        #elif defined BULL_WINDOWS_7
            #define BULL_WINNT 0x0601
        #elif defined BULL_WINDOWS_SERVER_2008 || defined BULL_WINDOWS_VISTA
            #define BULL_WINNT 0x0600
        #else
            #define BULL_WINNT 0x0501
        #endif

        #if defined _WIN32_WINNT
            #if _WIN32_WINNT < BULL_WINNT
                #undef _WIN32_WINNT
                #define _WIN32_WINNT BULL_WINNT
            #endif
        #else
            #define _WIN32_WINNT BULL_WINNT
        #endif
    #endif // defined
#elif defined __FreeBSD__
    #define BULL_OS_FREEBSD
    #define BULL_OS_UNIX
#elif defined __gnu_linux__
    #define BULL_OS_GNU_LINUX
    #define BULL_OS_UNIX
#elif defined __APPLE__ && __MACH__
    #define BULL_OS_OSX
    #define BULL_OS_UNIX
#else
    #error Your system is not supported by Bull
#endif

#endif // Bull_Os_hpp
<commit_msg>Fix documentation<commit_after>#ifndef Bull_Os_hpp
#define Bull_Os_hpp

#if defined _WIN32
    #define BULL_OS_WINDOWS

    /// Prevent conflict with other libs which would redefined _WIN32_WINNT
    #if defined BULL_BUILD
        #ifndef WIN32_LEAN_AND_MEAN
            #define WIN32_LEAN_AND_MEAN 1
        #endif

        #ifndef NOMINMAX
            #define NOMINMAX
        #endif

        #if defined BULL_WINDOWS_8 || defined BULL_WINDOWS_BLUE
            #define BULL_WINNT 0x0602
        #elif defined BULL_WINDOWS_7
            #define BULL_WINNT 0x0601
        #elif defined BULL_WINDOWS_SERVER_2008 || defined BULL_WINDOWS_VISTA
            #define BULL_WINNT 0x0600
        #else
            #define BULL_WINNT 0x0501
        #endif

        #if defined _WIN32_WINNT
            #if _WIN32_WINNT < BULL_WINNT
                #undef _WIN32_WINNT
                #define _WIN32_WINNT BULL_WINNT
            #endif
        #else
            #define _WIN32_WINNT BULL_WINNT
        #endif
    #endif // defined
#elif defined __FreeBSD__
    #define BULL_OS_FREEBSD
    #define BULL_OS_UNIX
#elif defined __gnu_linux__
    #define BULL_OS_GNU_LINUX
    #define BULL_OS_UNIX
#elif defined __APPLE__ && __MACH__
    #define BULL_OS_OSX
    #define BULL_OS_UNIX
#else
    #error Your system is not supported by Bull
#endif

#endif // Bull_Os_hpp
<|endoftext|>


<commit_before>// Copyright (c) 2014, Salesforce.com, Inc.  All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// - Redistributions of source code must retain the above copyright
//   notice, this list of conditions and the following disclaimer.
// - Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in the
//   documentation and/or other materials provided with the distribution.
// - Neither the name of Salesforce.com nor the names of its contributors
//   may be used to endorse or promote products derived from this
//   software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
// COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
// OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
// TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#pragma once

#include <distributions/common.hpp>
#include <distributions/special.hpp>
#include <distributions/random.hpp>
#include <distributions/vector.hpp>
#include <distributions/vector_math.hpp>
#include <distributions/mixture.hpp>

namespace distributions {
namespace dirichlet_discrete {

typedef uint32_t count_t;
typedef int Value;
template<int max_dim> struct Group;
template<int max_dim> struct Scorer;
template<int max_dim> struct Sampler;
template<int max_dim> struct Mixture;

template<int max_dim>
struct Shared
{
    typedef dirichlet_discrete::Value Value;
    typedef typename dirichlet_discrete::Group<max_dim> Group;
    typedef typename dirichlet_discrete::Scorer<max_dim> Scorer;
    typedef typename dirichlet_discrete::Sampler<max_dim> Sampler;

    int dim;  // fixed parameter
    float alphas[max_dim];  // hyperparamter

    static Shared<max_dim> EXAMPLE ()
    {
        Shared<max_dim> shared;
        shared.dim = max_dim;
        for (int i = 0; i < max_dim; ++i) {
            shared.alphas[i] = 0.5;
        }
        return shared;
    }
};

template<int max_dim>
struct Group
{
    typedef dirichlet_discrete::Value Value;

    count_t count_sum;
    count_t counts[max_dim];

    void init (
            const Shared<max_dim> & shared,
            rng_t &)
    {
        count_sum = 0;
        for (Value value = 0; value < shared.dim; ++value) {
            counts[value] = 0;
        }
    }

    void add_value (
            const Shared<max_dim> & shared,
            const Value & value,
            rng_t &)
    {
        DIST_ASSERT1(value < shared.dim, "bad value: out of bounds: " << value);
        count_sum += 1;
        counts[value] += 1;
    }

    void remove_value (
            const Shared<max_dim> & shared,
            const Value & value,
            rng_t &)
    {
        DIST_ASSERT1(value < shared.dim, "value out of bounds: " << value);
        count_sum -= 1;
        counts[value] -= 1;
    }

    void merge (
            const Shared<max_dim> & shared,
            const Group<max_dim> & source,
            rng_t &)
    {
        for (Value value = 0; value < shared.dim; ++value) {
            counts[value] += source.counts[value];
        }
    }

    float score_value (
            const Shared<max_dim> & shared,
            const Value & value,
            rng_t & rng) const;
};

template<int max_dim>
struct Sampler
{
    float ps[max_dim];

    void init (
            const Shared<max_dim> & shared,
            const Group<max_dim> & group,
            rng_t & rng)
    {
        for (Value value = 0; value < shared.dim; ++value) {
            ps[value] = shared.alphas[value] + group.counts[value];
        }

        sample_dirichlet(rng, shared.dim, ps, ps);
    }

    Value eval (
            const Shared<max_dim> & shared,
            rng_t & rng) const
    {
        return sample_discrete(rng, shared.dim, ps);
    }
};

template<int max_dim>
struct Scorer
{
    float alpha_sum;
    float alphas[max_dim];

    void init (
            const Shared<max_dim> & shared,
            const Group<max_dim> & group,
            rng_t &)
    {
        alpha_sum = 0;
        for (Value value = 0; value < shared.dim; ++value) {
            float alpha = shared.alphas[value] + group.counts[value];
            alphas[value] = alpha;
            alpha_sum += alpha;
        }
    }

    float eval (
            const Shared<max_dim> & shared,
            const Value & value,
            rng_t &) const
    {
        DIST_ASSERT1(value < shared.dim, "value out of bounds: " << value);
        return fast_log(alphas[value] / alpha_sum);
    }
};

template<int max_dim>
inline float Group<max_dim>::score_value (
        const Shared<max_dim> & shared,
        const Value & value,
        rng_t & rng) const
{
    Scorer<max_dim> scorer;
    scorer.init(shared, * this, rng);
    return scorer.eval(shared, value, rng);
}

template<int max_dim>
struct VectorizedScorer
{
    typedef dirichlet_discrete::Value Value;
    typedef dirichlet_discrete::Shared<max_dim> Shared;
    typedef dirichlet_discrete::Group<max_dim> Group;
    typedef dirichlet_discrete::Scorer<max_dim> BaseScorer;

    float alpha_sum;
    std::vector<VectorFloat> scores;
    VectorFloat scores_shift;

    void resize(const Shared & shared, size_t size)
    {
        scores_shift.resize(size);
        scores.resize(shared.dim);
        for (Value value = 0; value < shared.dim; ++value) {
            scores[value].resize(size);
        }
    }

    void add_group (const Shared & shared, rng_t & rng)
    {
        scores_shift.packed_add(0);
        for (Value value = 0; value < shared.dim; ++value) {
            scores[value].packed_add(0);
        }
    }

    void remove_group (const Shared & shared, size_t groupid)
    {
        scores_shift.packed_remove(groupid);
        for (Value value = 0; value < shared.dim; ++value) {
            scores[value].packed_remove(groupid);
        }
    }

    void update_group (
            const Shared & shared,
            size_t groupid,
            const Group & group,
            rng_t & rng)
    {
        for (Value value = 0; value < shared.dim; ++value) {
            update_group(shared, groupid, group, value, rng);
        }
    }

    void update_group (
            const Shared & shared,
            size_t groupid,
            const Group & group,
            const Value & value,
            rng_t &)
    {
        scores[value][groupid] =
            fast_log(shared.alphas[value] + group.counts[value]);
        scores_shift[groupid] = fast_log(alpha_sum + group.count_sum);
    }

    void update_all (
            const Shared & shared,
            const MixtureSlave<Shared> & slave,
            rng_t &)
    {
        const size_t group_count = slave.groups().size();

        alpha_sum = 0;
        for (Value value = 0; value < shared.dim; ++value) {
            alpha_sum += shared.alphas[value];
        }
        for (size_t groupid = 0; groupid < group_count; ++groupid) {
            const Group & group = slave.groups()[groupid];
            for (Value value = 0; value < shared.dim; ++value) {
                scores[value][groupid] =
                    shared.alphas[value] + group.counts[value];
            }
            scores_shift[groupid] = alpha_sum + group.count_sum;
        }
        vector_log(group_count, scores_shift.data());
        for (Value value = 0; value < shared.dim; ++value) {
            vector_log(group_count, scores[value].data());
        }
    }

    void score_value(
            const Shared &,
            const Value & value,
            VectorFloat & scores_accum,
            rng_t &) const
    {
        vector_add_subtract(
            scores_accum.size(),
            scores_accum.data(),
            scores[value].data(),
            scores_shift.data());
    }
};

template<int max_dim>
class Mixture
{
public:

    typedef dirichlet_discrete::Value Value;
    typedef dirichlet_discrete::Shared<max_dim> Shared;
    typedef dirichlet_discrete::Group<max_dim> Group;
    typedef dirichlet_discrete::Scorer<max_dim> Scorer;
    typedef dirichlet_discrete::VectorizedScorer<max_dim> VectorizedScorer;

    VectorizedScorer scorer;

    std::vector<Group> & groups () { return slave_.groups(); }
    Group & groups (size_t i) { return slave_.groups(i); }
    const std::vector<Group> & groups () const { return slave_.groups(); }
    const Group & groups (size_t i) const { return slave_.groups(i); }

    void init (
            const Shared & shared,
            rng_t & rng)
    {
        slave_.init(shared, rng);
        scorer.resize(shared, slave_.groups().size());
        scorer.update_all(shared, slave_, rng);
    }

    void add_group (
            const Shared & shared,
            rng_t & rng)
    {
        const size_t groupid = slave_.groups().size();
        slave_.add_group(shared, rng);
        scorer.add_group(shared, rng);
        scorer.update_group(shared, groupid, groups()[groupid], rng);
    }

    void remove_group (
            const Shared & shared,
            size_t groupid)
    {
        slave_.remove_group(shared, groupid);
        scorer.remove_group(shared, groupid);
    }

    void add_value (
            const Shared & shared,
            size_t groupid,
            const Value & value,
            rng_t & rng)
    {
        DIST_ASSERT1(value < shared.dim, "value out of bounds: " << value);
        slave_.add_value(shared, groupid, value, rng);
        scorer.update_group(shared, groupid, groups()[groupid], value, rng);
    }

    void remove_value (
            const Shared & shared,
            size_t groupid,
            const Value & value,
            rng_t & rng)
    {
        DIST_ASSERT1(value < shared.dim, "value out of bounds: " << value);
        slave_.remove_value(shared, groupid, value, rng);
        scorer.update_group(shared, groupid, groups()[groupid], value, rng);
    }

    void score_value (
            const Shared & shared,
            const Value & value,
            VectorFloat & scores_accum,
            rng_t & rng) const
    {
        DIST_ASSERT1(value < shared.dim, "value out of bounds: " << value);
        if (DIST_DEBUG_LEVEL >= 2) {
            DIST_ASSERT_EQ(scores_accum.size(), slave_.groups().size());
        }
        scorer.score_value(shared, value, scores_accum, rng);
    }

    float score_mixture (
            const Shared & shared,
            rng_t & rng) const
    {
        return slave_.score_mixture(shared, rng);
    }

private:

    MixtureSlave<Shared> slave_;
};

template<int max_dim>
inline Value sample_value (
        const Shared<max_dim> & shared,
        const Group<max_dim> & group,
        rng_t & rng)
{
    Sampler<max_dim> sampler;
    sampler.init(shared, group, rng);
    return sampler.eval(shared, rng);
}

template<int max_dim>
inline float score_group (
        const Shared<max_dim> & shared,
        const Group<max_dim> & group,
        rng_t &)
{
    float alpha_sum = 0;
    float score = 0;

    for (Value value = 0; value < shared.dim; ++value) {
        float alpha = shared.alphas[value];
        alpha_sum += alpha;
        score += fast_lgamma(alpha + group.counts[value]) - fast_lgamma(alpha);
    }

    score += fast_lgamma(alpha_sum) - fast_lgamma(alpha_sum + group.count_sum);

    return score;
}

} // namespace dirichlet_discrete
} // namespace distributions
<commit_msg>Fix unused variable.<commit_after>// Copyright (c) 2014, Salesforce.com, Inc.  All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// - Redistributions of source code must retain the above copyright
//   notice, this list of conditions and the following disclaimer.
// - Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in the
//   documentation and/or other materials provided with the distribution.
// - Neither the name of Salesforce.com nor the names of its contributors
//   may be used to endorse or promote products derived from this
//   software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
// COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
// OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
// TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#pragma once

#include <distributions/common.hpp>
#include <distributions/special.hpp>
#include <distributions/random.hpp>
#include <distributions/vector.hpp>
#include <distributions/vector_math.hpp>
#include <distributions/mixture.hpp>

namespace distributions {
namespace dirichlet_discrete {

typedef uint32_t count_t;
typedef int Value;
template<int max_dim> struct Group;
template<int max_dim> struct Scorer;
template<int max_dim> struct Sampler;
template<int max_dim> struct Mixture;

template<int max_dim>
struct Shared
{
    typedef dirichlet_discrete::Value Value;
    typedef typename dirichlet_discrete::Group<max_dim> Group;
    typedef typename dirichlet_discrete::Scorer<max_dim> Scorer;
    typedef typename dirichlet_discrete::Sampler<max_dim> Sampler;

    int dim;  // fixed parameter
    float alphas[max_dim];  // hyperparamter

    static Shared<max_dim> EXAMPLE ()
    {
        Shared<max_dim> shared;
        shared.dim = max_dim;
        for (int i = 0; i < max_dim; ++i) {
            shared.alphas[i] = 0.5;
        }
        return shared;
    }
};

template<int max_dim>
struct Group
{
    typedef dirichlet_discrete::Value Value;

    count_t count_sum;
    count_t counts[max_dim];

    void init (
            const Shared<max_dim> & shared,
            rng_t &)
    {
        count_sum = 0;
        for (Value value = 0; value < shared.dim; ++value) {
            counts[value] = 0;
        }
    }

    void add_value (
            const Shared<max_dim> & shared,
            const Value & value,
            rng_t &)
    {
        DIST_ASSERT1(value < shared.dim, "bad value: out of bounds: " << value);
        count_sum += 1;
        counts[value] += 1;
    }

    void remove_value (
            const Shared<max_dim> & shared,
            const Value & value,
            rng_t &)
    {
        DIST_ASSERT1(value < shared.dim, "value out of bounds: " << value);
        count_sum -= 1;
        counts[value] -= 1;
    }

    void merge (
            const Shared<max_dim> & shared,
            const Group<max_dim> & source,
            rng_t &)
    {
        for (Value value = 0; value < shared.dim; ++value) {
            counts[value] += source.counts[value];
        }
    }

    float score_value (
            const Shared<max_dim> & shared,
            const Value & value,
            rng_t & rng) const;
};

template<int max_dim>
struct Sampler
{
    float ps[max_dim];

    void init (
            const Shared<max_dim> & shared,
            const Group<max_dim> & group,
            rng_t & rng)
    {
        for (Value value = 0; value < shared.dim; ++value) {
            ps[value] = shared.alphas[value] + group.counts[value];
        }

        sample_dirichlet(rng, shared.dim, ps, ps);
    }

    Value eval (
            const Shared<max_dim> & shared,
            rng_t & rng) const
    {
        return sample_discrete(rng, shared.dim, ps);
    }
};

template<int max_dim>
struct Scorer
{
    float alpha_sum;
    float alphas[max_dim];

    void init (
            const Shared<max_dim> & shared,
            const Group<max_dim> & group,
            rng_t &)
    {
        alpha_sum = 0;
        for (Value value = 0; value < shared.dim; ++value) {
            float alpha = shared.alphas[value] + group.counts[value];
            alphas[value] = alpha;
            alpha_sum += alpha;
        }
    }

    float eval (
            const Shared<max_dim> & shared,
            const Value & value,
            rng_t &) const
    {
        DIST_ASSERT1(value < shared.dim, "value out of bounds: " << value);
        return fast_log(alphas[value] / alpha_sum);
    }
};

template<int max_dim>
inline float Group<max_dim>::score_value (
        const Shared<max_dim> & shared,
        const Value & value,
        rng_t & rng) const
{
    Scorer<max_dim> scorer;
    scorer.init(shared, * this, rng);
    return scorer.eval(shared, value, rng);
}

template<int max_dim>
struct VectorizedScorer
{
    typedef dirichlet_discrete::Value Value;
    typedef dirichlet_discrete::Shared<max_dim> Shared;
    typedef dirichlet_discrete::Group<max_dim> Group;
    typedef dirichlet_discrete::Scorer<max_dim> BaseScorer;

    float alpha_sum;
    std::vector<VectorFloat> scores;
    VectorFloat scores_shift;

    void resize(const Shared & shared, size_t size)
    {
        scores_shift.resize(size);
        scores.resize(shared.dim);
        for (Value value = 0; value < shared.dim; ++value) {
            scores[value].resize(size);
        }
    }

    void add_group (const Shared & shared, rng_t &)
    {
        scores_shift.packed_add(0);
        for (Value value = 0; value < shared.dim; ++value) {
            scores[value].packed_add(0);
        }
    }

    void remove_group (const Shared & shared, size_t groupid)
    {
        scores_shift.packed_remove(groupid);
        for (Value value = 0; value < shared.dim; ++value) {
            scores[value].packed_remove(groupid);
        }
    }

    void update_group (
            const Shared & shared,
            size_t groupid,
            const Group & group,
            rng_t & rng)
    {
        for (Value value = 0; value < shared.dim; ++value) {
            update_group(shared, groupid, group, value, rng);
        }
    }

    void update_group (
            const Shared & shared,
            size_t groupid,
            const Group & group,
            const Value & value,
            rng_t &)
    {
        scores[value][groupid] =
            fast_log(shared.alphas[value] + group.counts[value]);
        scores_shift[groupid] = fast_log(alpha_sum + group.count_sum);
    }

    void update_all (
            const Shared & shared,
            const MixtureSlave<Shared> & slave,
            rng_t &)
    {
        const size_t group_count = slave.groups().size();

        alpha_sum = 0;
        for (Value value = 0; value < shared.dim; ++value) {
            alpha_sum += shared.alphas[value];
        }
        for (size_t groupid = 0; groupid < group_count; ++groupid) {
            const Group & group = slave.groups()[groupid];
            for (Value value = 0; value < shared.dim; ++value) {
                scores[value][groupid] =
                    shared.alphas[value] + group.counts[value];
            }
            scores_shift[groupid] = alpha_sum + group.count_sum;
        }
        vector_log(group_count, scores_shift.data());
        for (Value value = 0; value < shared.dim; ++value) {
            vector_log(group_count, scores[value].data());
        }
    }

    void score_value(
            const Shared &,
            const Value & value,
            VectorFloat & scores_accum,
            rng_t &) const
    {
        vector_add_subtract(
            scores_accum.size(),
            scores_accum.data(),
            scores[value].data(),
            scores_shift.data());
    }
};

template<int max_dim>
class Mixture
{
public:

    typedef dirichlet_discrete::Value Value;
    typedef dirichlet_discrete::Shared<max_dim> Shared;
    typedef dirichlet_discrete::Group<max_dim> Group;
    typedef dirichlet_discrete::Scorer<max_dim> Scorer;
    typedef dirichlet_discrete::VectorizedScorer<max_dim> VectorizedScorer;

    VectorizedScorer scorer;

    std::vector<Group> & groups () { return slave_.groups(); }
    Group & groups (size_t i) { return slave_.groups(i); }
    const std::vector<Group> & groups () const { return slave_.groups(); }
    const Group & groups (size_t i) const { return slave_.groups(i); }

    void init (
            const Shared & shared,
            rng_t & rng)
    {
        slave_.init(shared, rng);
        scorer.resize(shared, slave_.groups().size());
        scorer.update_all(shared, slave_, rng);
    }

    void add_group (
            const Shared & shared,
            rng_t & rng)
    {
        const size_t groupid = slave_.groups().size();
        slave_.add_group(shared, rng);
        scorer.add_group(shared, rng);
        scorer.update_group(shared, groupid, groups()[groupid], rng);
    }

    void remove_group (
            const Shared & shared,
            size_t groupid)
    {
        slave_.remove_group(shared, groupid);
        scorer.remove_group(shared, groupid);
    }

    void add_value (
            const Shared & shared,
            size_t groupid,
            const Value & value,
            rng_t & rng)
    {
        DIST_ASSERT1(value < shared.dim, "value out of bounds: " << value);
        slave_.add_value(shared, groupid, value, rng);
        scorer.update_group(shared, groupid, groups()[groupid], value, rng);
    }

    void remove_value (
            const Shared & shared,
            size_t groupid,
            const Value & value,
            rng_t & rng)
    {
        DIST_ASSERT1(value < shared.dim, "value out of bounds: " << value);
        slave_.remove_value(shared, groupid, value, rng);
        scorer.update_group(shared, groupid, groups()[groupid], value, rng);
    }

    void score_value (
            const Shared & shared,
            const Value & value,
            VectorFloat & scores_accum,
            rng_t & rng) const
    {
        DIST_ASSERT1(value < shared.dim, "value out of bounds: " << value);
        if (DIST_DEBUG_LEVEL >= 2) {
            DIST_ASSERT_EQ(scores_accum.size(), slave_.groups().size());
        }
        scorer.score_value(shared, value, scores_accum, rng);
    }

    float score_mixture (
            const Shared & shared,
            rng_t & rng) const
    {
        return slave_.score_mixture(shared, rng);
    }

private:

    MixtureSlave<Shared> slave_;
};

template<int max_dim>
inline Value sample_value (
        const Shared<max_dim> & shared,
        const Group<max_dim> & group,
        rng_t & rng)
{
    Sampler<max_dim> sampler;
    sampler.init(shared, group, rng);
    return sampler.eval(shared, rng);
}

template<int max_dim>
inline float score_group (
        const Shared<max_dim> & shared,
        const Group<max_dim> & group,
        rng_t &)
{
    float alpha_sum = 0;
    float score = 0;

    for (Value value = 0; value < shared.dim; ++value) {
        float alpha = shared.alphas[value];
        alpha_sum += alpha;
        score += fast_lgamma(alpha + group.counts[value]) - fast_lgamma(alpha);
    }

    score += fast_lgamma(alpha_sum) - fast_lgamma(alpha_sum + group.count_sum);

    return score;
}

} // namespace dirichlet_discrete
} // namespace distributions
<|endoftext|>


<commit_before>#include <pybind11/embed.h>

#include <iostream>
#include <iterator>
#include <functional>

#include "term_window.h"
#include "draw_pane.h"

#include <wx/dcbuffer.h>
#include <wx/clipbrd.h>

#include "term_context.h"
#include "term_buffer.h"
#include "term_line.h"
#include "term_cell.h"
#include "term_network.h"
#include "color_theme.h"

void DrawPane::OnKeyDown(wxKeyEvent& event)
{
    wxChar uc = event.GetUnicodeKey();

    char c= uc & 0xFF;

    if (c >= 'A' && c <= 'Z' && !event.ShiftDown()) {
        c = c - 'A' + 'a';
    }

    TermContextPtr context = std::dynamic_pointer_cast<TermContext>(m_TermWindow->GetPluginContext());

    if (!context)
        return;

    TermNetworkPtr network = context->GetTermNetwork();

    std::function<void(const std::vector<unsigned char> & data)> send_data {
        [network](const std::vector<unsigned char> & data) {
            try
            {
                pybind11::gil_scoped_acquire acquire;

                network->Send(data, data.size());
            }
            catch(std::exception & e)
            {
                std::cerr << "!!Error Send:"
                          << std::endl
                          << e.what()
                          << std::endl;
                PyErr_Print();
            }
            catch(...)
            {
                std::cerr << "!!Error Send"
                          << std::endl;
                PyErr_Print();
            }
        }
    };

    std::vector<unsigned char> data;

    bool paste_data =
            (event.GetModifiers() == wxMOD_SHIFT && uc == WXK_NONE && event.GetKeyCode() == WXK_INSERT)
#ifdef __APPLE__
            || (event.GetModifiers() == wxMOD_CONTROL && uc == 'V')
#endif
            ;
    if (paste_data) {
        //paste from clipboard
        if (wxTheClipboard->Open()) {
            if (wxTheClipboard->IsSupported( wxDF_TEXT ) ||
                wxTheClipboard->IsSupported( wxDF_UNICODETEXT) ||
                wxTheClipboard->IsSupported( wxDF_OEMTEXT)) {
                wxTextDataObject txt_data;
                wxTheClipboard->GetData( txt_data );

                if (txt_data.GetTextLength() > 0) {
                    wxString s = txt_data.GetText();
                    const auto & s_buf = s.utf8_str();
                    const char * s_begin = s_buf;
                    const char * s_end = s_begin + s_buf.length();

                    std::copy(s_begin,
                              s_end,
                              std::back_inserter(data));

                    send_data(data);
                }
            }
            wxTheClipboard->Close();
        }

        return;
    }

    if (uc != WXK_NONE && (event.GetModifiers() & wxMOD_ALT)){
        data.push_back('\x1B');
    }

    if (m_AppDebug) {
        std::cout << "on key down:" << (int)c << ","
                  << event.AltDown() << ","
                  << event.RawControlDown()
                  << ","
                  << uc
                  << ","
                  << event.GetKeyCode()
                  << std::endl;
    }

    if (uc != WXK_NONE && (event.GetModifiers() & wxMOD_RAW_CONTROL)) {
        if (uc >= 'a' && uc <= 'z')
            data.push_back((char)(uc - 'a' + 1));
        if (uc >= 'A' && uc <= 'Z')
            data.push_back((char)(uc - 'A' + 1));
        else if (uc>= '[' && uc <= ']')
            data.push_back((char)(uc - '[' + 27));
        else if (uc == '6')
            data.push_back((char)('^' - '[' + 27));
        else if (uc == '-')
            data.push_back((char)('_' - '[' + 27));
    }

    if (uc == WXK_RETURN && !event.HasModifiers())
    {
        data.push_back((char)13);
    }

    if (data.size() == 0) {
        event.Skip();
        return;
    }

    //add char when there only ALT pressed
    if (data.size() == 1 && data[0] == '\x1B')
        data.push_back(c);

    send_data(data);
}

void DrawPane::OnKeyUp(wxKeyEvent& event)
{
    (void)event;
}

void DrawPane::OnChar(wxKeyEvent& event)
{
    wxChar uc = event.GetUnicodeKey();

    if (uc == WXK_NONE)
    {
        return;
    }

    TermContextPtr context = std::dynamic_pointer_cast<TermContext>(m_TermWindow->GetPluginContext());

    if (!context)
        return;

    TermNetworkPtr network = context->GetTermNetwork();

    std::vector<unsigned char> data;

    data.push_back(uc);

    try
    {
        pybind11::gil_scoped_acquire acquire;

        network->Send(data, data.size());
    }
    catch(std::exception & e)
    {
        std::cerr << "!!Error Send:"
                  << std::endl
                  << e.what()
                  << std::endl;
        PyErr_Print();
    }
    catch(...)
    {
        std::cerr << "!!Error Send"
                  << std::endl;
        PyErr_Print();
    }
}
<commit_msg>convert unicode to utf-8 before send out<commit_after>#include <pybind11/embed.h>

#include <iostream>
#include <iterator>
#include <functional>
#include <locale>
#include <codecvt>

#include "term_window.h"
#include "draw_pane.h"

#include <wx/dcbuffer.h>
#include <wx/clipbrd.h>

#include "term_context.h"
#include "term_buffer.h"
#include "term_line.h"
#include "term_cell.h"
#include "term_network.h"
#include "color_theme.h"

static
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> wcharconv;

void DrawPane::OnKeyDown(wxKeyEvent& event)
{
    wxChar uc = event.GetUnicodeKey();

    char c= uc & 0xFF;

    if (c >= 'A' && c <= 'Z' && !event.ShiftDown()) {
        c = c - 'A' + 'a';
    }

    TermContextPtr context = std::dynamic_pointer_cast<TermContext>(m_TermWindow->GetPluginContext());

    if (!context)
        return;

    TermNetworkPtr network = context->GetTermNetwork();

    std::function<void(const std::vector<unsigned char> & data)> send_data {
        [network](const std::vector<unsigned char> & data) {
            try
            {
                pybind11::gil_scoped_acquire acquire;

                network->Send(data, data.size());
            }
            catch(std::exception & e)
            {
                std::cerr << "!!Error Send:"
                          << std::endl
                          << e.what()
                          << std::endl;
                PyErr_Print();
            }
            catch(...)
            {
                std::cerr << "!!Error Send"
                          << std::endl;
                PyErr_Print();
            }
        }
    };

    std::vector<unsigned char> data;

    bool paste_data =
            (event.GetModifiers() == wxMOD_SHIFT && uc == WXK_NONE && event.GetKeyCode() == WXK_INSERT)
#ifdef __APPLE__
            || (event.GetModifiers() == wxMOD_CONTROL && uc == 'V')
#endif
            ;
    if (paste_data) {
        //paste from clipboard
        if (wxTheClipboard->Open()) {
            if (wxTheClipboard->IsSupported( wxDF_TEXT ) ||
                wxTheClipboard->IsSupported( wxDF_UNICODETEXT) ||
                wxTheClipboard->IsSupported( wxDF_OEMTEXT)) {
                wxTextDataObject txt_data;
                wxTheClipboard->GetData( txt_data );

                if (txt_data.GetTextLength() > 0) {
                    wxString s = txt_data.GetText();
                    const auto & s_buf = s.utf8_str();
                    const char * s_begin = s_buf;
                    const char * s_end = s_begin + s_buf.length();

                    std::copy(s_begin,
                              s_end,
                              std::back_inserter(data));

                    send_data(data);
                }
            }
            wxTheClipboard->Close();
        }

        return;
    }

    if (uc != WXK_NONE && (event.GetModifiers() & wxMOD_ALT)){
        data.push_back('\x1B');
    }

    if (m_AppDebug) {
        std::cout << "on key down:" << (int)c << ","
                  << event.AltDown() << ","
                  << event.RawControlDown()
                  << ","
                  << uc
                  << ","
                  << event.GetKeyCode()
                  << std::endl;
    }

    if (uc != WXK_NONE && (event.GetModifiers() & wxMOD_RAW_CONTROL)) {
        if (uc >= 'a' && uc <= 'z')
            data.push_back((char)(uc - 'a' + 1));
        if (uc >= 'A' && uc <= 'Z')
            data.push_back((char)(uc - 'A' + 1));
        else if (uc>= '[' && uc <= ']')
            data.push_back((char)(uc - '[' + 27));
        else if (uc == '6')
            data.push_back((char)('^' - '[' + 27));
        else if (uc == '-')
            data.push_back((char)('_' - '[' + 27));
    }

    if (uc == WXK_RETURN && !event.HasModifiers())
    {
        data.push_back((char)13);
    }

    if (data.size() == 0) {
        event.Skip();
        return;
    }

    //add char when there only ALT pressed
    if (data.size() == 1 && data[0] == '\x1B')
        data.push_back(c);

    send_data(data);
}

void DrawPane::OnKeyUp(wxKeyEvent& event)
{
    (void)event;
}

void DrawPane::OnChar(wxKeyEvent& event)
{
    wxChar uc = event.GetUnicodeKey();

    if (uc == WXK_NONE)
    {
        return;
    }

    TermContextPtr context = std::dynamic_pointer_cast<TermContext>(m_TermWindow->GetPluginContext());

    if (!context)
        return;

    TermNetworkPtr network = context->GetTermNetwork();

    std::vector<unsigned char> data;

    std::string bytes = wcharconv.to_bytes(uc);

    for(std::string::size_type i=0; i < bytes.length(); i++)
        data.push_back(bytes[i]);

    try
    {
        pybind11::gil_scoped_acquire acquire;

        network->Send(data, data.size());
    }
    catch(std::exception & e)
    {
        std::cerr << "!!Error Send:"
                  << std::endl
                  << e.what()
                  << std::endl;
        PyErr_Print();
    }
    catch(...)
    {
        std::cerr << "!!Error Send"
                  << std::endl;
        PyErr_Print();
    }
}
<|endoftext|>