<commit_before>#include "instream.h"
#include <errno.h>
#include <string.h>

Reader::Reader(FILE *in)
  : in(in), owner(false),fpos(0),pos(0),bad(0)
{
}

Reader::Reader(const char *file, const char *how)
  : in((FILE*)nullptr), owner(true),fpos(0),pos(0)
{
    open(file,how);
}

Reader::Reader(const std::string& file, const char *how)
  : in((FILE*)nullptr), owner(true),fpos(0),pos(0)
{
    open(file,how);
}

Reader::~Reader() {
  close();
}

void Reader::close() {
  if (owner && in != nullptr) {
     close_handle();
     in = nullptr;
  }
}

bool Reader::fail() {
    return bad != 0;
}

Reader::operator bool () {
  return ! fail();
}

std::string Reader::error() {
  return err_msg;
}

void Reader::set(FILE *new_in, bool own) {
  close();
  in = new_in;
  owner = own;
  pos = 0;
  bad = in == nullptr;
}

bool Reader::open(const std::string& file, const char *how) {
    in = fopen(file.c_str(),how);
    bad = errno;
    if (bad) {
        err_msg = strerror(errno);
    }
    return ! bad;
}

 void Reader::close_handle() {
    fclose(in);
}

 int Reader::read_fmt(const char *fmt, va_list ap) {
    return vfscanf(in,fmt,ap);
}

 char *Reader::read_raw_line(char *buff, int buffsize) {
    return fgets(buff,buffsize,in);
}

 size_t Reader::read(void *buff, int buffsize) {
  if (fail()) return 0;
  size_t sz = fread(buff,1,buffsize,in);
  ((char*)buff)[sz] = 0;
  return sz;
}

Reader& Reader::formatted_read(const char *ctype, const char *def, const char *fmt, ...) {
  if (fail()) return *this;
  va_list ap;
  va_start(ap,fmt);
  if (fmt == nullptr) {
     fmt = def;
  }
  int res = read_fmt(fmt,ap);
  int err = errno;
  if (res == EOF || err != 0) {
     err_msg = strerror(err);
     bad = errno;
  } else
  if (res != 1 && *ctype != 'S') {
     std::string chars;
     (*this)(chars,"%5s");
     err_msg = "error reading " + std::string(ctype) + " '" + std::string(fmt) + "' at  '" + chars + "'";
     bad = 1;
     //~ fprintf(stderr,"error for format '%s' at fpos %d %ld\n",fmt,pos,streampos());

  }
  pos += fpos;
  va_end(ap);
  return *this;
}

Reader& Reader::conversion_error(const char *kind, uint64_t val, bool was_unsigned) {
   bad = ERANGE;
   err_msg = "error converting " + std::string(kind) + " out of range ";
   if (was_unsigned) {
      err_msg += std::to_string(val);
   } else {
      err_msg += std::to_string((int64_t)val);
   }
   return *this;
}

long Reader::getpos() {
  return ftell(in);
}

 void Reader::setpos(long p, char end) {
  int whence = SEEK_SET;
  if (end == '$') {
     whence = SEEK_END;
  } else
  if (end == '.') {
     whence = SEEK_CUR;
  }
  fseek(in,p,whence);
}

int Reader::read_line(char *buff, int buffsize) {
  char *res = read_raw_line(buff,buffsize);
  if (res == nullptr) {
     bad = 1;
     err_msg = "EOF";
     return 0;
  }
  int sz = res != nullptr ? strlen(res) : 0;
  pos += sz;
  return sz;
}

bool Reader::readall (std::string& s) {
  char buff[512];
  size_t sz;
  s.clear();
  do {
     sz = read(buff,sizeof(buff));
     s.append(buff,sz);
  } while (sz == sizeof(buff));
  return errno != 0;
}

Reader& Reader::getfpos(int& p) {
  p = pos;
  return *this;
}

Reader& Reader::operator() (Reader::Error& err) {
  err.errcode = bad;
  err.msg = err_msg;
  err.pos = pos;
  return *this;
}

Reader& Reader::operator() (double &i,const char *fmt) {
  return formatted_read("double","%lf%n",fmt,&i,&fpos);
}

Reader& Reader::operator() (float &i,const char *fmt) {
  return formatted_read("double","%f%n",fmt,&i,&fpos);
}

Reader& Reader::operator() (int64_t &i,const char *fmt) {
  return formatted_read("int64","%" SCNd64 "%n",fmt,&i,&fpos);
}

Reader& Reader::operator() (uint64_t &i,const char *fmt) {
  return formatted_read("uint64","%" SCNu64 "%n",fmt,&i,&fpos);
}

Reader& Reader::operator() (int32_t &i,const char *fmt) {
   int64_t val;
   if (! (*this)(val)) return *this;
   if (val < INT32_MIN || val > INT32_MAX) return conversion_error("int32",val,false);
   i = (int16_t)val;
   return *this;
  //~ return formatted_read("int32","%" SCNd32 "%n",fmt,&i,&fpos);
}

Reader& Reader::operator() (uint32_t &i,const char *fmt) {
   uint64_t val;
   if (! (*this)(val)) return *this;
   if (val > UINT32_MAX) return conversion_error("uint32",val,true);
   i = (uint32_t)val;
   return *this;
  //~ return formatted_read("uint32","%" SCNu32 "%n",fmt,&i,&fpos);
}

Reader& Reader::operator() (int16_t &i,const char *fmt) {
   int64_t val;
   if (! (*this)(val)) return *this;
   if (val < INT16_MIN || val > INT16_MAX) return conversion_error("int16",val,false);
   i = (int16_t)val;
   return *this;
  //~ return formatted_read("int16","%" SCNd16 "%n",fmt,&i,&fpos);
}

Reader& Reader::operator() (uint16_t &i,const char *fmt) {
   uint64_t val;
   if (! (*this)(val)) return *this;
   if (val > UINT16_MAX) return conversion_error("uint16",val,true);
   i = (uint16_t)val;
   return *this;
  //~ return formatted_read("uint16","%" SCNu16 "%n",fmt,&i,&fpos);
}

Reader& Reader::operator() (char &i,const char *fmt) {
  return formatted_read("char","%c%n",fmt,&i,&fpos);
}

Reader& Reader::operator() (uint8_t &i,const char *fmt) {
   uint64_t val;
   if (! (*this)(val)) return *this;
   if (val > UINT8_MAX) return conversion_error("uchar",val,true);
   i = (uint8_t)val;
   return *this;
   //return formatted_read("uchar","%" SCNd8 "%n",fmt,&i,&fpos);
}

Reader& Reader::operator() (std::string &s,const char *fmt) {
  if (fail()) return *this;
  char buff[256];
  if (! formatted_read("string","%s%n",fmt,buff,&fpos)) return *this;
  s = buff;
  return *this;
}

Reader& Reader::operator() (const char *extra) {
  char buff[256];
  strcpy(buff,extra);
  strcat(buff,"%n");
  return formatted_read("S",extra,nullptr,&fpos);
}

Reader& Reader::operator() () {
  return skip();
}

Reader& Reader::getline(std::string& s) {
  if (fail()) return *this;
  char buff[256];
  int n = read_line(buff,sizeof(buff));
  if (n > 0) {
     buff[n-1] = 0; // trim \n
     s = buff;
  }
  return *this;
}

Reader& Reader::skip(int lines) {
  if (fail()) return *this;
  int i = 0;
  char buff[256];
  while (i < lines && read_line(buff,sizeof(buff)) > 0) {
     ++i;
  }
  return *this;
}

Reader ins(stdin);

CmdReader::CmdReader(std::string cmd, std::string extra)
: Reader((FILE*)nullptr) {
  std::string cmdline = cmd + " 2>&1 " + extra;
  set(popen(cmdline.c_str(),"r"),true);
}

bool CmdReader::operator == (std::string s) {
    return ((std::string)*this) == s;
}

bool CmdReader::operator != (std::string s) {
    return ((std::string)*this) != s;
}

 void CmdReader::close_handle() {
    pclose(in);
}

CmdReader::operator std::string () {
    std::string tmp;
    getline(tmp);
    return tmp;
}

StrReader::StrReader(const std::string& s) : Reader((FILE*)nullptr), pc(s.c_str()) {
    size = s.size();
}

StrReader::StrReader(const char* pc) : Reader((FILE*)nullptr), pc(pc) {
    size = strlen(pc);
}

int StrReader::read_fmt(const char *fmt, va_list ap) {
    if (pos >= size) {
        bad = 1; return 0;
    }
    return vsscanf(pc+pos,fmt,ap);
}

 char *StrReader::read_raw_line(char *buff, int buffsize) {
    char *P = buff;
    const char *Q = pc+pos;
    int i = 1;
    while (*Q != '\n' && *Q != 0) {
        *P++ = *Q++;
        ++i;
    }
    *P = 0;
    return buff;
}

long StrReader::getpos() {
    return pos;
}

void StrReader::setpos(long p, char end) {
    if (end == '^') {
        pos = p;
    } else
    if (end == '.') {
        pos += p;
    } else
    if (end == '$') {
        pos = size + p;
    }
}

<commit_msg>Reader::getlineinfo; EOF flag set properly<commit_after>#include "instream.h"
#include <errno.h>
#include <string.h>

const int line_size = 256;

Reader::Reader(FILE *in)
  : in(in), owner(false),fpos(0),pos(0),bad(0)
{
}

Reader::Reader(const char *file, const char *how)
  : in((FILE*)nullptr), owner(true),fpos(0),pos(0)
{
    open(file,how);
}

Reader::Reader(const std::string& file, const char *how)
  : in((FILE*)nullptr), owner(true),fpos(0),pos(0)
{
    open(file,how);
}

Reader::~Reader() {
  close();
}

void Reader::close() {
  if (owner && in != nullptr) {
     close_handle();
     in = nullptr;
  }
}

bool Reader::fail() {
    return bad != 0;
}

Reader::operator bool () {
  return ! fail();
}

int Reader::error_code() {
    return bad;
}

std::string Reader::error() {
  return err_msg;
}

void Reader::set(FILE *new_in, bool own) {
  close();
  in = new_in;
  owner = own;
  pos = 0;
  bad = in == nullptr;
}

bool Reader::open(const std::string& file, const char *how) {
    in = fopen(file.c_str(),how);
    bad = errno;
    if (bad) {
        err_msg = strerror(errno);
    }
    return ! bad;
}

void Reader::close_handle() {
    fclose(in);
}

int Reader::read_fmt(const char *fmt, va_list ap) {
    return vfscanf(in,fmt,ap);
}

char *Reader::read_raw_line(char *buff, int buffsize) {
    return fgets(buff,buffsize,in);
}

size_t Reader::read(void *buff, int buffsize) {
    if (fail()) return 0;
    size_t sz = fread(buff,1,buffsize,in);
    ((char*)buff)[sz] = 0;
    return sz;
}

Reader& Reader::formatted_read(const char *ctype, const char *def, const char *fmt, ...) {
    if (fail()) return *this;
    va_list ap;
    va_start(ap,fmt);
    if (fmt == nullptr) {
         fmt = def;
    }
    int res = read_fmt(fmt,ap);
    if (res == EOF) {
        if (errno != 0) { // we remain in hope
            err_msg = strerror(errno);
            bad = errno;
        } else { // but invariably it just means EOF
            err_msg = "EOF reading " + std::string(ctype);
            bad = EOF;
        }
    } else
    if (res != 1 && *ctype != 'S') {
         std::string chars;
         (*this)(chars,"%5s");
         err_msg = "error reading " + std::string(ctype) + " '" + std::string(fmt) + "' at  '" + chars + "'";
         bad = 1;
    }
    pos += fpos;
    va_end(ap);
    return *this;
}

Reader& Reader::conversion_error(const char *kind, uint64_t val, bool was_unsigned) {
   bad = ERANGE;
   err_msg = "error converting " + std::string(kind) + " out of range ";
   if (was_unsigned) {
      err_msg += std::to_string(val);
   } else {
      err_msg += std::to_string((int64_t)val);
   }
   return *this;
}

long Reader::getpos() {
  return ftell(in);
}

 void Reader::setpos(long p, char end) {
  int whence = SEEK_SET;
  if (end == '$') {
     whence = SEEK_END;
  } else
  if (end == '.') {
     whence = SEEK_CUR;
  }
  fseek(in,p,whence);
}

int Reader::read_line(char *buff, int buffsize) {
  char *res = read_raw_line(buff,buffsize);
  if (res == nullptr) {
     bad = 1;
     err_msg = "EOF";
     return 0;
  }
  int sz = res != nullptr ? strlen(res) : 0;
  pos += sz;
  return sz;
}

bool Reader::readall (std::string& s) {
  char buff[512];
  size_t sz;
  s.clear();
  do {
     sz = read(buff,sizeof(buff));
     s.append(buff,sz);
  } while (sz == sizeof(buff));
  return errno != 0;
}

Reader& Reader::getfpos(int& p) {
  p = pos;
  return *this;
}

Reader& Reader::operator() (Reader::Error& err) {
  err.errcode = bad;
  err.msg = err_msg;
  err.pos = pos;
  return *this;
}

Reader& Reader::operator() (double &i,const char *fmt) {
  return formatted_read("double","%lf%n",fmt,&i,&fpos);
}

Reader& Reader::operator() (float &i,const char *fmt) {
  return formatted_read("double","%f%n",fmt,&i,&fpos);
}

Reader& Reader::operator() (int64_t &i,const char *fmt) {
  return formatted_read("int64","%" SCNd64 "%n",fmt,&i,&fpos);
}

Reader& Reader::operator() (uint64_t &i,const char *fmt) {
  return formatted_read("uint64","%" SCNu64 "%n",fmt,&i,&fpos);
}

Reader& Reader::operator() (int32_t &i,const char *fmt) {
   int64_t val;
   if (! (*this)(val)) return *this;
   if (val < INT32_MIN || val > INT32_MAX) return conversion_error("int32",val,false);
   i = (int16_t)val;
   return *this;
  //~ return formatted_read("int32","%" SCNd32 "%n",fmt,&i,&fpos);
}

Reader& Reader::operator() (uint32_t &i,const char *fmt) {
   uint64_t val;
   if (! (*this)(val)) return *this;
   if (val > UINT32_MAX) return conversion_error("uint32",val,true);
   i = (uint32_t)val;
   return *this;
  //~ return formatted_read("uint32","%" SCNu32 "%n",fmt,&i,&fpos);
}

Reader& Reader::operator() (int16_t &i,const char *fmt) {
   int64_t val;
   if (! (*this)(val)) return *this;
   if (val < INT16_MIN || val > INT16_MAX) return conversion_error("int16",val,false);
   i = (int16_t)val;
   return *this;
  //~ return formatted_read("int16","%" SCNd16 "%n",fmt,&i,&fpos);
}

Reader& Reader::operator() (uint16_t &i,const char *fmt) {
   uint64_t val;
   if (! (*this)(val)) return *this;
   if (val > UINT16_MAX) return conversion_error("uint16",val,true);
   i = (uint16_t)val;
   return *this;
  //~ return formatted_read("uint16","%" SCNu16 "%n",fmt,&i,&fpos);
}

Reader& Reader::operator() (char &i,const char *fmt) {
  return formatted_read("char","%c%n",fmt,&i,&fpos);
}

Reader& Reader::operator() (uint8_t &i,const char *fmt) {
   uint64_t val;
   if (! (*this)(val)) return *this;
   if (val > UINT8_MAX) return conversion_error("uchar",val,true);
   i = (uint8_t)val;
   return *this;
   //return formatted_read("uchar","%" SCNd8 "%n",fmt,&i,&fpos);
}

Reader& Reader::operator() (std::string &s,const char *fmt) {
  if (fail()) return *this;
  char buff[line_size];
  if (! formatted_read("string","%s%n",fmt,buff,&fpos)) return *this;
  s = buff;
  return *this;
}

Reader& Reader::operator() (const char *extra) {
  char buff[line_size];
  strcpy(buff,extra);
  strcat(buff,"%n");
  return formatted_read("S",extra,nullptr,&fpos);
}

Reader& Reader::operator() () {
  return skip();
}

Reader& Reader::getline(std::string& s) {
  if (fail()) return *this;
  char buff[line_size];
  int n = read_line(buff,line_size);
  if (n > 0) {
     buff[n-1] = 0; // trim \n
     s = buff;
  }
  return *this;
}

Reader& Reader::skip(int lines) {
  if (fail()) return *this;
  int i = 0;
  char buff[line_size];
  while (i < lines && read_line(buff,line_size) > 0) {
     ++i;
  }
  return *this;
}

Reader::LineInfo Reader::getlineinfo (long p) {
    if (p == -1)
        p = pos;
    setpos(0,'^');
    pos = 0;
    char buff[line_size];
    int last_pos, lineno = 1;
    read_line(buff,line_size);
    while (pos < p) {
        lineno++;
        last_pos = pos;
        read_line(buff,line_size);
    }
    int col = p - last_pos;
    setpos(p,'^');
    return {lineno, col};
}

Reader ins(stdin);

CmdReader::CmdReader(std::string cmd, std::string extra)
: Reader((FILE*)nullptr) {
  std::string cmdline = cmd + " 2>&1 " + extra;
  set(popen(cmdline.c_str(),"r"),true);
}

bool CmdReader::operator == (std::string s) {
    return ((std::string)*this) == s;
}

bool CmdReader::operator != (std::string s) {
    return ((std::string)*this) != s;
}

 void CmdReader::close_handle() {
    pclose(in);
}

CmdReader::operator std::string () {
    std::string tmp;
    getline(tmp);
    return tmp;
}

StrReader::StrReader(const std::string& s) : Reader((FILE*)nullptr), pc(s.c_str()) {
    size = s.size();
}

StrReader::StrReader(const char* pc) : Reader((FILE*)nullptr), pc(pc) {
    size = strlen(pc);
}

int StrReader::read_fmt(const char *fmt, va_list ap) {
    if (pos >= size) {
        bad = 1; return 0;
    }
    return vsscanf(pc+pos,fmt,ap);
}

 char *StrReader::read_raw_line(char *buff, int buffsize) {
    char *P = buff;
    const char *Q = pc+pos;
    int i = 1;
    while (*Q != '\n' && *Q != 0) {
        *P++ = *Q++;
        ++i;
    }
    *P = 0;
    return buff;
}

long StrReader::getpos() {
    return pos;
}

void StrReader::setpos(long p, char end) {
    if (end == '^') {
        pos = p;
    } else
    if (end == '.') {
        pos += p;
    } else
    if (end == '$') {
        pos = size + p;
    }
}

<|endoftext|>


<commit_before>/*************************************************************************
 *
 *  $RCSfile: ODriver.cxx,v $
 *
 *  $Revision: 1.4 $
 *
 *  last change: $Author: oj $ $Date: 2001-05-15 08:18:13 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 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
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (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.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

#ifndef _CONNECTIVITY_ODBC_ODRIVER_HXX_
#include "odbc/ODriver.hxx"
#endif
#ifndef _CONNECTIVITY_ODBC_OCONNECTION_HXX_
#include "odbc/OConnection.hxx"
#endif
#ifndef _CONNECTIVITY_ODBC_OFUNCTIONS_HXX_
#include "odbc/OFunctions.hxx"
#endif
#include "odbc/OTools.hxx"

using namespace connectivity::odbc;
using namespace com::sun::star::uno;
using namespace com::sun::star::lang;
using namespace com::sun::star::beans;
using namespace com::sun::star::sdbc;
// --------------------------------------------------------------------------------
ODBCDriver::ODBCDriver()
    : ODriver_BASE(m_aMutex)
    ,m_pDriverHandle(SQL_NULL_HANDLE)
{
}
// --------------------------------------------------------------------------------
void ODBCDriver::disposing()
{
    ::osl::MutexGuard aGuard(m_aMutex);

    for (OWeakRefArray::iterator i = m_xConnections.begin(); m_xConnections.end() != i; ++i)
    {
        Reference< XComponent > xComp(i->get(), UNO_QUERY);
        if (xComp.is())
            xComp->dispose();
    }
    m_xConnections.clear();

    ODriver_BASE::disposing();
}

// static ServiceInfo
//------------------------------------------------------------------------------
rtl::OUString ODBCDriver::getImplementationName_Static(  ) throw(RuntimeException)
{
    return rtl::OUString::createFromAscii("com.sun.star.sdbc.ODBCDriver");
}

typedef Sequence< ::rtl::OUString > SS;
//------------------------------------------------------------------------------
SS ODBCDriver::getSupportedServiceNames_Static(  ) throw (RuntimeException)
{
    SS aSNS( 1 );
    aSNS[0] = ::rtl::OUString::createFromAscii("com.sun.star.sdbc.Driver");
    return aSNS;
}

//------------------------------------------------------------------
::rtl::OUString SAL_CALL ODBCDriver::getImplementationName(  ) throw(RuntimeException)
{
    return getImplementationName_Static();
}

//------------------------------------------------------------------
sal_Bool SAL_CALL ODBCDriver::supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException)
{
    SS aSupported(getSupportedServiceNames());
    const ::rtl::OUString* pSupported = aSupported.getConstArray();
    const ::rtl::OUString* pEnd = pSupported + aSupported.getLength();
    for (;pSupported != pEnd && !pSupported->equals(_rServiceName); ++pSupported)
        ;

    return pSupported != pEnd;
}

//------------------------------------------------------------------
SS SAL_CALL ODBCDriver::getSupportedServiceNames(  ) throw(RuntimeException)
{
    return getSupportedServiceNames_Static();
}

// --------------------------------------------------------------------------------
Reference< XConnection > SAL_CALL ODBCDriver::connect( const ::rtl::OUString& url, const Sequence< PropertyValue >& info ) throw(SQLException, RuntimeException)
{
    if(!m_pDriverHandle)
    {
        ::rtl::OUString aPath;
        if(!EnvironmentHandle(aPath))
            throw SQLException(aPath,*this,::rtl::OUString(),1000,Any());
    }
    OConnection* pCon = new OConnection(m_pDriverHandle,this);
    pCon->Construct(url,info);
    Reference< XConnection > xCon = NULL;
    m_xConnections.push_back(WeakReferenceHelper(*pCon));

    return xCon;
}
// --------------------------------------------------------------------------------
sal_Bool SAL_CALL ODBCDriver::acceptsURL( const ::rtl::OUString& url )
        throw(SQLException, RuntimeException)
{
    return (!url.compareTo(::rtl::OUString::createFromAscii("sdbc:odbc:"),10));
}
// --------------------------------------------------------------------------------
Sequence< DriverPropertyInfo > SAL_CALL ODBCDriver::getPropertyInfo( const ::rtl::OUString& url, const Sequence< PropertyValue >& info ) throw(SQLException, RuntimeException)
{
    return Sequence< DriverPropertyInfo >();
}
// --------------------------------------------------------------------------------
sal_Int32 SAL_CALL ODBCDriver::getMajorVersion(  ) throw(RuntimeException)
{
    return 1;
}
// --------------------------------------------------------------------------------
sal_Int32 SAL_CALL ODBCDriver::getMinorVersion(  ) throw(RuntimeException)
{
    return 0;
}
// --------------------------------------------------------------------------------
//-----------------------------------------------------------------------------

<commit_msg>#86528# template changes<commit_after>/*************************************************************************
 *
 *  $RCSfile: ODriver.cxx,v $
 *
 *  $Revision: 1.5 $
 *
 *  last change: $Author: oj $ $Date: 2001-05-21 11:01:07 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 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
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (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.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

#ifndef _CONNECTIVITY_ODBC_ODRIVER_HXX_
#include "odbc/ODriver.hxx"
#endif
#ifndef _CONNECTIVITY_ODBC_OCONNECTION_HXX_
#include "odbc/OConnection.hxx"
#endif
#ifndef _CONNECTIVITY_ODBC_OFUNCTIONS_HXX_
#include "odbc/OFunctions.hxx"
#endif
#include "odbc/OTools.hxx"

using namespace connectivity::odbc;
using namespace com::sun::star::uno;
using namespace com::sun::star::lang;
using namespace com::sun::star::beans;
using namespace com::sun::star::sdbc;
// --------------------------------------------------------------------------------
ODBCDriver::ODBCDriver()
    : ODriver_BASE(m_aMutex)
    ,m_pDriverHandle(SQL_NULL_HANDLE)
{
}
// --------------------------------------------------------------------------------
void ODBCDriver::disposing()
{
    ::osl::MutexGuard aGuard(m_aMutex);

    for (OWeakRefArray::iterator i = m_xConnections.begin(); m_xConnections.end() != i; ++i)
    {
        Reference< XComponent > xComp(i->get(), UNO_QUERY);
        if (xComp.is())
            xComp->dispose();
    }
    m_xConnections.clear();

    ODriver_BASE::disposing();
}

// static ServiceInfo
//------------------------------------------------------------------------------
rtl::OUString ODBCDriver::getImplementationName_Static(  ) throw(RuntimeException)
{
    return rtl::OUString::createFromAscii("com.sun.star.sdbc.ODBCDriver");
}

typedef Sequence< ::rtl::OUString > SS;
//------------------------------------------------------------------------------
SS ODBCDriver::getSupportedServiceNames_Static(  ) throw (RuntimeException)
{
    SS aSNS( 1 );
    aSNS[0] = ::rtl::OUString::createFromAscii("com.sun.star.sdbc.Driver");
    return aSNS;
}

//------------------------------------------------------------------
::rtl::OUString SAL_CALL ODBCDriver::getImplementationName(  ) throw(RuntimeException)
{
    return getImplementationName_Static();
}

//------------------------------------------------------------------
sal_Bool SAL_CALL ODBCDriver::supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException)
{
    SS aSupported(getSupportedServiceNames());
    const ::rtl::OUString* pSupported = aSupported.getConstArray();
    const ::rtl::OUString* pEnd = pSupported + aSupported.getLength();
    for (;pSupported != pEnd && !pSupported->equals(_rServiceName); ++pSupported)
        ;

    return pSupported != pEnd;
}

//------------------------------------------------------------------
SS SAL_CALL ODBCDriver::getSupportedServiceNames(  ) throw(RuntimeException)
{
    return getSupportedServiceNames_Static();
}

// --------------------------------------------------------------------------------
Reference< XConnection > SAL_CALL ODBCDriver::connect( const ::rtl::OUString& url, const Sequence< PropertyValue >& info ) throw(SQLException, RuntimeException)
{
    if(!m_pDriverHandle)
    {
        ::rtl::OUString aPath;
        if(!EnvironmentHandle(aPath))
            throw SQLException(aPath,*this,::rtl::OUString(),1000,Any());
    }
    OConnection* pCon = new OConnection(m_pDriverHandle,this);
    Reference< XConnection > xCon = pCon;
    pCon->Construct(url,info);
    m_xConnections.push_back(WeakReferenceHelper(*pCon));

    return xCon;
}
// --------------------------------------------------------------------------------
sal_Bool SAL_CALL ODBCDriver::acceptsURL( const ::rtl::OUString& url )
        throw(SQLException, RuntimeException)
{
    return (!url.compareTo(::rtl::OUString::createFromAscii("sdbc:odbc:"),10));
}
// --------------------------------------------------------------------------------
Sequence< DriverPropertyInfo > SAL_CALL ODBCDriver::getPropertyInfo( const ::rtl::OUString& url, const Sequence< PropertyValue >& info ) throw(SQLException, RuntimeException)
{
    return Sequence< DriverPropertyInfo >();
}
// --------------------------------------------------------------------------------
sal_Int32 SAL_CALL ODBCDriver::getMajorVersion(  ) throw(RuntimeException)
{
    return 1;
}
// --------------------------------------------------------------------------------
sal_Int32 SAL_CALL ODBCDriver::getMinorVersion(  ) throw(RuntimeException)
{
    return 0;
}
// --------------------------------------------------------------------------------
//-----------------------------------------------------------------------------

<|endoftext|>


<commit_before>#pragma once
#include "Interfaces\IInitializable.hpp"
#include "Utils\Hook.hpp"
#include "Utils\Patch.hpp"
#include "Utils\Singleton.hpp"
#include "Module.hpp"
#include "Blam\Math\RealPoint3D.hpp"
#include "Blam\Math\RealVector2D.hpp"
#include "Blam\Math\RealVector3D.hpp"

namespace AnvilEldorado
{
	using AnvilCommon::Utils::Hook;
	using AnvilCommon::Utils::Patch;

	struct CameraGlobals
	{
		size_t UpdatePerspectiveFunction;
		Blam::Math::RealPoint3D Position;
		Blam::Math::RealVector3D PositionShift;
		Blam::Math::RealVector2D LookShift;
		float Depth;
		float FieldOfView;
		Blam::Math::RealVector3D Forward;
		Blam::Math::RealVector2D Up;
	};

	class Camera final : public Module, public AnvilCommon::IInitializable, public AnvilCommon::Singleton<Camera>
	{
	public:
		Command* VarCameraCrosshair;
		Command* VarCameraFov;
		Command* VarCameraHideHud;
		Command* VarCameraMode;
		Command* VarCameraSpeed;
		Command* VarCameraSave;
		Command* VarCameraLoad;
		Command* VarCameraPosition;

		// patches to stop camera mode from changing
		Patch Debug1CameraPatch;
		Patch Debug2CameraPatch;
		Patch ThirdPersonPatch;
		Patch FirstPersonPatch;
		Patch DeadPersonPatch;

		Patch StaticILookVectorPatch;
		Patch StaticKLookVectorPatch;

		Patch HideHudPatch;
		Patch CenteredCrosshairPatch;

		Hook CameraPermissionHook;
		Hook CameraPermissionHookAlt1;
		Hook CameraPermissionHookAlt2;
		Hook CameraPermissionHookAlt3;

		static CameraGlobals *GetCameraGlobals();

		Camera();

		bool Init() override;

		void UpdatePosition();
	};
}<commit_msg>Added padding to CameraGlobals in AnvilEldorado<commit_after>#pragma once
#include "Interfaces\IInitializable.hpp"
#include "Utils\Hook.hpp"
#include "Utils\Patch.hpp"
#include "Utils\Singleton.hpp"
#include "Module.hpp"
#include "Blam\Math\RealPoint3D.hpp"
#include "Blam\Math\RealVector2D.hpp"
#include "Blam\Math\RealVector3D.hpp"

namespace AnvilEldorado
{
	using AnvilCommon::Utils::Hook;
	using AnvilCommon::Utils::Patch;

	struct CameraGlobals
	{
		size_t UpdatePerspectiveFunction;
		uint8_t Unknown[0x830];
		Blam::Math::RealPoint3D Position;
		Blam::Math::RealVector3D PositionShift;
		Blam::Math::RealVector2D LookShift;
		float Depth;
		float FieldOfView;
		Blam::Math::RealVector3D Forward;
		Blam::Math::RealVector2D Up;
	};

	class Camera final : public Module, public AnvilCommon::IInitializable, public AnvilCommon::Singleton<Camera>
	{
	public:
		Command* VarCameraCrosshair;
		Command* VarCameraFov;
		Command* VarCameraHideHud;
		Command* VarCameraMode;
		Command* VarCameraSpeed;
		Command* VarCameraSave;
		Command* VarCameraLoad;
		Command* VarCameraPosition;

		// patches to stop camera mode from changing
		Patch Debug1CameraPatch;
		Patch Debug2CameraPatch;
		Patch ThirdPersonPatch;
		Patch FirstPersonPatch;
		Patch DeadPersonPatch;

		Patch StaticILookVectorPatch;
		Patch StaticKLookVectorPatch;

		Patch HideHudPatch;
		Patch CenteredCrosshairPatch;

		Hook CameraPermissionHook;
		Hook CameraPermissionHookAlt1;
		Hook CameraPermissionHookAlt2;
		Hook CameraPermissionHookAlt3;

		static CameraGlobals *GetCameraGlobals();

		Camera();

		bool Init() override;

		void UpdatePosition();
	};
}<|endoftext|>


<commit_before>#include <iostream>
#include <fstream>
//#include <codecvt>
//#include <cstddef>
#include <locale>
#include <string>
#include <iomanip>

#include "cp-neural.h"

using std::wcout; using std::wstring;
using std::cerr; using std::vector;

class Text {
private:
    bool isinit=false;
    bool readDataFile(string inputfile) {
        std::wifstream txtfile(inputfile, std::ios::binary);
        if (!txtfile.is_open()) {
            return false;
        }
        txtfile.imbue(std::locale("en_US.UTF8"));
        wstring ttext((std::istreambuf_iterator<wchar_t>(txtfile)),
                     std::istreambuf_iterator<wchar_t>());
        txtfile.close();
        text=ttext;
        filename=inputfile;
        return true;
    }
public:
    wstring text;
    string filename;
    std::map<wchar_t,int> freq;
    std::map<wchar_t,int> w2v;
    std::map<int,wchar_t> v2w;
    Text(string filename) {
        if (readDataFile(filename)) {
            for (auto wc : text) {
                freq[wc]++;
            }
            int it=0;
            for (auto wc : freq) {
                w2v[wc.first]=it;
                v2w[it]=wc.first;
                ++it;
            }
            isinit=true;
            cerr << "Freq:" << freq.size() << ", w2v:" << w2v.size() << ", v2w:" << v2w.size() << endl;
        }
    }
    ~Text() {

    }
    bool isInit() {
        return isinit;
    }
    int vocsize() {
        if (!isinit) return 0;
        return v2w.size();
    }
};

int main(int argc, char *argv[]) {
    std::setlocale (LC_ALL, "");
    wcout << L"Rnn-Readär" << std::endl;

    bool allOk=true;
    if (argc!=2) {
        cerr << "rnnreader <path-to-text-file>" << endl;
        exit(-1);
    }
    Text txt(argv[1]);
    if (!txt.isInit()) {
        cerr << "Cannot initialize Text object from inputfile: " << argv[1] << endl;
        exit(-1);
    }

    wcout << L"Text size: " << txt.text.size() << endl;

    wcout << L"Size of vocabulary:" << txt.freq.size() << std::endl;
/*    for (auto f : txt.freq) {
        int c=(int)f.first;
        wstring wc(1,f.first);
        wcout << wc << L"|" <<  wchar_t(f.first) << L"(0x" << std::hex << c << L")" ": " << std::dec <<  f.second << endl;
    }
*/
    Color::Modifier red(Color::FG_RED);
    Color::Modifier green(Color::FG_GREEN);
    Color::Modifier def(Color::FG_DEFAULT);

    int T=24;
    int N=txt.text.size()-T-1;

    MatrixN Xr(N,T);
    MatrixN yr(N,T);

    wstring chunk,chunky;
    int n=0;
    for (int i=0; i<N-T-1; i+=T) {
        chunk=txt.text.substr(i,T);
        chunky=txt.text.substr(i+1,T);
        for (int t=0; t<T; t++) {
            Xr(i,t)=txt.w2v[chunk[t]];
            yr(i,t)=txt.w2v[chunky[t]];
            ++n;
        }
    }

    int maxN = 50000;
    if (n>maxN) n=maxN;
    int n1=n*0.9;
    int dn=(n-n1)/2;

    cerr << n1 << " datasets" << endl;

/*    MatrixN X(n1,T);
    MatrixN y(n1,T);
    MatrixN Xv(dn,T);
    MatrixN yv(dn,T);
    MatrixN Xt(dn,T);
    MatrixN yt(dn,T);
*/
    MatrixN X=Xr.block(0,0,n1,T);
    MatrixN y=yr.block(0,0,n1,T);
    MatrixN Xv=Xr.block(n1,0,dn,T);
    MatrixN yv=yr.block(n1,0,dn,T);
    MatrixN Xt=Xr.block(n1+dn,0,dn,T);
    MatrixN yt=yr.block(n1+dn,0,dn,T);
/*

    X=Xr.block(0,0,100000,T);
    y=yr.block(0,0,100000,T);
    Xv=Xr.block(100000,0,10000,T);
    yv=yr.block(100000,0,10000,T);
    Xt=Xr.block(110000,0,10000,T);
    yt=yr.block(110000,0,10000,T);
*/
/*
    X=Xr.block(0,0,10000,T);
    y=yr.block(0,0,10000,T);
    Xv=Xr.block(10000,0,1000,T);
    yv=yr.block(10000,0,1000,T);
    Xt=Xr.block(11000,0,1000,T);
    yt=yr.block(11000,0,1000,T);
*/
    cpInitCompute("Rnnreader");
    registerLayers();

    LayerBlock lb("{name='rnnreader';init='orthonormal';initfactor=0.001}");
    int VS=txt.vocsize();
    int H=64;
    int D=128;
    int BS=64;
    float clip=5.0;

    CpParams cp0;
    cp0.setPar("inputShape",vector<int>{T});
    cp0.setPar("V",VS);
    cp0.setPar("D",D);
    lb.addLayer("WordEmbedding","WE0",cp0,{"input"});

    CpParams cp1;
    cp1.setPar("inputShape",vector<int>{D,T});
    cp1.setPar("N",BS);
    cp1.setPar("H",H);
    cp1.setPar("clip",clip);
    lb.addLayer("RNN","rnn1",cp1,{"WE0"});

    CpParams cp2;
    cp2.setPar("inputShape",vector<int>{H,T});
    cp2.setPar("N",BS);
    cp2.setPar("H",H);
    cp2.setPar("clip",clip);
    lb.addLayer("RNN","rnn2",cp2,{"rnn1"});

    CpParams cp3;
    cp3.setPar("inputShape",vector<int>{H,T});
    cp3.setPar("N",BS);
    cp3.setPar("H",H);
    cp3.setPar("clip",clip);
    lb.addLayer("RNN","rnn3",cp3,{"rnn2"});

    CpParams cp10;
    cp10.setPar("inputShape",vector<int>{H,T});
    //cp10.setPar("T",T);
    //cp10.setPar("D",H);
    cp10.setPar("M",VS);
    lb.addLayer("TemporalAffine","af1",cp10,{"rnn3"});

    CpParams cp11;
    cp11.setPar("inputShape",vector<int>{VS,T});
    lb.addLayer("TemporalSoftmax","sm1",cp11,{"af1"});

    if (!lb.checkTopology(true)) {
        allOk=false;
        cerr << red << "Topology-check for LayerBlock: ERROR." << def << endl;
    } else {
        cerr << green << "Topology-check for LayerBlock: ok." << def << endl;
    }

/*    wstring chunk;
    chunk = txt.text.substr(512,128);
    wcout << chunk << endl;
*/
    CpParams cpo("{verbose=true;epsilon=1e-8}");
    // CpParams cpo("{verbose=false;epsilon=1e-8}");
    cpo.setPar("learning_rate", (floatN)1e-2); //2.2e-2);
    cpo.setPar("lr_decay", (floatN)0.95);
    //cpo.setPar("regularization", (floatN)1.);

    floatN dep=1.0;
    floatN sep=0.0;
    cpo.setPar("epochs",(floatN)dep);
    cpo.setPar("batch_size",BS);

    MatrixN xg(1,T);
    MatrixN xg2(1,T);
    wstring sg;
    for (int i=0; i<10000; i++) {

        cpo.setPar("startepoch", (floatN)sep);
        cpo.setPar("maxthreads", (int)1); // RNN can't cope with threads.

        Layer *prnn1=lb.layerMap["rnn1"];
        prnn1->params["ho"]->setZero();
        /*Layer *prnn2=lb.layerMap["rnn2"];
        prnn2->params["ho"]->setZero();
        Layer *prnn3=lb.layerMap["rnn3"];
        prnn3->params["ho"]->setZero();
*/
        floatN cAcc=lb.train(X, y, Xv, yv, "Adam", cpo);
        sep+=dep;

        int pos=rand() % 1000 + 5000;
        wstring instr=txt.text.substr(pos,T);

        for (int i=0; i<T; i++) {
            if (i<instr.size()) {
                xg(0,i)=txt.w2v[instr[i]];
            } else {
                xg(0,i)=txt.w2v[L' '];
            }
        }
        /*
        for (auto wc : instr) {
            sg[0]=wc;
            xg(0,0)=txt.w2v[sg[0]];
            MatrixN z(0,0);
            MatrixN yg=lb.forward(xg,z,nullptr);
        }
        */
        //xg(0,0)=txt.w2v[sg[0]];
        for (int rep=0; rep<3; rep++) {
            //Layer *prnn=lb.layerMap["rnn1"];
            //prnn->params["ho"]->setZero();
            cerr << "------------" << rep << "--------------------------" << endl;
            for (int g=0; g<200; g++) {
                //wcout << g << L">";
                MatrixN z(0,0);

                //for (int i; i<xg.cols(); i++) wcout << txt.v2w[xg(0,i)];
                //wcout << L"<" << endl << L">";
                MatrixN probst=lb.forward(xg,z,nullptr);
                MatrixN probsd=MatrixN(N*T,VS);
                for (int n=0; n<1; n++) {
                    for (int t=0; t<T; t++) {
                        for (int d=0; d<VS; d++) {
                            probsd(n*T+t,d)=probst(n,t*VS+d);
                        }
                    }
                }

                for (int t=0; t<T; t++) {
                    vector<floatN> probs(VS);
                    vector<floatN> index(VS);
                    for (int d=0; d<VS; d++) {
                        probs[d]=probsd(0*T+t,d);
                        index[d]=d;
                    }
                    int ind=(int)index[randomChoice(index, probs)];
/*                    float mx=-1000.0;
                    int ind=-1;
                    for (int d=0; d<VS; d++) {
                        floatN p=yg(0,t*D+d);
                        floatN pr=p*((floatN)(rand()%100)/5000.0+0.98);
                        if (pr>mx) {
                            mx=pr; // yg(0,t*D+d);
                            ind=d;
                        }
                    }
*/
                    wchar_t cw=txt.v2w[ind];
                    //if (t==0) wcout << L"[" << cw << L"<";
                    //wcout << L"<" << cw << L">";
                    if (t==0) wcout << cw;
                    xg2(0,t)=ind;
                }
                //wcout << L"<" << endl;

                //for (int t=T-1; t>0; t--) xg(0,t)=xg(0,t-1);
                for (int t=0; t< T-1; t++) xg(0,t)=xg(0,t+1);
                xg(0,0)=xg2(0,0);

                //xg=xg2;
            }
            wcout << endl;
        }
    }
    /*
    floatN train_err, val_err, test_err;
    bool evalFinal=true;
    if (evalFinal) {
        train_err=lb.test(X, y, cpo.getPar("batch_size", 50));
        val_err=lb.test(Xv, yv, cpo.getPar("batch_size", 50));
        test_err=lb.test(Xt, yt, cpo.getPar("batch_size", 50));

        cerr << "Final results on RnnReader after " << cpo.getPar("epochs",(floatN)0.0) << " epochs:" << endl;
        cerr << "      Train-error: " << train_err << " train-acc: " << 1.0-train_err << endl;
        cerr << " Validation-error: " << val_err <<   "   val-acc: " << 1.0-val_err << endl;
        cerr << "       Test-error: " << test_err <<  "  test-acc: " << 1.0-test_err << endl;
    }
    //return cAcc;
    */
    cpExitCompute();
}
<commit_msg>stage<commit_after>#include <iostream>
#include <fstream>
//#include <codecvt>
//#include <cstddef>
#include <locale>
#include <string>
#include <iomanip>

#include "cp-neural.h"

using std::wcout; using std::wstring;
using std::cerr; using std::vector;

class Text {
private:
    bool isinit=false;
    bool readDataFile(string inputfile) {
        std::wifstream txtfile(inputfile, std::ios::binary);
        if (!txtfile.is_open()) {
            return false;
        }
        txtfile.imbue(std::locale("en_US.UTF8"));
        wstring ttext((std::istreambuf_iterator<wchar_t>(txtfile)),
                     std::istreambuf_iterator<wchar_t>());
        txtfile.close();
        text=ttext;
        filename=inputfile;
        return true;
    }
public:
    wstring text;
    string filename;
    std::map<wchar_t,int> freq;
    std::map<wchar_t,int> w2v;
    std::map<int,wchar_t> v2w;
    Text(string filename) {
        if (readDataFile(filename)) {
            for (auto wc : text) {
                freq[wc]++;
            }
            int it=0;
            for (auto wc : freq) {
                w2v[wc.first]=it;
                v2w[it]=wc.first;
                ++it;
            }
            isinit=true;
            cerr << "Freq:" << freq.size() << ", w2v:" << w2v.size() << ", v2w:" << v2w.size() << endl;
        }
    }
    ~Text() {

    }
    bool isInit() {
        return isinit;
    }
    int vocsize() {
        if (!isinit) return 0;
        return v2w.size();
    }
};

int main(int argc, char *argv[]) {
    std::setlocale (LC_ALL, "");
    wcout << L"Rnn-Readär" << std::endl;

    bool allOk=true;
    if (argc!=2) {
        cerr << "rnnreader <path-to-text-file>" << endl;
        exit(-1);
    }
    Text txt(argv[1]);
    if (!txt.isInit()) {
        cerr << "Cannot initialize Text object from inputfile: " << argv[1] << endl;
        exit(-1);
    }

    wcout << L"Text size: " << txt.text.size() << endl;

    wcout << L"Size of vocabulary:" << txt.freq.size() << std::endl;
/*    for (auto f : txt.freq) {
        int c=(int)f.first;
        wstring wc(1,f.first);
        wcout << wc << L"|" <<  wchar_t(f.first) << L"(0x" << std::hex << c << L")" ": " << std::dec <<  f.second << endl;
    }
*/
    Color::Modifier red(Color::FG_RED);
    Color::Modifier green(Color::FG_GREEN);
    Color::Modifier def(Color::FG_DEFAULT);

    int T=24;
    int N=txt.text.size()-T-1;

    MatrixN Xr(N,T);
    MatrixN yr(N,T);

    wstring chunk,chunky;
    int n=0;
    for (int i=0; i<N-T-1; i+=T) {
        chunk=txt.text.substr(i,T);
        chunky=txt.text.substr(i+1,T);
        for (int t=0; t<T; t++) {
            Xr(i,t)=txt.w2v[chunk[t]];
            yr(i,t)=txt.w2v[chunky[t]];
            ++n;
        }
    }

    int maxN = 50000;
    if (n>maxN) n=maxN;
    int n1=n*0.9;
    int dn=(n-n1)/2;

    cerr << n1 << " datasets" << endl;

/*    MatrixN X(n1,T);
    MatrixN y(n1,T);
    MatrixN Xv(dn,T);
    MatrixN yv(dn,T);
    MatrixN Xt(dn,T);
    MatrixN yt(dn,T);
*/
    MatrixN X=Xr.block(0,0,n1,T);
    MatrixN y=yr.block(0,0,n1,T);
    MatrixN Xv=Xr.block(n1,0,dn,T);
    MatrixN yv=yr.block(n1,0,dn,T);
    MatrixN Xt=Xr.block(n1+dn,0,dn,T);
    MatrixN yt=yr.block(n1+dn,0,dn,T);
/*

    X=Xr.block(0,0,100000,T);
    y=yr.block(0,0,100000,T);
    Xv=Xr.block(100000,0,10000,T);
    yv=yr.block(100000,0,10000,T);
    Xt=Xr.block(110000,0,10000,T);
    yt=yr.block(110000,0,10000,T);
*/
/*
    X=Xr.block(0,0,10000,T);
    y=yr.block(0,0,10000,T);
    Xv=Xr.block(10000,0,1000,T);
    yv=yr.block(10000,0,1000,T);
    Xt=Xr.block(11000,0,1000,T);
    yt=yr.block(11000,0,1000,T);
*/
    cpInitCompute("Rnnreader");
    registerLayers();

    LayerBlock lb("{name='rnnreader';init='orthonormal';initfactor=0.001}");
    int VS=txt.vocsize();
    int H=64;
    int D=128;
    int BS=64;
    float clip=5.0;

    CpParams cp0;
    cp0.setPar("inputShape",vector<int>{T});
    cp0.setPar("V",VS);
    cp0.setPar("D",D);
    lb.addLayer("WordEmbedding","WE0",cp0,{"input"});

    CpParams cp1;
    cp1.setPar("inputShape",vector<int>{D,T});
    cp1.setPar("N",BS);
    cp1.setPar("H",H);
    cp1.setPar("clip",clip);
    lb.addLayer("RNN","rnn1",cp1,{"WE0"});

    CpParams cp2;
    cp2.setPar("inputShape",vector<int>{H,T});
    cp2.setPar("N",BS);
    cp2.setPar("H",H);
    cp2.setPar("clip",clip);
    lb.addLayer("RNN","rnn2",cp2,{"rnn1"});

    CpParams cp3;
    cp3.setPar("inputShape",vector<int>{H,T});
    cp3.setPar("N",BS);
    cp3.setPar("H",H);
    cp3.setPar("clip",clip);
    lb.addLayer("RNN","rnn3",cp3,{"rnn2"});

    CpParams cp10;
    cp10.setPar("inputShape",vector<int>{H,T});
    //cp10.setPar("T",T);
    //cp10.setPar("D",H);
    cp10.setPar("M",VS);
    lb.addLayer("TemporalAffine","af1",cp10,{"rnn3"});

    CpParams cp11;
    cp11.setPar("inputShape",vector<int>{VS,T});
    lb.addLayer("TemporalSoftmax","sm1",cp11,{"af1"});

    if (!lb.checkTopology(true)) {
        allOk=false;
        cerr << red << "Topology-check for LayerBlock: ERROR." << def << endl;
    } else {
        cerr << green << "Topology-check for LayerBlock: ok." << def << endl;
    }

/*    wstring chunk;
    chunk = txt.text.substr(512,128);
    wcout << chunk << endl;
*/
    CpParams cpo("{verbose=true;epsilon=1e-8}");
    // CpParams cpo("{verbose=false;epsilon=1e-8}");
    cpo.setPar("learning_rate", (floatN)1e-2); //2.2e-2);
    cpo.setPar("lr_decay", (floatN)0.95);
    //cpo.setPar("regularization", (floatN)1.);

    floatN dep=1.0;
    floatN sep=0.0;
    cpo.setPar("epochs",(floatN)dep);
    cpo.setPar("batch_size",BS);

    MatrixN xg(1,T);
    MatrixN xg2(1,T);
    wstring sg;
    for (int i=0; i<10000; i++) {

        cpo.setPar("startepoch", (floatN)sep);
        cpo.setPar("maxthreads", (int)1); // RNN can't cope with threads.

        Layer *prnn1=lb.layerMap["rnn1"];
        prnn1->params["ho"]->setZero();
        /*Layer *prnn2=lb.layerMap["rnn2"];
        prnn2->params["ho"]->setZero();
        Layer *prnn3=lb.layerMap["rnn3"];
        prnn3->params["ho"]->setZero();
*/
        floatN cAcc=lb.train(X, y, Xv, yv, "Adam", cpo);
        sep+=dep;

        int pos=rand() % 1000 + 5000;
        wstring instr=txt.text.substr(pos,T);

        for (int i=0; i<T; i++) {
            if (i<instr.size()) {
                xg(0,i)=txt.w2v[instr[i]];
            } else {
                xg(0,i)=txt.w2v[L' '];
            }
        }
        /*
        for (auto wc : instr) {
            sg[0]=wc;
            xg(0,0)=txt.w2v[sg[0]];
            MatrixN z(0,0);
            MatrixN yg=lb.forward(xg,z,nullptr);
        }
        */
        //xg(0,0)=txt.w2v[sg[0]];
        for (int rep=0; rep<3; rep++) {
            //Layer *prnn=lb.layerMap["rnn1"];
            //prnn->params["ho"]->setZero();
            cerr << "------------" << rep << "--------------------------" << endl;
            for (int g=0; g<200; g++) {
                //wcout << g << L">";
                MatrixN z(0,0);

                //for (int i; i<xg.cols(); i++) wcout << txt.v2w[xg(0,i)];
                //wcout << L"<" << endl << L">";
                MatrixN probst=lb.forward(xg,z,nullptr);
                MatrixN probsd=MatrixN(N*T,VS);
                for (int n=0; n<1; n++) {
                    for (int t=0; t<T; t++) {
                        for (int d=0; d<VS; d++) {
                            probsd(n*T+t,d)=probst(n,t*VS+d);
                        }
                    }
                }

                for (int t=0; t<T; t++) {
                    vector<floatN> probs(VS);
                    vector<floatN> index(VS);
                    for (int d=0; d<VS; d++) {
                        probs[d]=probsd(0*T+t,d);
                        index[d]=d;
                    }
                    int ind=(int)index[randomChoice(index, probs)];
/*                    float mx=-1000.0;
                    int ind=-1;
                    for (int d=0; d<VS; d++) {
                        floatN p=yg(0,t*D+d);
                        floatN pr=p*((floatN)(rand()%100)/5000.0+0.98);
                        if (pr>mx) {
                            mx=pr; // yg(0,t*D+d);
                            ind=d;
                        }
                    }
*/
                    wchar_t cw=txt.v2w[ind];
                    //if (t==0) wcout << L"[" << cw << L"<";
                    //wcout << L"<" << cw << L">";
                    if (t==0) wcout << cw;
                    xg2(0,t)=ind;
                }
                //wcout << L"<" << endl;

                //for (int t=T-1; t>0; t--) xg(0,t)=xg(0,t-1);
                for (int t=0; t< T-1; t++) xg(0,t)=xg(0,t+1);
                xg(0,T-1)=xg2(0,0);

                //xg=xg2;
            }
            wcout << endl;
        }
    }
    /*
    floatN train_err, val_err, test_err;
    bool evalFinal=true;
    if (evalFinal) {
        train_err=lb.test(X, y, cpo.getPar("batch_size", 50));
        val_err=lb.test(Xv, yv, cpo.getPar("batch_size", 50));
        test_err=lb.test(Xt, yt, cpo.getPar("batch_size", 50));

        cerr << "Final results on RnnReader after " << cpo.getPar("epochs",(floatN)0.0) << " epochs:" << endl;
        cerr << "      Train-error: " << train_err << " train-acc: " << 1.0-train_err << endl;
        cerr << " Validation-error: " << val_err <<   "   val-acc: " << 1.0-val_err << endl;
        cerr << "       Test-error: " << test_err <<  "  test-acc: " << 1.0-test_err << endl;
    }
    //return cAcc;
    */
    cpExitCompute();
}
<|endoftext|>


<commit_before>#include "regularizer.hpp"
#include <iostream>

namespace deconvolution {

void incrementBase(const std::vector<int>& extents, int subproblem, std::vector<int>& base) {
    for (int pos = 0; pos < int(extents.size()); ++pos) {
        if (pos == subproblem)
            continue;
        base[pos]++;
        if (base[pos] < extents[pos])
            return;
        else
            base[pos] = 0;
    }
}

template <int D>
double GridRegularizer<D>::evaluate(int subproblem, const double* lambda_a, double smoothing, double lambdaScale, double* gradient) const {
    assert(subproblem >= 0 && subproblem < D);
    double objective = 0;

    std::vector<int> lambdaExtents = _extents;
    lambdaExtents.push_back(numLabels());
    assert(lambdaExtents.size() == D+1);
    auto L = boost::const_multi_array_ref<double, D+1>{lambda_a, lambdaExtents};
    auto G = boost::multi_array_ref<double, D+1>{gradient, lambdaExtents};

    int width = _extents[subproblem]; // Length along this dimension of the grid
    std::vector<int> base(D, 0);
    int numBases = 1;
    for (int i = 0; i < D; ++i)
        numBases *= (i == subproblem) ? 1 : _extents[i];

    std::vector<double> lambdaSlice(_numLabels*width, 0);
    std::vector<double> m_L(_numLabels*width, 0);
    std::vector<double> m_R(_numLabels*width, 0);
    std::vector<double> labelCosts(_numLabels, 0);
    for (int countBase = 0; countBase < numBases; ++countBase, incrementBase(_extents, subproblem, base)) {
        /*
        std::cout << "\tbase: ";
        for (auto idx : base) std::cout << idx << " ";
        std::cout << "\n";
        */
        std::vector<int> point = base;
        point.push_back(0);
        int& pointIndex = point[subproblem];
        int& pointLabel = point[point.size()-1];

        for (pointIndex = 0; pointIndex < width; ++pointIndex)
            for (pointLabel = 0; pointLabel < _numLabels; ++pointLabel)
                lambdaSlice[pointIndex*_numLabels + pointLabel] = L(point);

        // Compute log m_L
        // Base step
        for (int l = 0; l < _numLabels; ++l)
            m_L[l] = lambdaScale*lambdaSlice[l]/smoothing;
        // Inductive step
        for (int j = 1; j < width; ++j) {
            for (int lCurr = 0; lCurr < _numLabels; ++lCurr) {
                double maxMessage = std::numeric_limits<double>::lowest();
                for (int lPrev = 0; lPrev < _numLabels; ++lPrev) {
                    labelCosts[lPrev] = -_edgeFn(lPrev, lCurr)/smoothing + m_L[(j-1)*_numLabels+lPrev];
                    maxMessage = std::max(maxMessage, labelCosts[lPrev]);
                }
                double sumExp = 0;
                for (int lPrev = 0; lPrev < _numLabels; ++lPrev)
                    sumExp += exp(labelCosts[lPrev] - maxMessage);
                m_L[j*_numLabels+lCurr] = lambdaScale*lambdaSlice[j*_numLabels+lCurr]/smoothing + maxMessage + log(sumExp);
            }
        }

        // Compute log m_R
        for (int lCurr = 0; lCurr < _numLabels; ++lCurr)
            m_R[(width-1)*_numLabels+lCurr] = 0.0;
        for (int j = width-2; j >= 0; --j) {
            for (int lCurr = 0; lCurr < _numLabels; ++lCurr) {
                double maxMessage = std::numeric_limits<double>::lowest();
                for (int lPrev = 0; lPrev < _numLabels; ++lPrev) {
                    labelCosts[lPrev] = -(_edgeFn(lCurr, lPrev) - lambdaScale*lambdaSlice[(j+1)*_numLabels+lPrev])/smoothing + m_R[(j+1)*_numLabels+lPrev];
                    maxMessage = std::max(maxMessage, labelCosts[lPrev]);
                }
                double sumExp = 0;
                for (int lPrev = 0; lPrev < _numLabels; ++lPrev)
                    sumExp += exp(labelCosts[lPrev] - maxMessage);
                m_R[j*_numLabels+lCurr] = maxMessage + log(sumExp);
            }
        }

        // Compute marginals, put them in G
        double logSumExp = 0;
        std::vector<double> logMarg(_numLabels, 0);
        for (int j = 0; j < width; ++j) {
            double maxMarg = std::numeric_limits<double>::lowest();
            for (int l = 0; l < _numLabels; ++l) {
                logMarg[l] = m_L[j*_numLabels+l] + m_R[j*_numLabels+l];
                maxMarg = std::max(maxMarg, logMarg[l]);
            }
            double sumExp = 0;
            for (int l = 0; l < _numLabels; ++l)
                sumExp += exp(logMarg[l] - maxMarg);
            logSumExp = maxMarg + log(sumExp);
            pointIndex = j;
            for (pointLabel = 0; pointLabel < _numLabels; ++pointLabel)
                G(point) = -lambdaScale*exp(logMarg[pointLabel] - logSumExp);
        }
        objective += -smoothing*logSumExp;
    }
    return objective;
}

#define INSTANTIATE_DECONVOLVE_REGULARIZER(d) \
    template class GridRegularizer<d>;
INSTANTIATE_DECONVOLVE_REGULARIZER(1)
INSTANTIATE_DECONVOLVE_REGULARIZER(2)
INSTANTIATE_DECONVOLVE_REGULARIZER(3)
#undef INSTANTIATE_DECONVOLVE_REGULARIZER

}
<commit_msg>Moving allocation of logMarg<commit_after>#include "regularizer.hpp"
#include <iostream>

namespace deconvolution {

void incrementBase(const std::vector<int>& extents, int subproblem, std::vector<int>& base) {
    for (int pos = 0; pos < int(extents.size()); ++pos) {
        if (pos == subproblem)
            continue;
        base[pos]++;
        if (base[pos] < extents[pos])
            return;
        else
            base[pos] = 0;
    }
}

template <int D>
double GridRegularizer<D>::evaluate(int subproblem, const double* lambda_a, double smoothing, double lambdaScale, double* gradient) const {
    assert(subproblem >= 0 && subproblem < D);
    double objective = 0;

    std::vector<int> lambdaExtents = _extents;
    lambdaExtents.push_back(numLabels());
    assert(lambdaExtents.size() == D+1);
    auto L = boost::const_multi_array_ref<double, D+1>{lambda_a, lambdaExtents};
    auto G = boost::multi_array_ref<double, D+1>{gradient, lambdaExtents};

    int width = _extents[subproblem]; // Length along this dimension of the grid
    std::vector<int> base(D, 0);
    int numBases = 1;
    for (int i = 0; i < D; ++i)
        numBases *= (i == subproblem) ? 1 : _extents[i];

    std::vector<double> lambdaSlice(_numLabels*width, 0);
    std::vector<double> m_L(_numLabels*width, 0);
    std::vector<double> m_R(_numLabels*width, 0);
    std::vector<double> logMarg(_numLabels, 0);
    std::vector<double> labelCosts(_numLabels, 0);
    for (int countBase = 0; countBase < numBases; ++countBase, incrementBase(_extents, subproblem, base)) {
        /*
        std::cout << "\tbase: ";
        for (auto idx : base) std::cout << idx << " ";
        std::cout << "\n";
        */
        std::vector<int> point = base;
        point.push_back(0);
        int& pointIndex = point[subproblem];
        int& pointLabel = point[point.size()-1];

        for (pointIndex = 0; pointIndex < width; ++pointIndex)
            for (pointLabel = 0; pointLabel < _numLabels; ++pointLabel)
                lambdaSlice[pointIndex*_numLabels + pointLabel] = L(point);

        // Compute log m_L
        // Base step
        for (int l = 0; l < _numLabels; ++l)
            m_L[l] = lambdaScale*lambdaSlice[l]/smoothing;
        // Inductive step
        for (int j = 1; j < width; ++j) {
            for (int lCurr = 0; lCurr < _numLabels; ++lCurr) {
                double maxMessage = std::numeric_limits<double>::lowest();
                for (int lPrev = 0; lPrev < _numLabels; ++lPrev) {
                    labelCosts[lPrev] = -_edgeFn(lPrev, lCurr)/smoothing + m_L[(j-1)*_numLabels+lPrev];
                    maxMessage = std::max(maxMessage, labelCosts[lPrev]);
                }
                double sumExp = 0;
                for (int lPrev = 0; lPrev < _numLabels; ++lPrev)
                    sumExp += exp(labelCosts[lPrev] - maxMessage);
                m_L[j*_numLabels+lCurr] = lambdaScale*lambdaSlice[j*_numLabels+lCurr]/smoothing + maxMessage + log(sumExp);
            }
        }

        // Compute log m_R
        for (int lCurr = 0; lCurr < _numLabels; ++lCurr)
            m_R[(width-1)*_numLabels+lCurr] = 0.0;
        for (int j = width-2; j >= 0; --j) {
            for (int lCurr = 0; lCurr < _numLabels; ++lCurr) {
                double maxMessage = std::numeric_limits<double>::lowest();
                for (int lPrev = 0; lPrev < _numLabels; ++lPrev) {
                    labelCosts[lPrev] = -(_edgeFn(lCurr, lPrev) - lambdaScale*lambdaSlice[(j+1)*_numLabels+lPrev])/smoothing + m_R[(j+1)*_numLabels+lPrev];
                    maxMessage = std::max(maxMessage, labelCosts[lPrev]);
                }
                double sumExp = 0;
                for (int lPrev = 0; lPrev < _numLabels; ++lPrev)
                    sumExp += exp(labelCosts[lPrev] - maxMessage);
                m_R[j*_numLabels+lCurr] = maxMessage + log(sumExp);
            }
        }

        // Compute marginals, put them in G
        double logSumExp = 0;
        for (int j = 0; j < width; ++j) {
            double maxMarg = std::numeric_limits<double>::lowest();
            for (int l = 0; l < _numLabels; ++l) {
                logMarg[l] = m_L[j*_numLabels+l] + m_R[j*_numLabels+l];
                maxMarg = std::max(maxMarg, logMarg[l]);
            }
            double sumExp = 0;
            for (int l = 0; l < _numLabels; ++l)
                sumExp += exp(logMarg[l] - maxMarg);
            logSumExp = maxMarg + log(sumExp);
            pointIndex = j;
            for (pointLabel = 0; pointLabel < _numLabels; ++pointLabel)
                G(point) = -lambdaScale*exp(logMarg[pointLabel] - logSumExp);
        }
        objective += -smoothing*logSumExp;
    }
    return objective;
}

#define INSTANTIATE_DECONVOLVE_REGULARIZER(d) \
    template class GridRegularizer<d>;
INSTANTIATE_DECONVOLVE_REGULARIZER(1)
INSTANTIATE_DECONVOLVE_REGULARIZER(2)
INSTANTIATE_DECONVOLVE_REGULARIZER(3)
#undef INSTANTIATE_DECONVOLVE_REGULARIZER

}
<|endoftext|>


<commit_before>#ifndef COUNTERPOINT_HPP
#define COUNTERPOINT_HPP
/*
  Status: RO
  Return-Path: <bil@ccrma.Stanford.EDU>
  Received: from smtp3.Stanford.EDU ([171.67.16.138])
  by aaron.mail.atl.earthlink.net (EarthLink SMTP Server) with ESMTP id 1ctFSm3Lh3Nl3qa0
  for <gogins@pipeline.com>; Mon, 15 Nov 2004 07:22:30 -0500 (EST)
  Received: from cm-mail.stanford.edu (cm-mail.Stanford.EDU [171.64.197.135])
  by smtp3.Stanford.EDU (8.12.11/8.12.11) with ESMTP id iAFCMSeh031083
  for <gogins@pipeline.com>; Mon, 15 Nov 2004 04:22:29 -0800
  Received: from ccrma (cmn13.stanford.edu [171.64.197.162])
  by cm-mail.stanford.edu (8.11.6/8.11.6) with ESMTP id iAFCMSH28453
  for <gogins@pipeline.com>; Mon, 15 Nov 2004 04:22:28 -0800
  Message-ID: <41989F84.7000906@ccrma>
  Date: Mon, 15 Nov 2004 04:22:28 -0800
  From: Bill Schottstaedt <bil@ccrma.Stanford.EDU>
  User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4.2) Gecko/20040308
  X-Accept-Language: en-us, en
  MIME-Version: 1.0
  To: Michael Gogins <gogins@pipeline.com>
  Subject: Re: Permission
  References: <000301c4cab5$48db27f0$6501a8c0@Generator>
  In-Reply-To: <000301c4cab5$48db27f0$6501a8c0@Generator>
  Content-Type: text/plain; charset=us-ascii; format=flowed
  Content-Transfer-Encoding: 7bit
  X-ELNK-AV: 0

  You're most welcome to use my Fux code and article in any
  way you like.  I did a C translation of it about 10 years
  ago -- I think the original SAIL article is at ccrma-ftp
  as fux.txt, and I'll append what I think is the C version --
  if it doesn't work, let me know, and I'll scrounge around
  some old disks at home.  (This project was a ton of fun --
  I've always wanted to go back and carry it further, but
  have never had time):
*/

/* AUTOMATIC COUNTERPOINT
 *
 * from "Automatic Species Counterpoint", Bill Schottstaedt, CCRMA STAN-M-19, May 1984
 * translated from SAIL to C in March 1995
 *
 * This file can be treated as a stand-alone C program or as a foreign function module for Lisp.
 *
 * make fux creates the C program
 * cc fux.c -c -O -DCM creates the module
 *
 * See the "main" function for examples, or fux.lisp (which ties fux.c into CMN/CM).
 */

#include "Platform.hpp"
#ifdef SWIG
%module CsoundAC
%{
#include <string>
#include <cstdarg>
#include <stdio.h>
#include <malloc.h>
#include <boost/random.hpp>
#include <boost/random/variate_generator.hpp>
%}
#else
#include <string>
#include <cstdarg>
#include <stdio.h>
#include <malloc.h>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/vector.hpp>
#include <boost/random.hpp>
#include <boost/random/variate_generator.hpp>
#include "Random.hpp"
using namespace boost::numeric;
#endif

class Counterpoint
{
public:
  void (*messageCallback)(CSOUND *csound, int attribute, const char *format, va_list valist);
  void message(const char *format,...);
  void message(const char *format, va_list valist);
  int MostNotes;
  int MostVoices;
  enum
    {
      MostNotes_ = 128,
      MostVoices_ = 12
    };
  long randx;
  boost::numeric::ublas::matrix<int> Ctrpt;
  boost::numeric::ublas::matrix<int> Onset;
  boost::numeric::ublas::matrix<int> Dur;
  boost::numeric::ublas::vector<int> TotalNotes;
  boost::numeric::ublas::matrix<int> BestFit;
  boost::numeric::ublas::matrix<int> BestFit1;
  boost::numeric::ublas::matrix<int> BestFit2;
  boost::numeric::ublas::vector<int> vbs;
  boost::numeric::ublas::matrix<int> RhyPat;
  boost::numeric::ublas::vector<int> RhyNotes;
  int Fits[3];
  virtual void initialize(int mostnotes, int mostvoices);
  virtual void clear();
  Counterpoint();
  virtual ~Counterpoint();
  int ABS(int i);
  int MIN(int a, int b);
  int MAX(int a, int b);
  void ARRBLT(int *dest, int *source, int num);
  enum
    {
      Unison = 0,
      MinorSecond = 1,
      MajorSecond = 2,
      MinorThird = 3,
      MajorThird = 4,
      Fourth = 5,
      Tritone = 6,
      Fifth = 7,
      MinorSixth = 8,
      MajorSixth = 9,
      MinorSeventh = 10,
      MajorSeventh = 11,
      Octave = 12
    };
#if !defined(SWIG)
  static int PerfectConsonance[13];
  static int ImperfectConsonance[13];
  static int Dissonance[13];
#endif
  enum
    {
      Aeolian = 1,
      Dorian = 2,
      Phrygian = 3,
      Lydian = 4,
      Mixolydian = 5,
      Ionian = 6,
      Locrian = 7
    };
#if !defined(SWIG)
  static int _Ionian[12];
  static int _Dorian[12];
  static int _Phrygian[12];
  static int _Lydian[12];
  static int _Mixolydian[12];
  static int _Aeolian[12];
  static int _Locrian[12];
#endif
  int InMode(int Pitch, int Mode);
#if !defined(SWIG)
  static int BadMelodyInterval[13];
#endif
  int BadMelody(int Intv);
  int ASkip(int Interval);
  int AStep(int Interval);
  int AThird(int Interval);
  int ASeventh(int Interval);
  int AnOctave(int Interval);
  int ATenth(int Interval);
  enum
    {
      DirectMotion = 1,
      ContraryMotion = 2,
      ObliqueMotion = 3,
      NoMotion = 4
    };
  int MotionType(int Pitch1, int Pitch2, int Pitch3, int Pitch4);
  int DirectMotionToPerfectConsonance(int Pitch1, int Pitch2, int Pitch3, int Pitch4);
  int ConsecutiveSkipsInSameDirection(int Pitch1, int Pitch2, int Pitch3);
  int LowestSemitone;
  int HighestSemitone;
  int OutOfRange(int Pitch);
  int ExtremeRange(int Pitch);
  int BasePitch,Mode,TotalTime;
  int Us(int n, int v);
  int LastNote(int n, int v);
  int FirstNote(int n, int v);
  int NextToLastNote(int n, int v);
  void SetUs(int n, int p, int v);
  int TotalRange(int Cn, int Cp, int v);
  int Cantus(int n, int v);
  int VIndex(int Time, int VNum);
  int Other(int Cn, int v, int v1);
  int Bass(int Cn, int v);
  enum {

    WholeNote = 8,
    HalfNote = 4,
    DottedHalfNote = 6,
    QuarterNote = 2,
    DottedQuarterNote = 3,
    EighthNote = 1
  };
  int Beat8(int n);
  int DownBeat(int n, int v);
  int UpBeat(int n, int v);
  int PitchRepeats(int Cn, int Cp, int v);
  enum {
    One = 0,
    Two = 2,
    Three = 3,
    Four = 4,
    Five = 5,
    Six = 6,
    Eight = 8
  };
  int Size(int MelInt);
  int TooMuchOfInterval(int Cn, int Cp, int v);
  int ADissonance(int Interval, int Cn, int Cp, int v, int Species);
  int Doubled(int Pitch, int Cn, int v);
  enum
    {
      infinity = 1000000,
      Bad = 100,
      RealBad = 200
    };
  int UnisonPenalty;
  int DirectToFifthPenalty;
  int DirectToOctavePenalty;
  int ParallelFifthPenalty;
  int ParallelUnisonPenalty;
  int EndOnPerfectPenalty;
  int NoLeadingTonePenalty;
  int DissonancePenalty;
  int OutOfRangePenalty;
  int OutOfModePenalty;
  int TwoSkipsPenalty;
  int DirectMotionPenalty;
  int PerfectConsonancePenalty;
  int CompoundPenalty;
  int TenthToOctavePenalty;
  int SkipTo8vePenalty;
  int SkipFromUnisonPenalty;
  int SkipPrecededBySameDirectionPenalty;
  int FifthPrecededBySameDirectionPenalty;
  int SixthPrecededBySameDirectionPenalty;
  int SkipFollowedBySameDirectionPenalty;
  int FifthFollowedBySameDirectionPenalty;
  int SixthFollowedBySameDirectionPenalty;
  int TwoSkipsNotInTriadPenalty;
  int BadMelodyPenalty;
  int ExtremeRangePenalty;
  int LydianCadentialTritonePenalty;
  int LowerNeighborPenalty;
  int UpperNeighborPenalty;
  int OverTwelfthPenalty;
  int OverOctavePenalty;
  int SixthLeapPenalty;
  int OctaveLeapPenalty;
  int BadCadencePenalty;
  int DirectPerfectOnDownbeatPenalty;
  int RepetitionOnUpbeatPenalty;
  int DissonanceNotFillingThirdPenalty;
  int UnisonDownbeatPenalty;
  int TwoRepeatedNotesPenalty;
  int ThreeRepeatedNotesPenalty;
  int FourRepeatedNotesPenalty;
  int LeapAtCadencePenalty;
  int NotaCambiataPenalty;
  int NotBestCadencePenalty;
  int UnisonOnBeat4Penalty;
  int NotaLigaturePenalty;
  int LesserLigaturePenalty;
  int UnresolvedLigaturePenalty;
  int NoTimeForaLigaturePenalty;
  int EighthJumpPenalty;
  int HalfUntiedPenalty;
  int UnisonUpbeatPenalty;
  int MelodicBoredomPenalty;
  int SkipToDownBeatPenalty;
  int ThreeSkipsPenalty;
  int DownBeatUnisonPenalty;
  int VerticalTritonePenalty;
  int MelodicTritonePenalty;
  int AscendingSixthPenalty;
  int RepeatedPitchPenalty;
  int NotContraryToOthersPenalty;
  int NotTriadPenalty;
  int InnerVoicesInDirectToPerfectPenalty;
  int InnerVoicesInDirectToTritonePenalty;
  int SixFiveChordPenalty;
  int UnpreparedSixFivePenalty;
  int UnresolvedSixFivePenalty;
  int AugmentedIntervalPenalty;
  int ThirdDoubledPenalty;
  int DoubledLeadingTonePenalty;
  int DoubledSixthPenalty;
  int DoubledFifthPenalty;
  int TripledBassPenalty;
  int UpperVoicesTooFarApartPenalty;
  int UnresolvedLeadingTonePenalty;
  int AllVoicesSkipPenalty;
  int DirectToTritonePenalty;
  int CrossBelowBassPenalty;
  /* I added the following during the translation to C */
  int CrossAboveCantusPenalty;
  int NoMotionAgainstOctavePenalty;
  int SpecialSpeciesCheck(int Cn, int Cp, int v, int Other0, int Other1, int Other2, int NumParts,
                          int Species, int MelInt, int Interval, int ActInt, int LastIntClass, int Pitch, int LastMelInt, int CurLim);
  enum
    {
      INTERVALS_WITH_BASS_SIZE = 8
    };
  int IntervalsWithBass[INTERVALS_WITH_BASS_SIZE];
  /* 0 = octave, 2 = step, 3 = third, 4 = fourth, 5 = fifth, 6 = sixth, 7 = seventh */
  void AddInterval(int n);
  int OtherVoiceCheck(int Cn, int Cp, int v, int NumParts, int Species, int CurLim);
  int Check(int Cn, int Cp, int v, int NumParts, int Species, int CurLim);
  int BestFitPenalty,MaxPenalty,Branches,AllDone;
  float PenaltyRatio;
  enum
    {
      NumFields = 16,
      Field = (MostVoices_+1),
      EndF = (Field*NumFields)
    };
  int SaveIndx(int indx, int *Sp);
  void SaveResults(int CurrentPenalty, int Penalty, int v1, int Species);
#if !defined(SWIG)
  static int Indx[17];
#endif
  int Look(int CurPen, int CurVoice, int NumParts, int Species, int Lim, int *Pens, int *Is, int *CurNotes);
  void BestFitFirst(int CurTime, int CurrentPenalty, int NumParts, int Species, int BrLim);
  void FillRhyPat();
  float RANDOM(float amp);
  void UsedRhy(int n);
  int CurRhy(int n);
  void CleanRhy();
  int GoodRhy();
  void counterpoint(int OurMode, int *StartPitches, int CurV, int CantusFirmusLength, int Species, int *cantus);
  void AnySpecies(int OurMode, int *StartPitches, int CurV, int CantusFirmusLength, int Species);
  void fillCantus(int c0, int c1, int c2, int c3, int c4, int c5, int c6, int c7, int c8, int c9, int c10, int c11, int c12, int c13, int c14);
  void toCsoundScore(std::string filename, double secondsPerPulse);
  void winners(int v1, int *data, int *best, int *best1, int *best2, int *durs);
#if !defined(SWIG)
  static boost::mt19937 mersenneTwister;
  boost::variate_generator<boost::mt19937, boost::uniform_real<> > *uniform_real_generator;
#endif
};

#endif

<commit_msg>changed to include stdlib.h instead of malloc.h to compile on OSX <commit_after>#ifndef COUNTERPOINT_HPP
#define COUNTERPOINT_HPP
/*
  Status: RO
  Return-Path: <bil@ccrma.Stanford.EDU>
  Received: from smtp3.Stanford.EDU ([171.67.16.138])
  by aaron.mail.atl.earthlink.net (EarthLink SMTP Server) with ESMTP id 1ctFSm3Lh3Nl3qa0
  for <gogins@pipeline.com>; Mon, 15 Nov 2004 07:22:30 -0500 (EST)
  Received: from cm-mail.stanford.edu (cm-mail.Stanford.EDU [171.64.197.135])
  by smtp3.Stanford.EDU (8.12.11/8.12.11) with ESMTP id iAFCMSeh031083
  for <gogins@pipeline.com>; Mon, 15 Nov 2004 04:22:29 -0800
  Received: from ccrma (cmn13.stanford.edu [171.64.197.162])
  by cm-mail.stanford.edu (8.11.6/8.11.6) with ESMTP id iAFCMSH28453
  for <gogins@pipeline.com>; Mon, 15 Nov 2004 04:22:28 -0800
  Message-ID: <41989F84.7000906@ccrma>
  Date: Mon, 15 Nov 2004 04:22:28 -0800
  From: Bill Schottstaedt <bil@ccrma.Stanford.EDU>
  User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4.2) Gecko/20040308
  X-Accept-Language: en-us, en
  MIME-Version: 1.0
  To: Michael Gogins <gogins@pipeline.com>
  Subject: Re: Permission
  References: <000301c4cab5$48db27f0$6501a8c0@Generator>
  In-Reply-To: <000301c4cab5$48db27f0$6501a8c0@Generator>
  Content-Type: text/plain; charset=us-ascii; format=flowed
  Content-Transfer-Encoding: 7bit
  X-ELNK-AV: 0

  You're most welcome to use my Fux code and article in any
  way you like.  I did a C translation of it about 10 years
  ago -- I think the original SAIL article is at ccrma-ftp
  as fux.txt, and I'll append what I think is the C version --
  if it doesn't work, let me know, and I'll scrounge around
  some old disks at home.  (This project was a ton of fun --
  I've always wanted to go back and carry it further, but
  have never had time):
*/

/* AUTOMATIC COUNTERPOINT
 *
 * from "Automatic Species Counterpoint", Bill Schottstaedt, CCRMA STAN-M-19, May 1984
 * translated from SAIL to C in March 1995
 *
 * This file can be treated as a stand-alone C program or as a foreign function module for Lisp.
 *
 * make fux creates the C program
 * cc fux.c -c -O -DCM creates the module
 *
 * See the "main" function for examples, or fux.lisp (which ties fux.c into CMN/CM).
 */

#include "Platform.hpp"
#ifdef SWIG
%module CsoundAC
%{
#include <string>
#include <cstdarg>
#include <stdio.h>
#include <stdlib.h>
#include <boost/random.hpp>
#include <boost/random/variate_generator.hpp>
%}
#else
#include <string>
#include <cstdarg>
#include <stdio.h>
#include <stdlib.h>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/vector.hpp>
#include <boost/random.hpp>
#include <boost/random/variate_generator.hpp>
#include "Random.hpp"
using namespace boost::numeric;
#endif

class Counterpoint
{
public:
  void (*messageCallback)(CSOUND *csound, int attribute, const char *format, va_list valist);
  void message(const char *format,...);
  void message(const char *format, va_list valist);
  int MostNotes;
  int MostVoices;
  enum
    {
      MostNotes_ = 128,
      MostVoices_ = 12
    };
  long randx;
  boost::numeric::ublas::matrix<int> Ctrpt;
  boost::numeric::ublas::matrix<int> Onset;
  boost::numeric::ublas::matrix<int> Dur;
  boost::numeric::ublas::vector<int> TotalNotes;
  boost::numeric::ublas::matrix<int> BestFit;
  boost::numeric::ublas::matrix<int> BestFit1;
  boost::numeric::ublas::matrix<int> BestFit2;
  boost::numeric::ublas::vector<int> vbs;
  boost::numeric::ublas::matrix<int> RhyPat;
  boost::numeric::ublas::vector<int> RhyNotes;
  int Fits[3];
  virtual void initialize(int mostnotes, int mostvoices);
  virtual void clear();
  Counterpoint();
  virtual ~Counterpoint();
  int ABS(int i);
  int MIN(int a, int b);
  int MAX(int a, int b);
  void ARRBLT(int *dest, int *source, int num);
  enum
    {
      Unison = 0,
      MinorSecond = 1,
      MajorSecond = 2,
      MinorThird = 3,
      MajorThird = 4,
      Fourth = 5,
      Tritone = 6,
      Fifth = 7,
      MinorSixth = 8,
      MajorSixth = 9,
      MinorSeventh = 10,
      MajorSeventh = 11,
      Octave = 12
    };
#if !defined(SWIG)
  static int PerfectConsonance[13];
  static int ImperfectConsonance[13];
  static int Dissonance[13];
#endif
  enum
    {
      Aeolian = 1,
      Dorian = 2,
      Phrygian = 3,
      Lydian = 4,
      Mixolydian = 5,
      Ionian = 6,
      Locrian = 7
    };
#if !defined(SWIG)
  static int _Ionian[12];
  static int _Dorian[12];
  static int _Phrygian[12];
  static int _Lydian[12];
  static int _Mixolydian[12];
  static int _Aeolian[12];
  static int _Locrian[12];
#endif
  int InMode(int Pitch, int Mode);
#if !defined(SWIG)
  static int BadMelodyInterval[13];
#endif
  int BadMelody(int Intv);
  int ASkip(int Interval);
  int AStep(int Interval);
  int AThird(int Interval);
  int ASeventh(int Interval);
  int AnOctave(int Interval);
  int ATenth(int Interval);
  enum
    {
      DirectMotion = 1,
      ContraryMotion = 2,
      ObliqueMotion = 3,
      NoMotion = 4
    };
  int MotionType(int Pitch1, int Pitch2, int Pitch3, int Pitch4);
  int DirectMotionToPerfectConsonance(int Pitch1, int Pitch2, int Pitch3, int Pitch4);
  int ConsecutiveSkipsInSameDirection(int Pitch1, int Pitch2, int Pitch3);
  int LowestSemitone;
  int HighestSemitone;
  int OutOfRange(int Pitch);
  int ExtremeRange(int Pitch);
  int BasePitch,Mode,TotalTime;
  int Us(int n, int v);
  int LastNote(int n, int v);
  int FirstNote(int n, int v);
  int NextToLastNote(int n, int v);
  void SetUs(int n, int p, int v);
  int TotalRange(int Cn, int Cp, int v);
  int Cantus(int n, int v);
  int VIndex(int Time, int VNum);
  int Other(int Cn, int v, int v1);
  int Bass(int Cn, int v);
  enum {

    WholeNote = 8,
    HalfNote = 4,
    DottedHalfNote = 6,
    QuarterNote = 2,
    DottedQuarterNote = 3,
    EighthNote = 1
  };
  int Beat8(int n);
  int DownBeat(int n, int v);
  int UpBeat(int n, int v);
  int PitchRepeats(int Cn, int Cp, int v);
  enum {
    One = 0,
    Two = 2,
    Three = 3,
    Four = 4,
    Five = 5,
    Six = 6,
    Eight = 8
  };
  int Size(int MelInt);
  int TooMuchOfInterval(int Cn, int Cp, int v);
  int ADissonance(int Interval, int Cn, int Cp, int v, int Species);
  int Doubled(int Pitch, int Cn, int v);
  enum
    {
      infinity = 1000000,
      Bad = 100,
      RealBad = 200
    };
  int UnisonPenalty;
  int DirectToFifthPenalty;
  int DirectToOctavePenalty;
  int ParallelFifthPenalty;
  int ParallelUnisonPenalty;
  int EndOnPerfectPenalty;
  int NoLeadingTonePenalty;
  int DissonancePenalty;
  int OutOfRangePenalty;
  int OutOfModePenalty;
  int TwoSkipsPenalty;
  int DirectMotionPenalty;
  int PerfectConsonancePenalty;
  int CompoundPenalty;
  int TenthToOctavePenalty;
  int SkipTo8vePenalty;
  int SkipFromUnisonPenalty;
  int SkipPrecededBySameDirectionPenalty;
  int FifthPrecededBySameDirectionPenalty;
  int SixthPrecededBySameDirectionPenalty;
  int SkipFollowedBySameDirectionPenalty;
  int FifthFollowedBySameDirectionPenalty;
  int SixthFollowedBySameDirectionPenalty;
  int TwoSkipsNotInTriadPenalty;
  int BadMelodyPenalty;
  int ExtremeRangePenalty;
  int LydianCadentialTritonePenalty;
  int LowerNeighborPenalty;
  int UpperNeighborPenalty;
  int OverTwelfthPenalty;
  int OverOctavePenalty;
  int SixthLeapPenalty;
  int OctaveLeapPenalty;
  int BadCadencePenalty;
  int DirectPerfectOnDownbeatPenalty;
  int RepetitionOnUpbeatPenalty;
  int DissonanceNotFillingThirdPenalty;
  int UnisonDownbeatPenalty;
  int TwoRepeatedNotesPenalty;
  int ThreeRepeatedNotesPenalty;
  int FourRepeatedNotesPenalty;
  int LeapAtCadencePenalty;
  int NotaCambiataPenalty;
  int NotBestCadencePenalty;
  int UnisonOnBeat4Penalty;
  int NotaLigaturePenalty;
  int LesserLigaturePenalty;
  int UnresolvedLigaturePenalty;
  int NoTimeForaLigaturePenalty;
  int EighthJumpPenalty;
  int HalfUntiedPenalty;
  int UnisonUpbeatPenalty;
  int MelodicBoredomPenalty;
  int SkipToDownBeatPenalty;
  int ThreeSkipsPenalty;
  int DownBeatUnisonPenalty;
  int VerticalTritonePenalty;
  int MelodicTritonePenalty;
  int AscendingSixthPenalty;
  int RepeatedPitchPenalty;
  int NotContraryToOthersPenalty;
  int NotTriadPenalty;
  int InnerVoicesInDirectToPerfectPenalty;
  int InnerVoicesInDirectToTritonePenalty;
  int SixFiveChordPenalty;
  int UnpreparedSixFivePenalty;
  int UnresolvedSixFivePenalty;
  int AugmentedIntervalPenalty;
  int ThirdDoubledPenalty;
  int DoubledLeadingTonePenalty;
  int DoubledSixthPenalty;
  int DoubledFifthPenalty;
  int TripledBassPenalty;
  int UpperVoicesTooFarApartPenalty;
  int UnresolvedLeadingTonePenalty;
  int AllVoicesSkipPenalty;
  int DirectToTritonePenalty;
  int CrossBelowBassPenalty;
  /* I added the following during the translation to C */
  int CrossAboveCantusPenalty;
  int NoMotionAgainstOctavePenalty;
  int SpecialSpeciesCheck(int Cn, int Cp, int v, int Other0, int Other1, int Other2, int NumParts,
                          int Species, int MelInt, int Interval, int ActInt, int LastIntClass, int Pitch, int LastMelInt, int CurLim);
  enum
    {
      INTERVALS_WITH_BASS_SIZE = 8
    };
  int IntervalsWithBass[INTERVALS_WITH_BASS_SIZE];
  /* 0 = octave, 2 = step, 3 = third, 4 = fourth, 5 = fifth, 6 = sixth, 7 = seventh */
  void AddInterval(int n);
  int OtherVoiceCheck(int Cn, int Cp, int v, int NumParts, int Species, int CurLim);
  int Check(int Cn, int Cp, int v, int NumParts, int Species, int CurLim);
  int BestFitPenalty,MaxPenalty,Branches,AllDone;
  float PenaltyRatio;
  enum
    {
      NumFields = 16,
      Field = (MostVoices_+1),
      EndF = (Field*NumFields)
    };
  int SaveIndx(int indx, int *Sp);
  void SaveResults(int CurrentPenalty, int Penalty, int v1, int Species);
#if !defined(SWIG)
  static int Indx[17];
#endif
  int Look(int CurPen, int CurVoice, int NumParts, int Species, int Lim, int *Pens, int *Is, int *CurNotes);
  void BestFitFirst(int CurTime, int CurrentPenalty, int NumParts, int Species, int BrLim);
  void FillRhyPat();
  float RANDOM(float amp);
  void UsedRhy(int n);
  int CurRhy(int n);
  void CleanRhy();
  int GoodRhy();
  void counterpoint(int OurMode, int *StartPitches, int CurV, int CantusFirmusLength, int Species, int *cantus);
  void AnySpecies(int OurMode, int *StartPitches, int CurV, int CantusFirmusLength, int Species);
  void fillCantus(int c0, int c1, int c2, int c3, int c4, int c5, int c6, int c7, int c8, int c9, int c10, int c11, int c12, int c13, int c14);
  void toCsoundScore(std::string filename, double secondsPerPulse);
  void winners(int v1, int *data, int *best, int *best1, int *best2, int *durs);
#if !defined(SWIG)
  static boost::mt19937 mersenneTwister;
  boost::variate_generator<boost::mt19937, boost::uniform_real<> > *uniform_real_generator;
#endif
};

#endif

<|endoftext|>