core: turn picture-table into std::vector<>

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-05-30 15:00:28 +02:00 committed by bstoeger
parent 3cb04d230b
commit 9e3e0a5a05
29 changed files with 170 additions and 316 deletions

View file

@ -4,71 +4,28 @@
#if !defined(SUBSURFACE_MOBILE)
#include "metadata.h"
#endif
#include "range.h"
#include "subsurface-string.h"
#include "table.h"
#include <stdlib.h>
#include <string.h>
static void free_picture(struct picture picture)
bool picture::operator<(const struct picture &p) const
{
free(picture.filename);
picture.filename = NULL;
return std::tie(offset.seconds, filename) <
std::tie(p.offset.seconds, p.filename);
}
static int comp_pictures(struct picture a, struct picture b)
void add_picture(picture_table &t, struct picture newpic)
{
if (a.offset.seconds < b.offset.seconds)
return -1;
if (a.offset.seconds > b.offset.seconds)
return 1;
return strcmp(a.filename ?: "", b.filename ?: "");
auto it = std::lower_bound(t.begin(), t.end(), newpic);
t.insert(it, std::move(newpic));
}
static bool picture_less_than(struct picture a, struct picture b)
int get_picture_idx(const picture_table &t, const std::string &filename)
{
return comp_pictures(a, b) < 0;
}
/* picture table functions */
//static MAKE_GET_IDX(picture_table, struct picture, pictures)
static MAKE_GROW_TABLE(picture_table, struct picture, pictures)
static MAKE_GET_INSERTION_INDEX(picture_table, struct picture, pictures, picture_less_than)
MAKE_ADD_TO(picture_table, struct picture, pictures)
MAKE_REMOVE_FROM(picture_table, pictures)
MAKE_SORT(picture_table, struct picture, pictures, comp_pictures)
//MAKE_REMOVE(picture_table, struct picture, picture)
MAKE_CLEAR_TABLE(picture_table, pictures, picture)
/* Add a clone of a picture to the end of a picture table.
* Cloned means that the filename-string is copied. */
static void add_cloned_picture(struct picture_table *t, struct picture pic)
{
pic.filename = copy_string(pic.filename);
int idx = picture_table_get_insertion_index(t, pic);
add_to_picture_table(t, idx, pic);
}
void copy_pictures(const struct picture_table *s, struct picture_table *d)
{
int i;
clear_picture_table(d);
for (i = 0; i < s->nr; i++)
add_cloned_picture(d, s->pictures[i]);
}
void add_picture(struct picture_table *t, struct picture newpic)
{
int idx = picture_table_get_insertion_index(t, newpic);
add_to_picture_table(t, idx, newpic);
}
int get_picture_idx(const struct picture_table *t, const char *filename)
{
for (int i = 0; i < t->nr; ++i) {
if (same_string(t->pictures[i].filename, filename))
return i;
}
return -1;
return index_of_if(t, [&filename] (const picture &p)
{ return p.filename == filename; });
}
#if !defined(SUBSURFACE_MOBILE)
@ -113,37 +70,37 @@ static struct dive *nearest_selected_dive(timestamp_t timestamp)
// only add pictures that have timestamps between 30 minutes before the dive and
// 30 minutes after the dive ends
#define D30MIN (30 * 60)
static constexpr timestamp_t d30min = 30 * 60;
static bool dive_check_picture_time(const struct dive *d, timestamp_t timestamp)
{
return time_from_dive(d, timestamp) < D30MIN;
return time_from_dive(d, timestamp) < d30min;
}
/* Creates a picture and indicates the dive to which this picture should be added.
* The caller is responsible for actually adding the picture to the dive.
* If no appropriate dive was found, no picture is created and NULL is returned.
* If no appropriate dive was found, no picture is created and null is returned.
*/
struct picture *create_picture(const char *filename, timestamp_t shift_time, bool match_all, struct dive **dive)
std::pair<std::optional<picture>, dive *> create_picture(const std::string &filename, timestamp_t shift_time, bool match_all)
{
struct metadata metadata;
timestamp_t timestamp;
get_metadata(filename, &metadata);
get_metadata(filename.c_str(), &metadata);
timestamp = metadata.timestamp + shift_time;
*dive = nearest_selected_dive(timestamp);
struct dive *dive = nearest_selected_dive(timestamp);
if (!*dive)
return NULL;
if (get_picture_idx(&(*dive)->pictures, filename) >= 0)
return NULL;
if (!match_all && !dive_check_picture_time(*dive, timestamp))
return NULL;
if (!dive)
return { {}, nullptr };
if (get_picture_idx(dive->pictures, filename) >= 0)
return { {}, nullptr };
if (!match_all && !dive_check_picture_time(dive, timestamp))
return { {}, nullptr };
struct picture *picture = (struct picture *)malloc(sizeof(struct picture));
picture->filename = strdup(filename);
picture->offset.seconds = metadata.timestamp - (*dive)->when + shift_time;
picture->location = metadata.location;
return picture;
struct picture picture;
picture.filename = filename;
picture.offset.seconds = metadata.timestamp - dive->when + shift_time;
picture.location = metadata.location;
return { picture, dive };
}
bool picture_check_valid_time(timestamp_t timestamp, timestamp_t shift_time)