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 : #ifndef C_BASE_VECTOR_H
11 : #define C_BASE_VECTOR_H
12 : #include "c_base.h"
13 : #ifdef __cplusplus
14 : extern "C" {
15 : #endif
16 : typedef struct {
17 : size_t itemSize;
18 : size_t size;
19 : size_t capacity;
20 : uint8_t* data;
21 : FnDestroy pfnDestroyItem;
22 : } Vector;
23 :
24 : #define NewVector(objType) CreateVector(sizeof(objType))
25 :
26 : // itemSize 不能为0,请调用者保证
27 : void InitVector(Vector* vector, size_t itemSize);
28 1 : static inline void SetVectorDestroyItem(Vector* vector, FnDestroy pfnDestroyItem)
29 : {
30 1 : vector->pfnDestroyItem = pfnDestroyItem;
31 1 : };
32 : void DeInitVector(Vector* vector);
33 :
34 : // itemSize 不能为0,请调用者保证
35 : Vector* CreateVector(size_t itemSize);
36 : void DestroyVector(Vector* vector);
37 : void ClearVector(Vector* vector);
38 2 : static inline size_t VectorSize(const Vector* vector) { return vector->size; };
39 : size_t ReSizeVector(Vector* vector, size_t size);
40 : size_t CapacityVector(Vector* vector, size_t capacity);
41 : void* EmplaceVector(Vector* vector, size_t index, void* data);
42 : void* EmplaceBackVector(Vector* vector, void* data);
43 : void* EmplaceHeadVector(Vector* vector, void* data);
44 : void RemoveVector(Vector* vector, size_t index);
45 : void MoveVector(Vector* src, Vector* desc);
46 : void* VectorAt(Vector* vector, size_t index);
47 : const void* ConstVectorAt(const Vector* vector, size_t index);
48 : #ifdef __cplusplus
49 : }
50 : #endif
51 : #endif // C_BASE_VECTOR_H
|