Back to index

texmacs  1.0.7.15
Typedefs | Functions | Variables
memorizer.cpp File Reference
#include "memorizer.hpp"
#include "environment.hpp"

Go to the source code of this file.

Typedefs

typedef memorizer_repmemorizer_ptr
typedef memorizer_rep ** memorizer_ptrs

Functions

void print_tree (memorizer mem)
template<typename T >
void double_size (T *&in, int &size)
void memorize_initialize ()
memorizer memorize_finalize ()
void memorize_start ()
void memorize_end ()
static memorizer_ptr bigmem_insert (memorizer_ptr ptr)
static void bigmem_remove (memorizer_ptr ptr)

Variables

int memorizer_count = 0
static int mem_cur
static int mem_max_pos
static int * mem_pos
static int mem_max_stack
static memorizermem_stack
static int bigmem_size = 0
static int bigmem_bags = 0
static int bigmem_bags_mask = bigmem_bags-1
static memorizer_ptrsbigmem_mem = NULL
static int * bigmem_len = NULL
static int * bigmem_cap = NULL

Typedef Documentation

Definition at line 166 of file memorizer.cpp.

Definition at line 167 of file memorizer.cpp.


Function Documentation

static memorizer_ptr bigmem_insert ( memorizer_ptr  ptr) [static]

Definition at line 177 of file memorizer.cpp.

                                  {
  if (bigmem_size >= bigmem_bags) {
    if (bigmem_bags == 0) {
      bigmem_bags= 2;
      //bigmem_bags= 1<<24;
      //cout << "Construct bigmem " << bigmem_bags << LF;
      bigmem_mem = tm_new_array<memorizer_ptrs> (bigmem_bags);
      bigmem_len = tm_new_array<int> (bigmem_bags);
      bigmem_cap = tm_new_array<int> (bigmem_bags);
      for (int i=0; i<bigmem_bags; i++) {
       bigmem_mem[i]= NULL;
       bigmem_len[i]= 0;
       bigmem_cap[i]= 0;
      }
    }
    else {
      int new_bags= bigmem_bags << 1;
      //cout << "Larger bigmem " << new_bags << LF;
      int new_bags_mask= new_bags-1;
      memorizer_ptrs* new_mem= tm_new_array<memorizer_ptrs> (new_bags);
      int* new_len= tm_new_array<int> (new_bags);
      int* new_cap= tm_new_array<int> (new_bags);
      for (int i=0; i<bigmem_bags; i++) {
       int j= i+ bigmem_bags;
       int len= bigmem_len[i];
       int cap= bigmem_cap[i];
       //cout << "bag " << i << ": " << len << ", " << cap << LF;
       if (len == 0) cap= 0;
       else while ((len<<1) <= cap && cap >= 4) cap= cap>>1;
       new_len[i]= 0;
       new_len[j]= 0;
       new_cap[i]= cap;
       new_cap[j]= cap;
       if (cap == 0) {
         new_mem[i]= NULL;
         new_mem[j]= NULL;
       }
       else {
         new_mem[i]= tm_new_array<memorizer_ptr> (cap);
         new_mem[j]= tm_new_array<memorizer_ptr> (cap);
         for (int k=0; k<len; k++) {
           memorizer_ptr mem_ptr= bigmem_mem[i][k];
           int h= mem_ptr->hash () & new_bags_mask;
           //cout << "  insert " << h << LF;
           ASSERT (h == i || h == j, "failed assertion");
           new_mem[h][new_len[h]++]= mem_ptr;
         }
       }
       if (cap != 0) tm_delete_array (bigmem_mem[i]);
      }
      tm_delete_array (bigmem_mem);
      tm_delete_array (bigmem_len);
      tm_delete_array (bigmem_cap);
      bigmem_bags= new_bags;
      bigmem_mem = new_mem;
      bigmem_len = new_len;
      bigmem_cap = new_cap;
    }
    bigmem_bags_mask= bigmem_bags-1;
  }

  int h= ptr->hash () & bigmem_bags_mask;
  memorizer_ptrs a= bigmem_mem[h];
  int i, len= bigmem_len[h];
  for (i=0; i<len; i++)
    if (a[i]->type () == ptr->type () && a[i]->equal (ptr))
      return a[i];
  int cap= bigmem_cap[h];
  if (len >= cap) {
    if (cap == 0) {
      a  = tm_new_array<memorizer_ptr> (2);
      cap= 2;
    }
    else {
      int new_cap= cap<<1;
      memorizer_ptrs b= tm_new_array<memorizer_ptr> (new_cap);
      for (i=0; i<len; i++) b[i]= a[i];
      tm_delete_array (a);
      a  = b;
      cap= new_cap;
    }
    bigmem_mem[h]= a;
    bigmem_cap[h]= cap;
  }
  a[bigmem_len[h]++]= ptr;
  bigmem_size++;
  //cout << "Added [" << bigmem_size << ", " << ptr << "] ";
  //ptr->print (cout);
  //cout << LF;
  return ptr;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void bigmem_remove ( memorizer_ptr  ptr) [static]

Definition at line 270 of file memorizer.cpp.

                                  {
  int h= ptr->hash () & bigmem_bags_mask;
  memorizer_ptrs a= bigmem_mem[h];
  int i, len= bigmem_len[h];
  for (i=0; i<len; i++)
    if (a[i] == ptr) {
      len--;
      a[i]= a[len];
      bigmem_len[h]= len;
      bigmem_size--;
      //cout << "Removed [" << bigmem_size << ", " << ptr << "] ";
      //ptr->print (cout);
      //cout << LF;
      return;
    }
  FAILED ("pointer not found");
}

Here is the call graph for this function:

Here is the caller graph for this function:

template<typename T >
void double_size ( T *&  in,
int &  size 
)

Definition at line 112 of file memorizer.cpp.

                                {
  T* out= tm_new_array<T> (2*size);
  for (int i=0; i<size; i++)
    out[i]= in[i];
  tm_delete_array (in);
  in= out;
  size *= 2;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void memorize_end ( )

Definition at line 155 of file memorizer.cpp.

                {
  mem_cur--;
  int start= mem_pos[mem_cur], end= mem_pos[mem_cur+1];
  //cout << UNINDENT << "Memorize end [" << start << ", " << end << "]" << LF;
  mem_stack[start-1]->set_children (mem_stack + start, end - start);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 133 of file memorizer.cpp.

                     {
  cout << UNINDENT << "Memorize finalize" << LF;
  memorizer mem= mem_stack[0];
  tm_delete_array (mem_pos);
  tm_delete_array (mem_stack);
  mem_max_pos  = 0;
  mem_pos      = NULL;
  mem_max_stack= 0;
  mem_stack    = NULL;
  return mem;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 122 of file memorizer.cpp.

                       {
  cout << "Memorize initialize" << INDENT << LF;
  mem_max_pos  = 16;
  mem_pos      = tm_new_array<int> (mem_max_pos);
  mem_max_stack= 16;
  mem_stack    = tm_new_array<memorizer> (mem_max_stack);
  mem_cur      = 0;
  mem_pos[0]   = 0;
}

Here is the caller graph for this function:

void memorize_start ( )

Definition at line 146 of file memorizer.cpp.

                  {
  //cout << "Memorize start" << INDENT << LF;
  mem_cur++;
  if (mem_cur == mem_max_pos)
    double_size (mem_pos, mem_max_pos);
  mem_pos[mem_cur]= mem_pos[mem_cur-1];
}

Here is the call graph for this function:

Here is the caller graph for this function:

void print_tree ( memorizer  mem)

Definition at line 87 of file memorizer.cpp.

                           {
  mem->print (cout);
  cout << LF;
  memorizer* a;
  int n;
  mem->get_children (a, n);
  if (n != 0) {
    cout << INDENT;
    for (int i=0; i<n; i++)
      print_tree (a[i]);
    cout << UNINDENT;
  }
}

Here is the call graph for this function:


Variable Documentation

int bigmem_bags = 0 [static]

Definition at line 170 of file memorizer.cpp.

int bigmem_bags_mask = bigmem_bags-1 [static]

Definition at line 171 of file memorizer.cpp.

int* bigmem_cap = NULL [static]

Definition at line 174 of file memorizer.cpp.

int* bigmem_len = NULL [static]

Definition at line 173 of file memorizer.cpp.

memorizer_ptrs* bigmem_mem = NULL [static]

Definition at line 172 of file memorizer.cpp.

int bigmem_size = 0 [static]

Definition at line 169 of file memorizer.cpp.

int mem_cur [static]

Definition at line 105 of file memorizer.cpp.

int mem_max_pos [static]

Definition at line 106 of file memorizer.cpp.

int mem_max_stack [static]

Definition at line 108 of file memorizer.cpp.

int* mem_pos [static]

Definition at line 107 of file memorizer.cpp.

memorizer* mem_stack [static]

Definition at line 109 of file memorizer.cpp.

int memorizer_count = 0

Definition at line 19 of file memorizer.cpp.