csearch.cpp

来自「eC++编译器源码」· C++ 代码 · 共 94 行

CPP
94
字号
#pragma csearch        /* string.cpp*/                         

#include <cstring.h>
#include <stdio.h>                                
#include <SYSTEM.h>


ADDRESS lfind (const WORD &key[],const WORD &table[],
              const unsigned int noElements,
              const unsigned int elementSizeInBytes,
              Compare procedure)
{unsigned int i;
  char *temp;
  i=0;
  temp = ADR(table); 
  while(i<noElements) {
    if(procedure(temp,ADR(key))==0) return temp;
    temp = ADDRESS(temp)+LONG(elementSizeInBytes);
    INC(i);
  }
  return NULL;
};        

ADDRESS bsearch (const WORD &key[],const WORD &table[],
              const unsigned int noElements,
              const unsigned int elementSizeInBytes,
              Compare procedure)
{
  int lower,upper,mid;
  int i;
  long es;
  char *temp;  
  char *temp1;
  es = LONG(elementSizeInBytes);
  lower = 0; 
  temp = ADR(table); 
  upper = noElements-1;
  while (lower <= upper) {
     mid = (lower + upper) / 2;
     temp1 =ADDRESS(temp)+LONG(mid)*es;
     i = procedure(temp1,ADR(key));
     if (i==-1) lower = mid+1;
     else if (i==1) upper = mid-1;
     else return temp1;
  }
  return NULL;
};

void swap(ADDRESS element1,ADDRESS element2,
          const unsigned int elementSizeInBytes)
{
}; 

void quicksort (const WORD &table[],
               const  unsigned int m,
               const  unsigned int n,
               const  int elementSizeInBytes,
               Compare procedure)   
{
 char *temp,*temp1,*temp2;
  unsigned int i; 
  int j;
  char *key; 
  temp  = ADR(table);
  if (m<n){
      i = m; j = n;
      key = ADDRESS(temp) + LONG(int(m)*elementSizeInBytes);
           do {
                 INC(i);
                 temp1 = ADDRESS(temp) + LONG(i)*LONG(elementSizeInBytes);}
           while ((procedure(temp1,key)<= 0)&&(i<n));   
           do {
                 DEC(j);
                 temp2 = ADDRESS(temp) + LONG(j)*LONG(elementSizeInBytes);}
           while ((procedure(temp1,key)>= 0)&&(j>=0));
           if (int(i)<j) swap(temp1,temp2,elementSizeInBytes);             
        
         temp1 = ADDRESS(temp)+LONG(m)*LONG(elementSizeInBytes);
         temp2 = ADDRESS(temp)+LONG(j)*LONG(elementSizeInBytes);
         swap(temp1,temp2,elementSizeInBytes);
         quicksort(table,m,j-1,elementSizeInBytes,procedure);
         quicksort(table,j+1,n,elementSizeInBytes,procedure);
         
  }                      
};
        
void qsort (const WORD &table[],
              const unsigned int noElements,
              const unsigned int elementSizeInBytes,
              Compare procedure)
{
 quicksort(table,0,noElements,elementSizeInBytes,procedure);
};

⌨️ 快捷键说明

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