⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 r2clist.c

📁 这是一个C程序分析工具
💻 C
字号:
/*====================================================================*/
/*  FILE   :   @(#)r2clist.c	2.4  -  08/04/99 */
/*====================================================================*/
/*  PURPOSE:  Abstract Data Type implementation of a height balanced  */
/*	      binary tree of Component Nodes.  This new implementation*/
/*	      is based on re-use of code found on the web, where the  */
/*	      functions within this file generally just call the      */
/*            appropriate function in the avl tree code, now called   */
/*            r2btree.c.    */
/*                                                                    */
/*  SYSTEM :  RECON II                                                */
/*                                                                    */
/*  USED BY:  main(), ReadTestData(), BuildCompList(), SelectComp(),  */
/*            and AnnotateSource().		                      */
/*                                                                    */
/*  HISTORY:                                                          */
/*  VER   DATE       AUTHOR        DESCRIPTION                        */
/*  1.00  25 Feb 93  L. Richard    Created unit.                      */
/*  1.01  28 Feb 93  L. Richard    Modified FindCompElem().           */
/*        Nov 4/93   N. Wilde      Modified FindCompElem to match     */
/*                                 Index, Line, Cval and Sval. Added  */
/*                                 the ok... invariant checks         */
/*  2.2   31 Jan 98  W. Hall       Modified elemVal() to clean up a   */
/*                                 compiler warning issue.            */
/*  2.3   20 Apr 98  A. Conwell    Gutted to use new avl tree code,   */
/*				   per Tisk 30 			      */
/*--------------------------------------------------------------------*/

/*==[ INTERFACE ]=====================================================*/

#include "r2.h"
#include "r2clist.h"
#include <stdio.h>

/*==[ PRIVATE IMPLEMENTATION ]========================================*/
#include <limits.h>
/*==[ PUBLIC IMPLEMENTATION ==========================================*/

/*-----------------------------Functions for r2btree------------------*/
/* The following three functions are needed to implement the new
   avl tree code found in r2btree.c.  This code was found on the
   Internet and used with enthusiastic permission by the author
   (Sam Rushing, www.nightmare.com).

   The package is a generic avl tree container that needs functions
   to manipulate the structures its organizing.  The three functions
   produce a comparison result, free the memory used by a given
   structure, and sprintf to a given buffer a short summary of the
   contents (used during verbose/debug sessions; package has a cute
   recursive tree print utility!).  The compare function pointer is
   retained at the tree's root; the free structure memory function is
   passed by pointer by the DeleteCompList function.
*/
int compare_CompElems(void *unused, void *a, void *b)
{
  COMPELEM *ca,*cb;

  ca=(COMPELEM *)a;
  cb=(COMPELEM *)b;

  if (ca->Index<cb->Index)
    return -1;
  if (ca->Index>cb->Index)
    return 1;

  if (ca->Line<cb->Line)
    return -1;
  else if (ca->Line>cb->Line)
    return 1;

  if (ca->Cval<cb->Cval)
    return -1;
  else if (ca->Cval>cb->Cval)
    return 1;

  if (ca->Sval<cb->Sval)
    return -1;
  else if (ca->Sval>cb->Sval)
    return 1;

  return 0;
}

int CompElem_Print(char *buf,void *x)
{
  COMPELEM *z;

  z=(COMPELEM *)x;
  if (z==NULL)
    return sprintf(buf,"NULL!!");
  else
    return sprintf(buf,"%d:%d|%c%ld",z->Index,z->Line,z->Cval,z->Sval);
}

int Free_CompElem(void *x)
{
  COMPELEM *z;

  z=(COMPELEM *)x;
  if (!z)
    free(z);
  return 0;
}
/*------------------------------------End of r2btree functions--------*/

/*--------------------------------------------------------------------*/
/* FUNCTION: AddCompElem                                              */
/*--------------------------------------------------------------------*/
/* PURPOSE : Allocates memory for a List Node and inserts it in the   */
/*           Component list sorted by index number and line number.   */
/*                                                                    */
/* PRECOND : Sorted list of size n exists.                            */
/* POSTCOND: Sorted list of size n+1 exists.                          */
/*                                                    		      */
/* HISTORY :                                                          */
/* VER   DATE         AUTHOR    DESCRIPTION                           */
/* 1.00  25 Feb 93  L. Richard  Created function.                     */
/* 2.30  17 Apr 98  A. Conwell  TISK30 guts orignal code	      */
/*--------------------------------------------------------------------*/

extern void AddCompElem(
  COMPLIST	*cl,			/* Component list	      */
  COMPELEM	*elem			/* Component element	      */
)
{
  COMPELEM  *ptr;

  ptr = (COMPELEM *)malloc(sizeof(COMPELEM));
  if (NULL == ptr)
    AbortMsg( "Unable to allocate space for Comp Element." );
  *ptr=*elem;
  insert_by_key(cl,(void *)ptr);

  return;

} /*AddCompElem() */

/*--------------------------------------------------------------------*/
/* FUNCTION: CompListSize                                             */
/*--------------------------------------------------------------------*/
/* PURPOSE : Returns the number of nodes in the list.                 */
/*                                                                    */
/* PRECOND : List exists.                                             */
/* POSTCOND: n/a                                                      */
/*                                                    		      */
/* HISTORY :                                                          */
/* VER   DATE         AUTHOR    DESCRIPTION                           */
/* 1.00  25 Feb 93  L. Richard  Created function.                     */
/* 2.30  17 Apr 98  A. Conwell  TISK30 changes          */
/*--------------------------------------------------------------------*/

extern int CompListSize(
  COMPLIST	*cl			/* Component list	      */
)
{

  return( (int)cl->length );

} /* CompListSize() */

/*--------------------------------------------------------------------*/
/* FUNCTION: CreateCompList                                           */
/*--------------------------------------------------------------------*/
/* PURPOSE : Creates an empty Component list.                         */
/*                                                                    */
/* PRECOND : List does not exists.                                    */
/* POSTCOND: List exists and is empty.                                */
/*                                                    		      */
/* HISTORY :                                                          */
/* VER   DATE         AUTHOR    DESCRIPTION                           */
/* 1.00  25 Feb 93  L. Richard  Created function.                     */
/* 2.30  17 Apr 98  A. Conwell  TISK30 changes           	      */
/*--------------------------------------------------------------------*/

extern void CreateCompList(
  COMPLIST	**cl			/* Component list	      */
)
{
  COMPLIST	*ptr;

  /* Create list */
  ptr = new_avl_tree(compare_CompElems,NULL);

  if (NULL == ptr)
     AbortMsg( "Unable to allocate space for Comp List." );

  *cl = ptr;

} /* CreateCompList() */

/*--------------------------------------------------------------------*/
/* FUNCTION: DeleteCompList                                           */
/*--------------------------------------------------------------------*/
/* PURPOSE : Disposes of all elems in a Comp list and returns the     */
/*           memory previously allocated.                             */
/*                                                                    */
/* PRECOND : List exists.                                             */
/* POSTCOND: List does not exist.                                     */
/*                                                    		      */
/* HISTORY :                                                          */
/* VER   DATE         AUTHOR    DESCRIPTION                           */
/* 1.00  25 Feb 93  L. Richard  Created function.                     */
/* 2.30  17 Apr 98  A. Conwell  TISK30 changes         		      */
/*--------------------------------------------------------------------*/

extern void DeleteCompList(
  COMPLIST	**cl			/* Component list	      */
)
{

  free_avl_tree(*cl,Free_CompElem);
  *cl=NULL;

} /* DeleteCompList() */

/*--------------------------------------------------------------------*/
/* FUNCTION: FindCompElem      				              */
/*--------------------------------------------------------------------*/
/* PURPOSE : Designate a component list node as current if it matches */
/*           given Index, Line, Cval and Sval sub-components.         */
/*                                                                    */
/* PRECOND : List exists.                                             */
/* POSTCOND: n/a                                                      */
/*                                                    		      */
/* HISTORY :                                                          */
/* VER   DATE         AUTHOR    DESCRIPTION                           */
/* 1.00  25 Feb 93 L. Richard   Created function.                     */
/* 1.01  28 Mar 93 L. Richard   Expandes comparsion to include Sval.  */
/* ...   31 Oct 93 N. Wilde     Expand comparison to include CVal.    */
/* 2.30  17 Apr 98 A. Conwell   TISK30 changes             	      */
/*--------------------------------------------------------------------*/

extern BOOL FindCompElem(
  COMPLIST	*cl,			/* Component list	      */
  COMPELEM	*elem			/* Component element	      */
)
{
  BOOL		found;
  COMPELEM    *dummy;

  if (get_item_by_key(cl,(void *)elem,(void *)&dummy))
  {
    found = FALSE;
  }
  else
    found = TRUE;

  return( found );

} /* FindCompElem() */

/*--------------------------------------------------------------------*/
/* FUNCTION: MergeCompElem                                            */
/*--------------------------------------------------------------------*/
/* PURPOSE : Merges an intermediate component list into the master    */
/*           component list.                                          */
/*                                                                    */
/* PRECOND : List exists and is not empty.                            */
/* POSTCOND: n/a                                                      */
/*                                                    		      */
/* HISTORY :                                                          */
/* VER   DATE         AUTHOR    DESCRIPTION                           */
/* 2.30  18 Apr 98  A. Conwell  Created function.                     */
/*--------------------------------------------------------------------*/

extern void MergeCompElem(
  COMPLIST  *icl,       /* Intermediate component list */
  COMPLIST  *cl         /* Master component list  */
)
{
  COMPNODE  *icl_node;
  COMPELEM  *icl_elem;
  COMPELEM  *cl_elem;

  if (icl==NULL)
    return;
  if (CompListSize(icl)<=0)
    return;
  icl_node=get_first_node(icl);

  do
  {
    if (get_item_by_key(cl,(void *)(icl_node->key),(void *)&cl_elem))
    {
      icl_elem=(COMPELEM *)(icl_node->key);
      insert_by_key(cl,(void *)icl_elem);
    }
    else
    {
      icl_elem=(COMPELEM *)(icl_node->key);
      cl_elem->With += icl_elem->With;
      cl_elem->Tot  += icl_elem->Tot;
    }
    icl_node=get_successor(icl_node);
  } while (icl_node->key!=NULL);

  return;
} /* MergeCompElem() */

/*--------------------------------------------------------------------*/
/* FUNCTION: RemoveCompElem                                           */
/*--------------------------------------------------------------------*/
/* PURPOSE : Deletes the current node from the list.                  */
/*                                                                    */
/* PRECOND : List exists and has a size of n (n >= 1).                */
/* POSTCOND: List exists and has a size of n-1 (n >= 0)               */
/*                                                    		      */
/* HISTORY :                                                          */
/* VER   DATE         AUTHOR    DESCRIPTION                           */
/* 1.00  25 Feb 93  L. Richard  Created function.                     */
/* 2.3   20 Apr 98  A. Conwell  Chgd per Tisk 30		      */
/*--------------------------------------------------------------------*/

extern void RemoveCompElem(
  COMPLIST	*cl,			/* Component list	      */
  COMPELEM	*elem			/* Component element	      */
)
{

  remove_by_key(cl,(void *)elem,Free_CompElem);

} /* RemoveCompElem() */

/*--------------------------------------------------------------------*/
/* FUNCTION: ShowCompList                                             */
/*--------------------------------------------------------------------*/
/* PURPOSE : Displays the contents of a Component List.               */
/*                                                                    */
/* PRECOND : Component list exists.                                   */
/* POSTCOND: n/a                                                      */
/*                                                    		      */
/* HISTORY :                                                          */
/* VER   DATE         AUTHOR    DESCRIPTION                           */
/* 1.00  25 Feb 93  L. Richard  Created function.                     */
/* 2.3   20 Apr 98  A. Conwell  Changed per Tisk 30		      */
/*--------------------------------------------------------------------*/

extern void ShowCompList(
  COMPLIST	*cl,			/* Component list	      */
  STRING	title[]			/* Display title	      */
)
{

  /* Display banner */
  printf( "\n----- %s -----\n", title );
  print_tree(cl,CompElem_Print);

} /* ShowCompList() */

/*====================================================================*/
/*  EOF    :  r2clist.c                                               */
/*====================================================================*/

⌨️ 快捷键说明

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