#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include "cdi.h"
#include "namespace.h"
#include "pio_util.h"


static int nNamespaces = 1;
static int activeNamespace = 0;

struct namespace
{
  int hasLocalFiles;
  statusCode resStage;
} initialNamespace = { .hasLocalFiles = 1, .resStage = STAGE_DEFINITION };

struct namespace *namespaces = &initialNamespace;

enum {
  intbits = sizeof(int) * CHAR_BIT,
  nspbits = 4,
  idxbits = intbits - nspbits,
  nspmask = (( 1 << nspbits ) - 1) << idxbits,
  idxmask = ( 1 << idxbits ) - 1,
};

enum {
  NUM_NAMESPACES = 1 << nspbits,
  NUM_IDX = 1 << idxbits,
};


#if 0
void namespaceShowbits ( int n, char *name )
{
  int i;
  unsigned mask;
  char bitvalues[intbits + 1];

  mask = 1;
  for ( i = 0; i < intbits; i++ )
    {
      bitvalues[i] = ((unsigned)n & mask) ? '1':'0';
      mask <<= 1;
    }
  bitvalues[intbits] = '\0';
  fprintf (stdout, "%s: %s\n", name, bitvalues );
}
#endif


int namespaceIdxEncode ( namespaceTuple_t tin )
{
  xassert ( tin.nsp < NUM_NAMESPACES && tin.idx < NUM_IDX);
  return ( tin.nsp << idxbits ) + tin.idx;
}

int namespaceIdxEncode2 ( int nsp, int idx )
{
  xassert(nsp < NUM_NAMESPACES && idx < NUM_IDX);
  return ( nsp << idxbits ) + idx;
}


namespaceTuple_t namespaceResHDecode ( int resH )
{
  namespaceTuple_t tin;

  tin.idx = resH & idxmask;
  tin.nsp = (int)(((unsigned)( resH & nspmask )) >> idxbits);

  return tin;
}


void namespaceInit ( int nspn, int * argHasLocalFile )
{
  /* FIXME: this should not be PIO-only */
#ifdef USE_MPI

  xassert(nspn <= NUM_NAMESPACES && nspn >= 1 );

  nNamespaces = nspn;
  if ( nspn >= 1 )
    {
      namespaces = xmalloc(nspn * sizeof (namespaces[0]));
      for (int nspID = 0; nspID < nspn; ++nspID)
        {
          namespaces[nspID].hasLocalFiles = argHasLocalFile[nspID];
          namespaces[nspID].resStage = STAGE_DEFINITION;
        }
    }
#endif
}


void namespaceCleanup ( void )
{
  if ( nNamespaces > 1 )
    {
      free(namespaces);
      namespaces = &initialNamespace;
      nNamespaces = 1;
    }
}


int namespaceGetNumber ()
{
  return nNamespaces;
}


void pioNamespaceSetActive ( int nId )
{
  /* FIXME: this should not be PIO-only */
#ifdef USE_MPI
  xassert ( nId < nNamespaces && nId >= 0 );

  activeNamespace = nId;
#endif
}


int namespaceGetActive ()
{
  return activeNamespace;
}


int namespaceHasLocalFile ( int nId )
{
  xassert ( nId < nNamespaces && nId >= 0 );

  return namespaces ? namespaces[nId].hasLocalFiles : 0;
}


int namespaceAdaptKey ( int key, int nspTarget )
{
  namespaceTuple_t tin;
  int nsp;

  if ( key == CDI_UNDEFID ) return CDI_UNDEFID;

  tin.idx = key & idxmask;
  tin.nsp = (int)(((unsigned)( key & nspmask )) >> idxbits);

  xassert ( tin.nsp == nspTarget );

  nsp = namespaceGetActive ();

  return namespaceIdxEncode2 ( nsp, tin.idx );
}


int namespaceAdaptKey2 ( int key )
{
  namespaceTuple_t tin;
  int nsp;

  if ( key == CDI_UNDEFID ) return CDI_UNDEFID;

  tin.idx = key & idxmask;
  tin.nsp = (int)(((unsigned)( key & nspmask )) >> idxbits);

  nsp = namespaceGetActive ();

  return namespaceIdxEncode2 ( nsp, tin.idx );
}


void namespaceDefResStatus ( statusCode argResStatus )
{
  int nsp = namespaceGetActive ();
  namespaces[nsp].resStage = argResStatus;
}


statusCode namespaceInqResStatus ( void )
{
  int nsp = namespaceGetActive ();
  return namespaces[nsp].resStage;
}

/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */
