/*************************************************************************
 * Copyright (c) 2011 AT&T Intellectual Property 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors: Details at https://graphviz.org
 *************************************************************************/

#pragma once

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <assert.h>
/* Applications that do not use the common library can define STANDALONE
 * to get definitions/definitions that are normally provided there.
 * In particular, note that Verbose is declared but undefined.
 */
#ifndef STANDALONE
#include "cgraph.h"
#include "exit.h"
#include "globals.h"
#include "logic.h"
#include "arith.h"
#include <common/memory.h>
#endif  /* STANDALONE */

#ifdef __cplusplus
extern "C" {
#endif

#define set_flag(a, flag) ((a)=((a)|(flag)))
#define test_flag(a, flag) ((a)&(flag))
#define clear_flag(a, flag) ((a) &=(~(flag)))

#ifdef STANDALONE
#define CALLOC calloc
#define MALLOC malloc
#define REALLOC realloc

#define N_NEW(n,t)   (t*)calloc((n),sizeof(t))
#define NEW(t)       (t*)malloc(sizeof(t))
#define MAX(a,b) ((a)>(b)?(a):b)
#define MIN(a,b) ((a)<(b)?(a):b)

#ifdef TRUE
#undef TRUE
#endif
#define TRUE 1

#ifdef FALSE
#undef FALSE
#endif
#define FALSE 0

#define PI 3.14159

#define POINTS(inch) 72*(inch)

#ifdef GVDLL
__declspec(dllimport) extern unsigned char Verbose;
#else
extern unsigned char Verbose;
#endif

#ifdef __GNUC__
// FIXME: use _Noreturn for all compilers when we move to C11
#define NORETURN __attribute__((noreturn))
#elif defined(_MSC_VER)
#define NORETURN __declspec(noreturn)
#else
#define NORETURN /* nothing */
#endif

static inline NORETURN void graphviz_exit(int status) {
#ifdef __MINGW32__
  // workaround for https://gitlab.com/graphviz/graphviz/-/issues/2178
  fflush(stdout);
  fflush(stderr);
#endif
  exit(status);
}

#undef NORETURN

#else  /* STANDALONE */
#define CALLOC gcalloc
#define MALLOC gmalloc
#define REALLOC grealloc
#endif    /* STANDALONE */

#ifdef DEBUG
extern double _statistics[10];
#endif


extern int irand(int n);
extern double drand(void);
extern int *random_permutation(int n);/* random permutation of 0 to n-1 */


double* vector_subtract_to(int n, double *x, double *y);/* y = x-y */

double vector_product(int n, double *x, double *y);

double* vector_saxpy(int n, double *x, double *y, double beta); /* y = x+beta*y */


double* vector_saxpy2(int n, double *x, double *y, double beta);/* x = x+beta*y */

/* take m elements v[p[i]]],i=1,...,m and oput in u. u will be assigned if *u = NULL */
void vector_float_take(int n, float *v, int m, int *p, float **u);

/* give the position of the smallest, second smallest etc in vector v.
   results in p. If *p == NULL, p is assigned.
*/
void vector_ordering(int n, double *v, int **p);
void vector_sort_int(int n, int *v);

void vector_print(char *s, int n, double *x);

#define MACHINEACC 1.0e-16
#define SQRT_MACHINEACC 1.0e-8

#define MINDIST 1.e-15

enum {UNMATCHED = -1};


double distance(double *x, int dim, int i, int j);
double distance_cropped(double *x, int dim, int i, int j);

double point_distance(double *p1, double *p2, int dim);

char *strip_dir(char *s);

void scale_to_box(double xmin, double ymin, double xmax, double ymax, int n, int dim, double *x);

#ifdef __cplusplus
}
#endif
