<commit_before>/*
 *  Copyright 2007-2010 The OpenMx Project
 *
 *  Licensed 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 <R.h>
#include <Rinternals.h>
#include <Rdefines.h>
#include <R_ext/Rdynload.h>
#include <R_ext/BLAS.h>
#include <R_ext/Lapack.h>
#include "omxAlgebraFunctions.h"

#ifndef _OMX_R_OBJECTIVE_
#define _OMX_R_OBJECTIVE_ TRUE

typedef struct {

	SEXP objfun;
	SEXP model;
	PROTECT_INDEX modelIndex;
	SEXP flatModel;
	SEXP parameters;
	SEXP state;
	PROTECT_INDEX stateIndex;

} omxRObjective;

void omxDestroyRObjective(omxObjective *oo) {

	UNPROTECT(5); 			// objfun, model, flatModel, parameters, and state
}

void omxCallRObjective(omxObjective *oo) {

	omxRObjective* rObjective = (omxRObjective*)oo->argStruct;
	SEXP theCall, theReturn;
	PROTECT(theCall = allocVector(LANGSXP, 3));
	SETCAR(theCall, rObjective->objfun);
	SETCADR(theCall, rObjective->model);
	SETCADDR(theCall, rObjective->state);
	PROTECT(theReturn = eval(theCall, R_GlobalEnv));
	oo->matrix->data[0] = REAL(AS_NUMERIC(theReturn))[0];
	if (LENGTH(theReturn) > 1) {
		REPROTECT(rObjective->state = CADR(theReturn), rObjective->stateIndex);
	}

	UNPROTECT(2); // theCall and theReturn<commit_msg>Some cleanup of the mxRObjectiveFunction.<commit_after>/*
 *  Copyright 2007-2010 The OpenMx Project
 *
 *  Licensed 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 <R.h>
#include <Rinternals.h>
#include <Rdefines.h>
#include <R_ext/Rdynload.h>
#include <R_ext/BLAS.h>
#include <R_ext/Lapack.h>
#include "omxAlgebraFunctions.h"

#ifndef _OMX_R_OBJECTIVE_
#define _OMX_R_OBJECTIVE_ TRUE

typedef struct {

	SEXP objfun;
	SEXP model;
	PROTECT_INDEX modelIndex;
	SEXP flatModel;
	SEXP parameters;
	SEXP state;
	PROTECT_INDEX stateIndex;

} omxRObjective;

void omxDestroyRObjective(omxObjective *oo) {

	UNPROTECT(5); 			// objfun, model, flatModel, parameters, and state
}

void omxCallRObjective(omxObjective *oo) {

	omxRObjective* rObjective = (omxRObjective*)oo->argStruct;
	SEXP theCall, theReturn;
	PROTECT(theCall = allocVector(LANGSXP, 3));
	SETCAR(theCall, rObjective->objfun);
	SETCADR(theCall, rObjective->model);
	SETCADDR(theCall, rObjective->state);
	PROTECT(theReturn = eval(theCall, R_GlobalEnv));
	if (LENGTH(theReturn) == 1) {
		oo->matrix->data[0] = REAL(AS_NUMERIC(theReturn))[0];
	} else if (LENGTH(theReturn) == 2) {
		oo->matrix->data[0] = REAL(VECTOR_ELT(theReturn, 0))[0];
		REPROTECT(rObjective->state = VECTOR_ELT(theReturn, 1), rObjective->stateIndex);
	} else {
		// throw an error
	}

	UNPROTECT(2); // theCall and theReturn
{'fraction_non_alphanumeric': 0.11173184357541899, 'fraction_numerical': 0.011452513966480447, 'mean_word_length': 4.738782051282051, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2839262', 'n_tokens_mistral': 1326, 'n_tokens_neox': 1257, 'n_words': 341}