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)
38 : {
39 0 : DeInitVector(&vector->vector);
40 0 : }
41 :
42 0 : SortVector *CreateSortVector(size_t itemSize, FnBinaryCompare pfnCmp, void *appInfo)
43 : {
44 0 : SortVector *sortVector = (SortVector *)mmMalloc(sizeof(SortVector));
45 0 : if (sortVector == NULL) {
46 0 : return NULL;
47 : }
48 0 : InitSortVector(sortVector, itemSize, pfnCmp, appInfo);
49 0 : return sortVector;
50 : }
51 :
52 0 : void DestroySortVector(SortVector *sortVector)
53 : {
54 0 : DeInitSortVector(sortVector);
55 0 : mmFree(sortVector);
56 0 : }
57 :
58 0 : size_t CapacitySortVector(SortVector *sortVector, size_t capacity)
59 : {
60 0 : return CapacityVector(&sortVector->vector, capacity);
61 : }
62 :
63 0 : void *SortVectorAt(SortVector *sortVector, size_t index)
64 : {
65 0 : return VectorAt(&sortVector->vector, index);
66 : }
67 :
68 0 : static int SortVectorBinaryCmp(void *a, void *b, void *appInfo)
69 : {
70 0 : SortVector *sortVector = (SortVector *)appInfo;
71 0 : return sortVector->fnCmp(a, b, sortVector->appInfo);
72 : }
73 :
74 0 : static void *SortVectorGet(void *appInfo, size_t index)
75 : {
76 0 : return SortVectorAt((SortVector *)appInfo, index);
77 : }
78 :
79 0 : static int FindSortVectorClosest(SortVector *sortVector, void *key, size_t *closestIndex)
80 : {
81 0 : return BinarySearchClosest(
82 0 : sortVector, VectorSize(&sortVector->vector), key, SortVectorGet, SortVectorBinaryCmp, closestIndex);
83 : }
84 :
85 0 : size_t FindSortVector(SortVector *sortVector, void *key)
86 : {
87 : size_t index;
88 0 : int cmpRst = FindSortVectorClosest(sortVector, key, &index);
89 0 : return (cmpRst == 0) ? index : SortVectorSize(sortVector);
90 : }
91 :
92 0 : void *SortVectorAtKey(SortVector *sortVector, void *key)
93 : {
94 0 : return VectorAt(&sortVector->vector, FindSortVector(sortVector, key));
95 : }
96 :
97 0 : void *EmplaceSortVector(SortVector *sortVector, void *data)
98 : {
99 : size_t index;
100 0 : int cmpRst = FindSortVectorClosest(sortVector, data, &index);
101 0 : if (cmpRst == 0) {
102 0 : size_t size = sortVector->vector.itemSize;
103 0 : void *keyData = VectorAt(&sortVector->vector, index);
104 0 : errno_t ret = memcpy_s(keyData, size, data, size);
105 0 : if (ret != EOK) {
106 0 : return NULL;
107 : }
108 0 : return keyData;
109 : }
110 :
111 0 : if (cmpRst > 0) {
112 0 : index++;
113 : }
114 0 : return EmplaceVector(&sortVector->vector, index, data);
115 : }
116 :
117 0 : void RemoveSortVector(SortVector *sortVector, size_t index)
118 : {
119 0 : RemoveVector(&sortVector->vector, index);
120 0 : return;
121 : }
122 : #ifdef __cplusplus
123 : }
124 : #endif
|