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__ */

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章