field.cpp

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C++ 代码 · 共 2,389 行 · 第 1/5 页

CPP
2,389
字号
#pragma inline_depth(0);
/* @(#)alloc.C	1.3   Release Date: 2/19/93
//	 Author:  Kent G. Budge, 
//		  Computational Physics Research and Development (1431)
//		  Sandia National Laboratories 
*/

#include <stdlib.h>
#include <stdio.h>
#include <math.h>		// Needed for all math functions

#include "field.h"
#include "fldalloc.h"

#define NUM_BUCKETS 8
#define NUM_BUCKETS_M1 (NUM_BUCKETS-1)
#define HASH_SIZE_OFFSET 3

static int NMC_OFFSET = -1;	/* Must be initialized at runtime */

typedef struct BucketST Bucket;

typedef struct MemoryChainST {
  struct MemoryChainST* next;
  struct MemoryChainST* next_free;
  Bucket *my_bucket;
  double ptr[1];
} MemoryChain;

static struct BucketST {
  size_t size;
  MemoryChain* root;
  MemoryChain* next_free;
  Bucket* next;
} *size_bucket[NUM_BUCKETS];

#define HashSizeCode(val) (NUM_BUCKETS_M1 & ((size_t)(val) >> HASH_SIZE_OFFSET))

void* __field_alloc_Allocate(size_t size){
  Bucket *new_entry;
  size_t n = HashSizeCode(size);
  Bucket *entry = size_bucket[n];
  while (entry){
    if (entry->size == size){
      MemoryChain *ptr = entry->next_free;
      if (ptr){
        entry->next_free = ptr->next_free;
        return (void*)ptr->ptr;
      }else{
        MemoryChain *new_chunk = 
            (MemoryChain*)malloc(size + sizeof(MemoryChain));
        if (!new_chunk) return 0;
        new_chunk->next = entry->root;
        new_chunk->next_free = NULL;
        new_chunk->my_bucket = entry;
        entry->root = new_chunk;
        entry->next_free = new_chunk;
        return __field_alloc_Allocate(size);
      }
    }
    entry = entry->next;
  }
  new_entry = (Bucket*)malloc(sizeof(Bucket));
  if (!new_entry) return 0;
  new_entry->size = size;
  new_entry->root = NULL;
  new_entry->next_free = NULL;
  new_entry->next = size_bucket[n];
  size_bucket[n] = new_entry;
  return __field_alloc_Allocate(size);
}

void __field_alloc_Deallocate(void* returned_pointer){
  MemoryChain *rtn;
  if (NMC_OFFSET == -1){
    NMC_OFFSET = (char*)((MemoryChain*)0)->ptr - (char*)(MemoryChain*)0;
  }
  rtn = (MemoryChain*)((char*)returned_pointer - NMC_OFFSET);
  rtn->next_free = rtn->my_bucket->next_free;
  rtn->my_bucket->next_free = rtn;
}

void __field_alloc_collect_garbage(void){
  /* nothing implemented just now */
  fprintf(stderr, "Out of memory; aborting ...\n"); 
  abort();
}
// @(#)f_acos.C	1.1   Release Date: 2/19/93
//	 Author:  Kent G. Budge, 
//		  Computational Physics Research and Development (1431)
//		  Sandia National Laboratories 



field acos(const field& f){
  size_t l = f.root->length;
  field rtn(l);
  for (register i=0; i<l; i++) rtn.root->data[i] = acos(f.root->data[i]);
  return rtn;
}
// @(#)f_asin.C	1.1   Release Date: 2/19/93
//	 Author:  Kent G. Budge, 
//		  Computational Physics Research and Development (1431)
//		  Sandia National Laboratories 



field asin(const field& f){
  size_t l = f.root->length;
  field rtn(l);
  for (register i=0; i<l; i++) rtn.root->data[i] = asin(f.root->data[i]);
  return rtn;
}
// @(#)f_assem_1.C	1.1   Release Date: 2/19/93


field& field::Assemble(const field& b, const ifield& ia, const ifield& ib){
  const size_t l = ia.Length();
  assert(l == ib.Length());
  Private();
  field& This = *this;
  for (register i=0; i<l; i++)  This[ia[i]] += b[ib[i]];
  return This;
}

// @(#)f_assem_2.C	1.1   Release Date: 2/19/93


field& field::Assemble(const field& b, const ifield& ia){
  const size_t l = ia.Length();
  assert(l == b.Length());
  Private();
  field& This = *this;
  for (register i=0; i<l; i++)  This[ia[i]] += b[i];
  return This;
}

// @(#)f_assem_3.C	1.1   Release Date: 2/19/93


field& field::Assemble(double b, const ifield& ia){
  const size_t l = ia.Length();
  Private();
  field& This = *this;
  for (register i=0; i<l; i++)  This[ia[i]] += b;
  return This;
}

// @(#)f_atan.C	1.1   Release Date: 2/19/93
//	 Author:  Kent G. Budge, 
//		  Computational Physics Research and Development (1431)
//		  Sandia National Laboratories 



field atan(const field& f){
  size_t l = f.root->length;
  field rtn(l);
  for (register i=0; i<l; i++) rtn.root->data[i] = atan(f.root->data[i]);
  return rtn;
}
// @(#)f_atan2_df.C	1.2   Release Date: 2/19/93



field atan2(double a, const field& b){
  size_t l = b.Length();
  field rtn(l);
  double *rp = rtn;
  const double *bp = b;
  for (register i=0; i<l; i++) rp[i] = atan2(a, bp[i]);
  return rtn;
}
// @(#)f_atan2_fd.C	1.2   Release Date: 2/19/93



field atan2(const field& a, double b){
  size_t l = a.Length();
  field rtn(l);
  double *rp = rtn;
  const double *ap = a;
  for (register i=0; i<l; i++) rp[i] = atan2(ap[i], b);
  return rtn;
}
// @(#)f_atan2_ff.C	1.1   Release Date: 2/19/93



field atan2(const field& a, const field& b){
  size_t l = a.root->length;
  assert(b.root->length == l);
  field rtn(l);
  for (register i=0; i<l; i++) 
    rtn.root->data[i] = atan2(a.root->data[i], b.root->data[i]);
  return rtn;
}
// @(#)f_ceil.C	1.1   Release Date: 2/19/93
//	 Author:  Kent G. Budge, 
//		  Computational Physics Research and Development (1431)
//		  Sandia National Laboratories 



field ceil(const field& f){
  size_t l = f.root->length;
  field rtn(l);
  for (register i=0; i<l; i++) rtn.root->data[i] = ceil(f.root->data[i]);
  return rtn;
}
// @(#)f_cos.C	1.1   Release Date: 2/19/93
//	 Author:  Kent G. Budge, 
//		  Computational Physics Research and Development (1431)
//		  Sandia National Laboratories 



field cos(const field& f){
  size_t l = f.root->length;
  field rtn(l);
  for (register i=0; i<l; i++) rtn.root->data[i] = cos(f.root->data[i]);
  return rtn;
}
// @(#)f_cosh.C	1.1   Release Date: 2/19/93
//	 Author:  Kent G. Budge, 
//		  Computational Physics Research and Development (1431)
//		  Sandia National Laboratories 



field cosh(const field& f){
  size_t l = f.root->length;
  field rtn(l);
  for (register i=0; i<l; i++) rtn.root->data[i] = cosh(f.root->data[i]);
  return rtn;
}
// @(#)f_ct_a.C	1.1   Release Date: 2/19/93
//	 Author:  Kent G. Budge, 
//		  Computational Physics Research and Development (1431)
//		  Sandia National Laboratories 



field::field(size_t len_a, const double *a) :
  ref_flag(0),
  root(new(len_a) __field_node(len_a))
{
  if (a) memcpy(root->data, a, sizeof(double)*len_a);
}
// @(#)f_ct_f.C	1.1   Release Date: 2/19/93
//	 Author:  Kent G. Budge, 
//		  Computational Physics Research and Development (1431)
//		  Sandia National Laboratories 


field::field(const field& f) :
  ref_flag(0)
{
  if (!f.root){
    root = NULL;
  }else if (f.ref_flag){
    root = f.root->NewCopy();
  }else{
    root = f.root;
    f.root->ref_count++;
  }
}
// @(#)f_di_d.C	1.1   Release Date: 2/19/93
//	 Author:  Kent G. Budge, 
//		  Computational Physics Research and Development (1431)
//		  Sandia National Laboratories 


field& field::operator/=(double d){
  assert(root!=NULL);
  Private();
  size_t l = root->length;
  for (register i=0; i<l; i++) root->data[i] /= d;
  return *this;
}
// @(#)f_di_df.C	1.1   Release Date: 2/19/93
//	 Author:  Kent G. Budge, 
//		  Computational Physics Research and Development (1431)
//		  Sandia National Laboratories 


field operator/(double d, const field& a){
  size_t l = a.root->length;
  field rtn(l);
  for (register i=0; i<l; i++) rtn.root->data[i] = d / a.root->data[i];
  return rtn;
}
// @(#)f_di_f.C	1.1   Release Date: 2/19/93
//	 Author:  Kent G. Budge, 
//		  Computational Physics Research and Development (1431)
//		  Sandia National Laboratories 


field& field::operator/=(const field& f){
  assert(root && f.root && root->length == f.root->length);
  Private();
  size_t l = root->length;
  for (register i=0; i<l; i++) root->data[i] /= f.root->data[i];
  return *this;
}
// @(#)f_di_fd.C	1.1   Release Date: 2/19/93
//	 Author:  Kent G. Budge, 
//		  Computational Physics Research and Development (1431)
//		  Sandia National Laboratories 


field operator/(const field& a, double d){
  size_t l = a.root->length;
  field rtn(l);
  for (register i=0; i<l; i++) rtn.root->data[i] = a.root->data[i] / d;
  return rtn;
}
// @(#)f_di_ff.C	1.1   Release Date: 2/19/93
//	 Author:  Kent G. Budge, 
//		  Computational Physics Research and Development (1431)
//		  Sandia National Laboratories 


field operator/(const field& a, const field& b){
  assert(a.root->length == b.root->length);
  size_t l = a.root->length;
  field rtn(l);
  for (register i=0; i<l; i++) 
    rtn.root->data[i] = a.root->data[i] / b.root->data[i];
  return rtn;
}
// @(#)f_dim_df.C	1.2   Release Date: 2/19/93



field dim(double a, const field& b){
  size_t l = b.Length();
  field rtn(l);
  double *rp = rtn;
  const double *bp = b;
  for (register i=0; i<l; i++) rp[i] = (a > bp[i]? a - bp[i] : 0.0);
  return rtn;
}
// @(#)f_dim_fd.C	1.1   Release Date: 2/19/93



field dim(const field& a, double b){
  size_t l = a.Length();
  field rtn(l);
  for (register i=0; i<l; i++)
    rtn.root->data[i] = (a.root->data[i] > b? a.root->data[i] - b : 0.0);
  return rtn;
}
// @(#)f_dim_ff.C	1.2   Release Date: 2/19/93



field dim(const field& a, const field& b){
  size_t l = b.Length();
  assert(a.Length() == l);
  field rtn(l);
  double *rp = rtn;
  const double *ap=a, *bp=b;
  for (register i=0; i<l; i++) rp[i] = (ap[i] > bp[i]? ap[i] - bp[i] : 0.0);
  return rtn;
}
// @(#)f_eq.C	1.1   Release Date: 2/19/93
//	 Author:  Kent G. Budge, 
//		  Computational Physics Research and Development (1431)
//		  Sandia National Laboratories 


field& field::operator=(const field& f){
  if (!f.root){
    Free();
    root = NULL;
  }else if (this == &f){
    return *this;
  }else if (f.ref_flag){
    Free();
    root = f.root->NewCopy();
  }else{
    Free();
    root = f.root;
    f.root->ref_count++;
  }
  ref_flag = 0;
  return *this;
}
// @(#)f_eq_d.C	1.1   Release Date: 2/26/93
//	 Author:  Kent G. Budge, 
//		  Computational Physics Research and Development (1431)
//		  Sandia National Laboratories 


field& field::operator=(double fill){
  if (root){
    if (root->ref_count > 1) Private();
    ref_flag = 0;
    const size_t l = root->length;
    for (register i=0; i<l; i++) root->data[i] = fill;
  }
  return *this;
}
// @(#)f_eq_i.C	1.1   Release Date: 2/19/93
//	 Author:  Kent G. Budge, 
//		  Computational Physics Research and Development (1431)
//		  Sandia National Laboratories 


field& field::operator=(const ifield& f){
  if (!f.root){
    Free();
    root = NULL;
  }else{
    Free();
    root = new(f.root->length) __field_node(f.root->length);
    for (register i=0; i<root->length; i++) 
      root->data[i] = (double)f.root->data[i];
  }
  ref_flag = 0;
  return *this;
}
// @(#)f_exp.C	1.1   Release Date: 2/19/93
//	 Author:  Kent G. Budge, 
//		  Computational Physics Research and Development (1431)
//		  Sandia National Laboratories 



field exp(const field& f){
  size_t l = f.root->length;
  field rtn(l);
  for (register i=0; i<l; i++) rtn.root->data[i] = exp(f.root->data[i]);
  return rtn;
}
// @(#)f_fabs.C	1.1   Release Date: 2/19/93
//	 Author:  Kent G. Budge, 
//		  Computational Physics Research and Development (1431)
//		  Sandia National Laboratories 



field fabs(const field& f){
  size_t l = f.root->length;
  field rtn(l);
  for (register i=0; i<l; i++) rtn.root->data[i] = fabs(f.root->data[i]);
  return rtn;
}
// @(#)f_floor.C	1.1   Release Date: 2/19/93
//	 Author:  Kent G. Budge, 
//		  Computational Physics Research and Development (1431)
//		  Sandia National Laboratories 



field floor(const field& f){
  size_t l = f.root->length;
  field rtn(l);
  for (register i=0; i<l; i++) rtn.root->data[i] = floor(f.root->data[i]);
  return rtn;
}
// @(#)f_fmod_df.C	1.2   Release Date: 2/19/93



field fmod(double a, const field& b){
  size_t l = b.Length();
  field rtn(l);
  double *rp = rtn;
  const double *bp = b;
  for (register i=0; i<l; i++) rp[i] = fmod(a, bp[i]);
  return rtn;
}
// @(#)f_fmod_fd.C	1.2   Release Date: 2/19/93


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?