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

📄 tuple_dim.c

📁 稀疏矩阵、链表、图、队列、二叉树、多叉树、排序、遗传算法等的实现
💻 C
字号:
/**************************************************************************
**  SP_TUPLE_DIM                                                         **
**                                                                       **
**    Assign a dimension to a tuple.                                     **
**                                                                       **
**  INPUT:                                                               **
**    tuple -- The tuple which is to get a dimension                     **
**    dim -- The dimension of the tuple                                  **
**                                                                       **
**  OUTPUT:                                                              **
**    SP_TUPLE * -- A pointer to the modified tuple                      **
**                                                                       **
**  SIDE EFFECTS:                                                        **
**    None.                                                              **
**                                                                       **
**  NOTES:                                                               **
**                                                                       **
**************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sparse.h"

SP_TUPLE *sp_tuple_dim(SP_TUPLE *tuple, int dim)
/* SP_TUPLE *tuple  The tuple which is to get the new dimension */
/* int dim          The dimension number */
{
  int *new_stack, curr_dim;

  /* If the tuple is empty, then no dimension is possible */
  if (tuple == (SP_TUPLE *)NULL)
  {
    return((SP_TUPLE *)NULL);
  }

  /* If the number of dimensions is illegal low, then fail */
  if (dim < (int)1)
  {
    return((SP_TUPLE *)NULL);
  }

  /* if the dimension isn't changing, then simply return */
  if (dim == tuple->dimensions)
  {
    return(tuple);
  }

  /* Attempt to allocate space for the new stack */
  new_stack = (int *)malloc(sizeof(int) * dim);
  if (new_stack == (int *)NULL)
  {
    return((SP_TUPLE *)NULL);
  }
  for (curr_dim = 0; curr_dim < dim; curr_dim++)
  {
    *((int *)(new_stack + (curr_dim))) = 0;
  }

  /* If there is currently a stack of sequences, 
     then the tuple is being redimensioned */
  if (tuple->seq != (int *)NULL)
  {
    /* If the old stack is bigger, then only copy the beginning */
    if (tuple->dimensions > dim)
    {
       (void *)memcpy((void *)new_stack, (void *)(tuple->seq),
                       sizeof(int) * dim);
    }
    else
    {
       (void *)memcpy((void *)new_stack, (void *)(tuple->seq),
                       sizeof(int) * (tuple->dimensions));
    }

    /* free the old stack */
    free(tuple->seq);
  }

  /* load in the new stack and dimension value */
  tuple->seq = new_stack;
  tuple->dimensions = dim;

  return(tuple);
}

⌨️ 快捷键说明

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