File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -283,4 +283,47 @@ function dtutils.check_os(operating_systems)
283
283
return false
284
284
end
285
285
286
+ dtutils .libdoc .functions [" find_image_by_id" ] = {
287
+ Name = [[ find_image_by_id]] ,
288
+ Synopsis = [[ look up an image by ID in the database]] ,
289
+ Usage = [[ local du = require "lib/dtutils"
290
+ local img = du.find_image_by_id(imgid)
291
+ id - int - the ID to look up
292
+ ]] ,
293
+ Description = [[ find_image_by_id looks up an image by ID in the database.]] ,
294
+ Return_Value = [[ result - dt_lua_image_t - image with the given ID if found, nil if not]] ,
295
+ Limitations = [[ ]] ,
296
+ Example = [[ ]] ,
297
+ See_Also = [[ ]] ,
298
+ Reference = [[ ]] ,
299
+ License = [[ ]] ,
300
+ Copyright = [[ ]] ,
301
+ }
302
+
303
+ function dtutils .find_image_by_id (imgid )
304
+ if # dt .database == 0 or imgid > dt .database [# dt .database ].id then
305
+ return nil
306
+ end
307
+ local min = 1
308
+ local max = # dt .database
309
+ while (max - min )// 2 > 0 do
310
+ local mid = min + (max - min )// 2
311
+ local midID = dt .database [mid ].id
312
+ if imgid == midID then
313
+ return dt .database [mid ]
314
+ elseif imgid < midID then
315
+ max = mid - 1
316
+ else
317
+ min = mid + 1
318
+ end
319
+ end
320
+ if dt .database [min ].id == imgid then
321
+ return dt .database [min ]
322
+ elseif dt .database [max ].id == imgid then
323
+ return dt .database [max ]
324
+ else
325
+ return nil
326
+ end
327
+ end
328
+
286
329
return dtutils
You can’t perform that action at this time.
0 commit comments