VLC 4.0.0-dev
vlc_diffutil.h
Go to the documentation of this file.
1/******************************************************************************
2 * vlc_diffutil.h
3 ******************************************************************************
4 * Copyright (C) 2022 VLC authors and VideoLAN
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
20#ifndef VLC_DIFFUTIL_H
21#define VLC_DIFFUTIL_H
22
23#include <vlc_common.h>
24#include <vlc_vector.h>
25
26/**
27 * this struture defines callback to access and compare elements from
28 * the old and the new list
29 */
30typedef struct {
31 /// return the size of the old list @a list
32 uint32_t (*getOldSize)(const void* list);
33 /// return the size of the new list @a list
34 uint32_t (*getNewSize)(const void* list);
35 /// compare 2 elements
36 bool (*isSame)(const void* listOld, uint32_t oldIndex, const void* listNew, uint32_t newIndex);
38
39typedef struct {
40 /**
41 * notify that the item from @a listNew at position @a posNew is inserted in list @a listOld at position @a posOld
42 *
43 * @param opaque user data from function vlc_diffutil_walk_snake
44 * @param listOld pointer to the old model
45 * @param posOld position of the element inserted in the old model (before removal)
46 * @param listNew pointer to the new model
47 * @param posNew position of the element inserted in the new model
48 */
49 void (*insert)(void* opaque, const void* listOld, uint32_t posOld, const void* listNew, uint32_t posNew);
50 /**
51 * notify that the item from @a listOld at position @a posOld is removed
52 * @param opaque user data from function vlc_diffutil_walk_snake
53 * @param listOld pointer to the old model
54 * @param posOld position of the element removed in the old model
55 * @param listNew pointer to the new model
56 * @param posNew position of the element removed in the new model (before removal)
57 */
58 void (*remove)(void* opaque, const void* listOld, uint32_t posOld, const void* listNew, uint32_t posNew);
59 /**
60 * notify that the item as @a posOld from the old list @a listOld is unchanged, the respective item
61 * position in the new list is at the positoin @a posNew in @a listNew
62 */
63 void (*equal)(void* opaque, const void* listOld, uint32_t posOld, const void* listNew, uint32_t posNew);
65
69 ///items have been added to the list
71 ///items have been removed from the list
73 ///items have been moved within the list
75 ///current change should be ignored
77};
78
79
80/**
81 * represent a change to the model, each change assumes that previous changes
82 * have already been applied
83 *
84 * for instance with a model "aBcDef", the operations [remove(index=1, count=1), remove(index=2, count=1)]
85 * will result in "acef" (with "acDef" as intermediary step)
86 */
87typedef struct {
88 union {
89 /**
90 * the data positionned at newModel[ y ] is inserted at position index in the current model
91 *
92 * @example
93 * model = "abcdefg"
94 * newModel[3] = 'X'
95 * after operation insert(y=3, index = 3), model will be
96 * model = "abcXdefg"
97 */
98 struct {
99 /// data position in the old model
100 uint32_t x;
101 /// data position in the new model
102 uint32_t y;
103 /// insertion position in the updated model
104 uint32_t index;
105 } insert;
107 /**
108 * the data positionned at oldModel[ y ] is removed at position index in the current model
109 *
110 * @example
111 * model = "abCdefg"
112 * oldModel[4] = 'C'
113 * after operation remove(x=4, index = 2), model will be
114 * model = "abdefg"
115 */
116 struct {
117 /// data position in the old model
118 uint32_t x;
119 /// data position in the new model
120 uint32_t y;
121 /// removal position in the updated model
122 uint32_t index;
123 } remove;
125 /**
126 * moves the data from position model[ from ] to model[ to ]
127 * the data is available either at newModel[ y ] or oldModel[ x ]
128 *
129 * the positions @a from and @a to are given in the referenrial before the operation
130 *
131 * @example
132 * model = "aBCdefg"
133 * after operation move(from=1, to=5, count=2), model will be
134 * model = "adeCBfg"
135 */
136 struct {
137 /// move origin
138 uint32_t from;
139 /// move destination
140 uint32_t to;
141 /// data position in the old model
142 uint32_t x;
143 /// data position in the new model
144 uint32_t y;
145 } move;
147 } op;
149 /// type of change operation
150 enum vlc_diffutil_op_type type;
152 /// number of elements to be inserted/removed/moved
153 uint32_t count;
155
156typedef struct VLC_VECTOR(vlc_diffutil_change_t) vlc_diffutil_changelist_t;
159 /// try to transform an insertion with a matching supression into a move operation
161 /**
162 * aggreate similar consecutive operations into a single operation
163 * for instance this:
164 * [{INSERT, i=5}{INSERT, x=6}{REMOVE, i=10}{REMOVE, i=10}{REMOVE, i=10}]
165 * would be tranformed into:
166 * [{INSERT, i=5, count=2}{REMOVE, i=10, count=3}]
167 */
170
171/**
172 * vlc_diffutil_build_snake compute a diff model
173 * between the @a dataOld model and the @a dataNew model. This model can be
174 * processed manually using vlc_diffutil_walk_snake or translated into a change list using
175 * vlc_diffutil_build_change_list
176 *
177 * @param diffOp callback to compare the elements from the old and new model
178 * @param dataOld old model
179 * @param dataNew new model
180 * @return the diff model, NULL on error
181 */
182VLC_API struct diffutil_snake_t* vlc_diffutil_build_snake(const vlc_diffutil_callback_t* diffOp, const void* dataOld, const void* dataNew);
183
184/// free the snake created by vlc_diffutil_build_snake
186
187/**
188 * iterate over the changelist and callback user on each operation (keep/insert/remove)
189 *
190 * @param snake the snake created with vlc_diffutil_build_snake
191 * @param snakeOp snake callback
192 * @param cbData user data for snake callbacks
193 * @param diffOp callbacks used in vlc_diffutil_build_snake
194 * @param dataOld old model
195 * @param dataNew new model
196 * @return false on error
197 *
198 * @warning @a dataOld and @a dataNew should not be altered during the operation
199 */
201 const diffutil_snake_t* snake,
202 const vlc_diffutil_snake_callback_t* snakeOp, void* cbData,
203 const vlc_diffutil_callback_t* diffOp, const void* dataOld, const void* dataNew);
204
205/**
206 * vlc_diffutil_build_change_list creates a list of changes to apply to transform @a dataOld into @a dataNew
207 *
208 * @param snake the snake created with vlc_diffutil_build_snake
209 * @param diffOp callbacks used in vlc_diffutil_build_snake
210 * @param dataOld old model
211 * @param dataNew new model
212 * @param flags vlc_diffutil_result_flag flags
213 * @return the list of changes, NULL on error
214 */
216 const struct diffutil_snake_t* snake,
217 const vlc_diffutil_callback_t* diffOp, const void* dataOld, const void* dataNew,
218 int flags);
219
220/// free the changelist created by vlc_diffutil_build_change_list
222
223
224#endif // VLC_DIFFUTIL_H
struct vlc_param ** list
Definition: core.c:402
size_t count
Definition: core.c:403
#define VLC_API
Definition: fourcc_gen.c:31
Definition: diffutil.c:34
this struture defines callback to access and compare elements from the old and the new list
Definition: vlc_diffutil.h:31
represent a change to the model, each change assumes that previous changes have already been applied
Definition: vlc_diffutil.h:88
uint32_t y
data position in the new model
Definition: vlc_diffutil.h:103
uint32_t index
insertion position in the updated model
Definition: vlc_diffutil.h:105
uint32_t count
number of elements to be inserted/removed/moved
Definition: vlc_diffutil.h:154
enum vlc_diffutil_op_type type
type of change operation
Definition: vlc_diffutil.h:151
uint32_t x
data position in the old model
Definition: vlc_diffutil.h:101
uint32_t from
move origin
Definition: vlc_diffutil.h:139
uint32_t to
move destination
Definition: vlc_diffutil.h:141
Definition: vlc_diffutil.h:157
Definition: vlc_diffutil.h:40
This file is a collection of common definitions and types.
void vlc_diffutil_free_change_list(vlc_diffutil_changelist_t *changelist)
free the changelist created by vlc_diffutil_build_change_list
Definition: diffutil.c:612
vlc_diffutil_changelist_t * vlc_diffutil_build_change_list(const struct diffutil_snake_t *snake, const vlc_diffutil_callback_t *diffOp, const void *dataOld, const void *dataNew, int flags)
vlc_diffutil_build_change_list creates a list of changes to apply to transform dataOld into dataNew
void vlc_diffutil_free_snake(struct diffutil_snake_t *snake)
free the snake created by vlc_diffutil_build_snake
Definition: diffutil.c:300
vlc_diffutil_op_type
Definition: vlc_diffutil.h:69
@ VLC_DIFFUTIL_OP_MOVE
items have been moved within the list
Definition: vlc_diffutil.h:75
@ VLC_DIFFUTIL_OP_INSERT
items have been added to the list
Definition: vlc_diffutil.h:71
@ VLC_DIFFUTIL_OP_REMOVE
items have been removed from the list
Definition: vlc_diffutil.h:73
@ VLC_DIFFUTIL_OP_IGNORE
current change should be ignored
Definition: vlc_diffutil.h:77
vlc_diffutil_result_flag
Definition: vlc_diffutil.h:159
@ VLC_DIFFUTIL_RESULT_AGGREGATE
aggreate similar consecutive operations into a single operation for instance this: [{INSERT,...
Definition: vlc_diffutil.h:169
@ VLC_DIFFUTIL_RESULT_MOVE
try to transform an insertion with a matching supression into a move operation
Definition: vlc_diffutil.h:161
struct diffutil_snake_t * vlc_diffutil_build_snake(const vlc_diffutil_callback_t *diffOp, const void *dataOld, const void *dataNew)
vlc_diffutil_build_snake compute a diff model between the dataOld model and the dataNew model.
Definition: diffutil.c:248
bool vlc_diffutil_walk_snake(const diffutil_snake_t *snake, const vlc_diffutil_snake_callback_t *snakeOp, void *cbData, const vlc_diffutil_callback_t *diffOp, const void *dataOld, const void *dataNew)
iterate over the changelist and callback user on each operation (keep/insert/remove)
Definition: diffutil.c:309
This provides convenience helpers for vectors.