Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 : #include "securec.h"
11 : #include "binary_search.h"
12 : #include "vector.h"
13 : #include "mmpa_api.h"
14 : #include "sort_vector.h"
15 : #ifdef __cplusplus
16 : extern "C" {
17 : #endif
18 :
19 0 : static int DefaultCmpFunc(void* a, void* b, void* appInfo)
20 : {
21 0 : SortVector* sortVector = (SortVector*)appInfo;
22 0 : return memcmp(a, b, sortVector->vector.itemSize);
23 : }
24 :
25 0 : void InitSortVector(SortVector* sortVector, size_t itemSize, FnBinaryCompare pfnCmp, void* appInfo)
26 : {
27 0 : InitVector(&sortVector->vector, itemSize);
28 0 : if (pfnCmp != NULL) {
29 0 : sortVector->fnCmp = pfnCmp;
30 0 : sortVector->appInfo = appInfo;
31 : } else {
32 0 : sortVector->fnCmp = DefaultCmpFunc;
33 0 : sortVector->appInfo = sortVector;
34 : }
35 0 : }
36 :
37 0 : void DeInitSortVector(SortVector* vector) { DeInitVector(&vector->vector); }
38 :
39 0 : SortVector* CreateSortVector(size_t itemSize, FnBinaryCompare pfnCmp, void* appInfo)
40 : {
41 0 : SortVector* sortVector = (SortVector*)mmMalloc(sizeof(SortVector));
42 0 : if (sortVector == NULL) {
43 0 : return NULL;
44 : }
45 0 : InitSortVector(sortVector, itemSize, pfnCmp, appInfo);
46 0 : return sortVector;
47 : }
48 :
49 0 : void DestroySortVector(SortVector* sortVector)
50 : {
51 0 : DeInitSortVector(sortVector);
52 0 : mmFree(sortVector);
53 0 : }
54 :
55 0 : size_t CapacitySortVector(SortVector* sortVector, size_t capacity)
56 : {
57 0 : return CapacityVector(&sortVector->vector, capacity);
58 : }
59 :
60 0 : void* SortVectorAt(SortVector* sortVector, size_t index) { return VectorAt(&sortVector->vector, index); }
61 :
62 0 : static int SortVectorBinaryCmp(void* a, void* b, void* appInfo)
63 : {
64 0 : SortVector* sortVector = (SortVector*)appInfo;
65 0 : return sortVector->fnCmp(a, b, sortVector->appInfo);
66 : }
67 :
68 0 : static void* SortVectorGet(void* appInfo, size_t index) { return SortVectorAt((SortVector*)appInfo, index); }
69 :
70 0 : static int FindSortVectorClosest(SortVector* sortVector, void* key, size_t* closestIndex)
71 : {
72 0 : return BinarySearchClosest(
73 0 : sortVector, VectorSize(&sortVector->vector), key, SortVectorGet, SortVectorBinaryCmp, closestIndex);
74 : }
75 :
76 0 : size_t FindSortVector(SortVector* sortVector, void* key)
77 : {
78 : size_t index;
79 0 : int cmpRst = FindSortVectorClosest(sortVector, key, &index);
80 0 : return (cmpRst == 0) ? index : SortVectorSize(sortVector);
81 : }
82 :
83 0 : void* SortVectorAtKey(SortVector* sortVector, void* key)
84 : {
85 0 : return VectorAt(&sortVector->vector, FindSortVector(sortVector, key));
86 : }
87 :
88 0 : void* EmplaceSortVector(SortVector* sortVector, void* data)
89 : {
90 : size_t index;
91 0 : int cmpRst = FindSortVectorClosest(sortVector, data, &index);
92 0 : if (cmpRst == 0) {
93 0 : size_t size = sortVector->vector.itemSize;
94 0 : void* keyData = VectorAt(&sortVector->vector, index);
95 0 : errno_t ret = memcpy_s(keyData, size, data, size);
96 0 : if (ret != EOK) {
97 0 : return NULL;
98 : }
99 0 : return keyData;
100 : }
101 :
102 0 : if (cmpRst > 0) {
103 0 : index++;
104 : }
105 0 : return EmplaceVector(&sortVector->vector, index, data);
106 : }
107 :
108 0 : void RemoveSortVector(SortVector* sortVector, size_t index)
109 : {
110 0 : RemoveVector(&sortVector->vector, index);
111 0 : return;
112 : }
113 : #ifdef __cplusplus
114 : }
115 : #endif
|