📄 tools.cpp
字号:
#include "header.h"
#include <stdio.h>
#include <stdlib.h>
#define TOOLS_C
#include "labels.h"
#include "tools.h"
#include "errors.h"
#include <string.h>
struct typelist snapshot_list[] = {
{SNAPSHOT_SAVEFILE, "file", NULL},
#ifndef NO_PIPED_COMMANDS
{SNAPSHOT_EXEC_CMD, "command", NULL},
{SNAPSHOT_EXEC_CMD_ASYNC, "command_async", NULL},
#endif /* NO_PIPED_COMMANDS */
{SNAPSHOT_SAVEFILE, NULL, NULL}}; /* default */
/* alloc_entries - allocate and initialize an entries-structure. Return NULL
on error. */
struct entries *alloc_entries(void)
{
struct entries *en;
if ((en = (struct entries *)malloc(sizeof(struct entries))) == NULL)
{
Msg(0, "could not allocate memory for entries, probably not enough free mem");
return NULL;
}
/* initialize to reasonable values */
en->dimension = 0;
en->topol = TOPOL_UNKNOWN;
en->neigh = NEIGH_UNKNOWN;
en->xdim = en->ydim = 0;
en->current = en->entry = NULL;
en->num_loaded = 0;
en->num_entries = 0;
en->fi = NULL;
en->lap = 0;
en->buffer = 0;
en->flags.loadmode = LOADMODE_ALL;
en->flags.totlen_known = 0;
en->flags.random_order = 0;
en->flags.skip_empty = 1;
en->flags.labels_needed = (!label_not_needed(-1));
return en;
}
int label_not_needed(int level)
{
static int label_level = 0;
if (level >= 0)
{
label_level = level;
}
return(label_level);
}
/* copy_entries - create a new entries structure with the same parameters
as the original. Doesn't copy data_entrys */
struct entries *copy_entries(struct entries *entr)
{
struct entries *New;
New = (struct entries *)alloc_entries();
if (New == NULL)
return NULL;
New->dimension = entr->dimension;
New->topol = entr->topol;
New->neigh = entr->neigh;
New->xdim = entr->xdim;
New->ydim = entr->ydim;
return New;
}
/* rewind_entries - go to the first entry in entries list. Returns pointer
to first data_entry. Loads data from file if it hasn't been loaded yet and
rewinds file if we are using buffered reading. */
struct data_entry *rewind_entries(struct entries *entries, eptr *ptr)
{
struct data_entry *current = entries->entry;
ptr->parent = entries;
if (current == NULL)
{
/* file not loaded into memory */
Msg(0, "rewind_entries failed\n");
return NULL;
}
ptr->current = current;
ptr->index = 0;
entries->lap++;
return current;
}
/* next_entry - Get next entry from the entries table. Returns NULL when
at end of table or end of file is encountered. If loadmode is buffered,
loads more data from file when needed. */
struct data_entry *next_entry(eptr *ptr)
{
struct entries *entries = ptr->parent;
struct data_entry *next, *current;
current = ptr->current;
if (current == NULL)
return NULL;
next = current->next;
ptr->current = next;
if (next)
ptr->index++;
return next;
}
/* close_entries - deallocates an entries-file. Frees memory allocated for
entry-list and closes the file associated with entries if there is one. */
void close_entries(struct entries *entries)
{
if (entries)
{
/* deallocate data */
if (entries->entry)
free_entrys(entries->entry);
/* free memory allocated for structure */
free(entries);
}
}
/* free_entrys - Free a list of entries */
void free_entrys(struct data_entry *data)
{
struct data_entry *tmp;
for (;data != NULL; data = tmp)
{
tmp = data->next;
free_entry(data);
}
}
/* initialize and possibly allocate room for data_entry. If entry is NULL,
a new entry is allocated and initialized. If entry is a pointer to an
old entry, the old entry is re-initialized. Return NULL on error. */
struct data_entry *init_entry(struct entries *entr, struct data_entry *entry)
{
clear_err();
if (entry == NULL)
{
entry = (struct data_entry *)malloc(sizeof(struct data_entry));
if (entry == NULL)
{
/*Msg(0, "alloc_entry");*/
ERROR(ERR_NOMEM);
return NULL;
}
entry->fixed = NULL;
entry->next = NULL;
entry->mask = NULL;
entry->lab.label_array = NULL;
entry->num_labs = 0;
entry->points = (float*)calloc(entr->dimension, sizeof(float));
if (entry->points == NULL)
{
free_entry(entry);
Msg(0, "alloc_entry");
ERROR(ERR_NOMEM);
return NULL;
}
}
/* discard mask */
if (entry->mask)
{
free(entry->mask);
entry->mask = NULL;
}
/* discard fixed point */
if (entry->fixed)
{
free(entry->fixed);
entry->fixed = NULL;
}
clear_entry_labels(entry);
entry->weight = 0;
return entry;
}
/* copy_entry - Copy one entry (next==NULL) */
struct data_entry *copy_entry(struct entries *entries, struct data_entry *data)
{
int i;
struct data_entry *tmp;
clear_err();
/* allocate memory for the copy */
tmp = alloc_entry(entries);
if (tmp == NULL)
return NULL;
/* copy data vector */
for (i = 0; i < entries->dimension; i++)
tmp->points[i] = data->points[i];
/* copy labels */
copy_entry_labels(tmp, data);
/* copy mask */
if (data->mask)
{
if ((tmp->mask = (char*)malloc(entries->dimension)) == NULL)
{
Msg(0, "Can't allocate memory for mask");
free_entry(tmp);
ERROR(ERR_NOMEM);
return NULL;
}
memcpy(tmp->mask, data->mask, entries->dimension);
}
/* copy other stuff */
tmp->weight = data->weight;
/* copy fixed point */
if (data->fixed)
{
if ((tmp->fixed = (struct fixpoint *)malloc(sizeof(struct fixpoint))) == NULL)
{
free_entry(tmp);
ERROR(ERR_NOMEM);
return NULL;
}
memcpy(tmp->fixed, data->fixed, sizeof(struct fixpoint));
}
tmp->next = NULL;
return(tmp);
}
/* free_entry - deallocates a data_entry. */
void free_entry(struct data_entry *entry)
{
if (entry)
{
if (entry->points)
free(entry->points);
if (entry->fixed)
free(entry->fixed);
if (entry->mask)
free(entry->mask);
clear_entry_labels(entry);
free(entry);
}
}
/*******************************************************************
* Routines to handle files that contain the learning rate values *
*******************************************************************/
int alpha_read(float *alpha, long noc, char *infile)
{
return -1;
#if SAMI
long i;
char basename[STR_LNG];
FILE *fp;
struct file_info *fi;
if (infile == NULL)
return 0;
strcpy(basename, infile);
strtok(basename, ".");
strcat(basename, ".lra");
fi = open_file(basename, "r");
if (fi == NULL)
{
ifverbose(1)
{
Msg(0, "Can't open alpha file %s", basename);
}
return(0);
}
fp = fi2fp(fi);
for (i = 0; i < noc; i++)
{
if (fscanf(fp, "%g\n", &(alpha[i])) < 0)
return(0);
}
close_file(fi);
return(1);
#endif
}
int alpha_write(float *alpha, long noc, char *outfile)
{
long i;
char basename[STR_LNG];
FILE *fp;
struct file_info *fi;
if (outfile == NULL)
return 0;
strcpy(basename, outfile);
strtok(basename, ".");
strcat(basename, ".lra");
fi = open_file(basename, "w+");
if (fi == NULL)
{
Msg(0, "Can't open alpha file %s for writing", basename);
return 0;
}
fp = fi2fp(fi);
for (i = 0; i < noc; i++)
{
fprintf(fp, "%g\n", alpha[i]);
}
close_file(fi);
return 0;
}
void invalidate_alphafile(char *outfile)
{
int i;
char basename[STR_LNG];
FILE *fp;
if (outfile == NULL)
return;
strcpy(basename, outfile);
strtok(basename, ".");
strcat(basename, ".lra");
fp = fopen(basename, "r");
if (fp != NULL)
{
ifverbose(1)
fprintf(fp, "Removing the learning rate file %s\n", basename);
fclose(fp);
i = remove(basename);
if (i)
{
Msg(0, "Can not remove %s", basename);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -