C的常用模板函数接口 --字符串操作、排序等

/**! 
   * 2018年1月29日10:28:50
   * Author: xiyuan255
   * 快速排序的模板代码
*/
#define __STATIC_INLINE  static inline

typedef Cali_Fix_t DataType;

__STATIC_INLINE uint32_t getCompValue( const void *pCurrent, uint8_t index )
{
   assert(pCurrent != NULL);
   
   return ((DataType *)pCurrent + index)->Original;
}

__STATIC_INLINE void copyValue( void *dstAddr, uint8_t index1, 
                     const void *srcAddr, uint8_t index2 )
{
   if (NULL == dstAddr && NULL == srcAddr)
      return;
   
   *((DataType *)dstAddr+index1) = *((DataType *)srcAddr+index2);
}

static void quickSort( void *pArr, int16_t left, int16_t right, 
                uint32_t ( *getCompValue )( const void *pCurrent, uint8_t index ),
                void ( *copyValue )( void *dstAddr, uint8_t index1, 
                           const void *srcAddr, uint8_t index2 ) ) 
{
   assert(pArr != NULL && getCompValue != NULL && copyValue != NULL);
   
   int16_t i = left;
   int16_t j = right;
   DataType compValue;
   
   if (left < right) {
      copyValue(&compValue, 0, pArr, i);
      while (i < j) {
         while ((i < j) && (getCompValue(pArr, j) >= getCompValue(&compValue, 0))) j--;
         copyValue(pArr, i, pArr, j);
         while ((i < j) && (getCompValue(pArr, i) <= getCompValue(&compValue, 0))) i++;
         copyValue(pArr, j, pArr, i);
      }
      copyValue(pArr, i, &compValue, 0);
      quickSort(pArr, left, i-1, getCompValue, copyValue);
      quickSort(pArr, i+1, right, getCompValue, copyValue);
   }

}

/* 这是个Lora项目在使用STM32为主控芯片下的快速排序的模板函数,
 * 其中: int16_t为signed short(2bytes)
         uint32_t为unsigned int(4bytes)
         uint8_t为unsigned char(1bytes)
   修改对应的类型,可直接使用VS 2010直接编译 */
/* 以下代码是测试动态库libxiyuan.so或静态库xiyuanlib.a的字符串
   操作和排序的Test.c源代码。
其中:(该版本是上次模板的V2.0版)
    1.获取子字符串在字符串的位置函数接口:
        该函数接口,空间复杂度较低,时间复杂度最大为O(n),效率较高。
    2.冒泡排序模板函数接口:
        整合度高,空间复杂度较低,时间复杂度为O(n^2),具有C++函数模板
        的效果,不管的C内置类型,还是结构体都可以实现排序。
    3.快排排序模板函数接口:
        整合度高,空间复杂度较低,时间复杂度为O(nlogn),具有C++函数模
        板的效果,不管的C内置类型,还是结构体都可以实现排序。
对应的库文件和头文件下载地址:
    https://download.csdn.net/download/xiyuan255/10630569 */
#include <stdio.h>
#include <stdlib.h>
#include "xy_sort_code.h"
#include "xy_string_code.h"

typedef struct sDataStduent
{
	int key;
	char *name;
	double score;
}__attribute__((packed)) DataStduent_t;


typedef struct scompdata
{
	int key;
	char *name;
	float value;
}compdata_t;


int getCompFunc( const void *CompValue1, const void *CompValue2 )
{
	if (((DataStduent_t *)CompValue1)->key == ((DataStduent_t *)CompValue2)->key)
		return 0;
	else if (((DataStduent_t *)CompValue1)->key > ((DataStduent_t *)CompValue2)->key)
		return 1;
	else
		return -1;

}

int getCompFunc1( const void *CompValue1, const void *CompValue2 )
{
	if (*(int *)CompValue1 == *(int *)CompValue2)
		return 0;
	else if (*(int *)CompValue1 > *(int *)CompValue2)
		return 1;
	else
		return -1;

}

int getCompFunc2( const void *CompValue1, const void *CompValue2 )
{
	if (((compdata_t *)CompValue1)->key == ((compdata_t *)CompValue2)->key)
		return 0;
	else if (((compdata_t *)CompValue1)->key > ((compdata_t *)CompValue2)->key)
		return 1;
	else
		return -1;

}

int main(int argc, const char *argv[])
{

  int i = 0, index = 0;
	
	
  DataStduent_t ds[6] = {{5, "ssss", 25.6}, 
			{8, "dddd", 66.66}, 
			{1, "ddee", 77.56},
			{6, "wedfd", 88.92},
			{5, "dsgvsd", 56.2123},
			{3, "541sdsd", 87.2},};
  BubbleSortTPL(ds, sizeof(DataStduent_t), sizeof(ds)/sizeof(DataStduent_t),getCompFunc);
  for (i = 0; i < sizeof(ds)/sizeof(DataStduent_t); i++)
	  printf("key:%d, %s, %10lf\n",ds[i].key,ds[i].name,ds[i].score);
		
		
  DataStduent_t ds1[6] = {{5, "ssss", 25.6}, 
			{8, "dddd", 66.66}, 
			{1, "ddee", 77.56},
			{6, "wedfd", 88.92},
			{5, "dsgvsd", 56.2123},
			{3, "541sdsd", 87.2},};
  QuickSortTPL(ds1,sizeof(DataStduent_t), sizeof(ds1)/sizeof(DataStduent_t),getCompFunc);
  for (i = 0; i < sizeof(ds1)/sizeof(DataStduent_t); i++)
      printf("key:%d, %s, %10lf\n",ds1[i].key,ds1[i].name,ds1[i].score);
		

  int intdata[10] = {4, 5, 7, 0, 5, 9, 45, 1, 9, 8};
  BubbleSortTPL(intdata, sizeof(int), sizeof(intdata)/sizeof(int), getCompFunc1);
  for (i = 0; i < sizeof(intdata)/sizeof(int); i++)
	  printf("[%d]: %d\n",i,intdata[i]);
		
		
  int intdata1[10] = {4, 5, 7, 0, 5, 9, 45, 1, 9, 8};
  QuickSortTPL(intdata1, sizeof(int), sizeof(intdata1)/sizeof(int), getCompFunc1);
  for (i = 0; i < sizeof(intdata1)/sizeof(int); i++)
  	  printf("[%d]: %d\n",i,intdata1[i]);
		

  compdata_t cd[8] = {{5, "ssss", 25.6}, 
			{8, "dddd", 66.66}, 
			{1, "ddee", 77.56},
			{6, "wedfd", 88.92},
			{5, "dsgvsd", 56.2123},
			{3, "541sdsd", 87.2},
			{18, "sdfsdv", 98.6},
			{0, "cxvxcv", 65.23},};
  BubbleSortTPL(cd, sizeof(compdata_t), sizeof(cd)/sizeof(compdata_t), getCompFunc2);
  for (i = 0; i < sizeof(cd)/sizeof(compdata_t); i++)
	  printf("key:%d, %s, %f\n",cd[i].key,cd[i].name,cd[i].value);

  char *pStr = "asdfsasafsf";
  char *pSub = "sasa";
  if (0 == GetSubLocation(pStr, pSub, &index))
  {
	  printf("%s is %s substring at %d location.\n",pStr,pSub,index);
  }
	
  return 0;
}
/* xy_sort_code.h code */
#ifndef __XY_SORT_CODE_H__
#define __XY_SORT_CODE_H__

/*
** Make sure we can call this stuff from C++.
*/
#ifdef __cplusplus
extern "C" {
#endif

#ifndef XIYUAN_API  
#define XIYUAN_API
#endif

/** 以下是一个示例,比较函数只要返回值按要求,内容实现由用户自己选择
 * \fn int getCompFunc( const void *CompValue1, const void *CompValue2 );
 * \brief 排序的比较函数 该函数需要用户根据实际数据结果重新实现
 * \param CompValue1  [in] 参数名称,比较数1
 * \param CompValue2  [in] 参数名称,比较数2
 * \return
 *		    0		: 比较数1 = 比较数2	\n
 *		    1		: 比较数1 > 比较数2	\n
 *		   -1		: 比较数1 < 比较数2 \n
 */
//int getCompFunc( const void *CompValue1, const void *CompValue2 );



//***  XIYUAN_API 接口函数说明  ***//
/** 
 * \fn XIYUAN_API int QuickSortTPL(void *pAssemble, size_t typeSize,
		int AssemLen, int ( *getCompFunc )(const void *, const void *)) 
 * \brief 该函数是快速排序的模板函数
 * \param pAssemble	  [in] 参数名称,要进行排序的集合
 * \param typeSize	  [in] 参数名称,集合元素的数据类型的长度
 * \param AssemLen  	[in] 参数名称,pAssemble集合的元素个数
 * \param getCompFunc [in] 参数名称,排序的比较函数
 * \return
 *			  0		: 成功			\n
 *		   -1		: 输入集合指针或比较函数指针为NULL	\n
 *		   -2		: 集合长度 <=0
 */
XIYUAN_API int QuickSortTPL(void *pAssemble, size_t typeSize, 
	int AssemLen, int ( *getCompFunc )(const void *, const void *));
	
	
/** 
 * \fn XIYUAN_API int BubbleSortTPL(void *pAssemble, size_t typeSize,
		int AssemLen, int ( *getCompFunc )(const void *, const void *)) 
 * \brief 该函数是冒泡排序的模板函数
 * \param pAssemble	  [in] 参数名称,要进行排序的集合
 * \param typeSize	  [in] 参数名称,集合元素的数据类型的长度
 * \param AssemLen  	[in] 参数名称,pAssemble集合的元素个数
 * \param getCompFunc [in] 参数名称,排序的比较函数
 * \return
 *		    0		: 成功			\n
 *		   -1		: 输入集合指针或比较函数指针为NULL	\n
 *		   -2		: 集合长度 <=0
 */
XIYUAN_API int BubbleSortTPL(void *pAssemble, size_t typeSize, 
	int AssemLen, int ( *getCompFunc )(const void *, const void *)); 

#ifdef __cplusplus
}
#endif

#endif /* __XY_SORT_CODE_H__ */

 

/* xy_string_code.h code */
#ifndef __XY_STRING_CODE_H__
#define __XY_STRING_CODE_H__
  
/*
** Make sure we can call this stuff from C++.
*/
#ifdef __cplusplus
extern "C" {
#endif

#ifndef XIYUAN_API  
#define XIYUAN_API
#endif

//***  XIYUAN_API 接口函数说明  ***//
/** 
 * \fn int GetSubLocation(const char *pStr, const char *pSub, int *pIndex)
 * \brief 判断子字符串pSub到字符串pStr中的位置
 * \param pStr	[in] 参数名称,字符串
 * \param pSub	[in] 参数名称,子字符串
 * \param pSub	[out] 参数名称,子字符串pSub到字符串pStr中的位置
 * \return
 * 		    0		: 字符串pStr中存在字符串pSub			\n
 * 		   -1		: 字符串pSub、pStr和pIndex出现指针为NULL	\n
 * 		   -2		: 子字符串长度大于字符串长度
 * 		   -3		: 字符串pStr中不存在字符串pSub    \n
 * 说明:该函数的空间复杂度较小,时间复杂度最大为T(O) = n
 */
XIYUAN_API int GetSubLocation(const char *pStr, const char *pSub, int *pIndex);

#ifdef __cplusplus
}
#endif

#endif /* __XY_STRING_CODE_H__ */

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值