1/*	$NetBSD: vector.h,v 1.2 2021/12/18 23:45:07 riastradh Exp $	*/
2
3/*
4 * Copyright 2012-15 Advanced Micro Devices, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: AMD
25 *
26 */
27
28#ifndef __DAL_VECTOR_H__
29#define __DAL_VECTOR_H__
30
31struct vector {
32	uint8_t *container;
33	uint32_t struct_size;
34	uint32_t count;
35	uint32_t capacity;
36	struct dc_context *ctx;
37};
38
39bool dal_vector_construct(
40	struct vector *vector,
41	struct dc_context *ctx,
42	uint32_t capacity,
43	uint32_t struct_size);
44
45struct vector *dal_vector_create(
46	struct dc_context *ctx,
47	uint32_t capacity,
48	uint32_t struct_size);
49
50/* 'initial_value' is optional. If initial_value not supplied,
51 * each "structure" in the vector will contain zeros by default. */
52struct vector *dal_vector_presized_create(
53	struct dc_context *ctx,
54	uint32_t size,
55	void *initial_value,
56	uint32_t struct_size);
57
58void dal_vector_destruct(
59	struct vector *vector);
60
61void dal_vector_destroy(
62	struct vector **vector);
63
64uint32_t dal_vector_get_count(
65	const struct vector *vector);
66
67/* dal_vector_insert_at
68 * reallocate container if necessary
69 * then shell items at right and insert
70 * return if the container modified
71 * do not check that index belongs to container
72 * since the function is private and index is going to be calculated
73 * either with by function or as get_count+1 */
74bool dal_vector_insert_at(
75	struct vector *vector,
76	const void *what,
77	uint32_t position);
78
79bool dal_vector_append(
80	struct vector *vector,
81	const void *item);
82
83/* operator[] */
84void *dal_vector_at_index(
85	const struct vector *vector,
86	uint32_t index);
87
88void dal_vector_set_at_index(
89	const struct vector *vector,
90	const void *what,
91	uint32_t index);
92
93/* create a clone (copy) of a vector */
94struct vector *dal_vector_clone(
95	const struct vector *vector_other);
96
97/* dal_vector_remove_at_index
98 * Shifts elements on the right from remove position to the left,
99 * removing an element at position by overwrite means*/
100bool dal_vector_remove_at_index(
101	struct vector *vector,
102	uint32_t index);
103
104uint32_t dal_vector_capacity(const struct vector *vector);
105
106bool dal_vector_reserve(struct vector *vector, uint32_t capacity);
107
108void dal_vector_clear(struct vector *vector);
109
110/***************************************************************************
111 * Macro definitions of TYPE-SAFE versions of vector set/get functions.
112 ***************************************************************************/
113
114#define DAL_VECTOR_INSERT_AT(vector_type, type_t) \
115	static bool vector_type##_vector_insert_at( \
116		struct vector *vector, \
117		type_t what, \
118		uint32_t position) \
119{ \
120	return dal_vector_insert_at(vector, what, position); \
121}
122
123#define DAL_VECTOR_APPEND(vector_type, type_t) \
124	static bool vector_type##_vector_append( \
125		struct vector *vector, \
126		type_t item) \
127{ \
128	return dal_vector_append(vector, item); \
129}
130
131/* Note: "type_t" is the ONLY token accepted by "checkpatch.pl" and by
132 * "checkcommit" as *return type*.
133 * For uniformity reasons "type_t" is used for all type-safe macro
134 * definitions here. */
135#define DAL_VECTOR_AT_INDEX(vector_type, type_t) \
136	static type_t vector_type##_vector_at_index( \
137		const struct vector *vector, \
138		uint32_t index) \
139{ \
140	return dal_vector_at_index(vector, index); \
141}
142
143#define DAL_VECTOR_SET_AT_INDEX(vector_type, type_t) \
144	static void vector_type##_vector_set_at_index( \
145		const struct vector *vector, \
146		type_t what, \
147		uint32_t index) \
148{ \
149	dal_vector_set_at_index(vector, what, index); \
150}
151
152#endif /* __DAL_VECTOR_H__ */
153