File: vector_ops.c

package info (click to toggle)
libpuzzle 0.11-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 2,092 kB
  • sloc: sh: 9,723; ansic: 1,811; makefile: 977; php: 269; sql: 148
file content (95 lines) | stat: -rw-r--r-- 2,874 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "puzzle_common.h"
#include "puzzle_p.h"
#include "puzzle.h"
#include "globals.h"

int puzzle_vector_sub(PuzzleContext * const context,
                      PuzzleCvec * const cvecr,
                      const PuzzleCvec * const cvec1,
                      const PuzzleCvec * const cvec2,
                      const int fix_for_texts)
{
    size_t remaining;
    signed char c1, c2, cr;

    (void) context;
    if (cvec1->sizeof_vec != cvec2->sizeof_vec ||
        cvec1->sizeof_vec <= (size_t) 0U) {
        puzzle_err_bug(__FILE__, __LINE__);
    }
    if (cvecr->vec != NULL) {
        puzzle_err_bug(__FILE__, __LINE__);
    }
    cvecr->sizeof_vec = cvec1->sizeof_vec;    
    if ((cvecr->vec = calloc(cvecr->sizeof_vec, sizeof *cvecr->vec)) == NULL) {
        return -1;
    }
    remaining = cvec1->sizeof_vec;
    if (fix_for_texts != 0) {
        do {
            remaining--;
            c1 = cvec1->vec[remaining];
            c2 = cvec2->vec[remaining];
            if ((c1 == 0 && c2 == -2) || (c1 == -2 && c2 == 0)) {
                cr = -3;
            } else if ((c1 == 0 && c2 == +2) || (c1 == +2 && c2 == 0)) {
                cr = +3;
            } else {
                cr = c1 - c2;
            }
            cvecr->vec[remaining] = cr;
        } while (remaining > (size_t) 0U);        
    } else {
        do {
            remaining--;
            cvecr->vec[remaining] =
                cvec1->vec[remaining] - cvec2->vec[remaining];
        } while (remaining > (size_t) 0U);
    }
    return 0;
}

double puzzle_vector_euclidean_length(PuzzleContext * const context,
                                      const PuzzleCvec * const cvec)
{
    unsigned long t = 0U;
    unsigned long c;
    int c2;
    size_t remaining;

    (void) context;
    if ((remaining = cvec->sizeof_vec) <= (size_t) 0U) {
        return 0.0;
    }
    do {
        remaining--;
        c2 = (int) cvec->vec[remaining];
        c = (unsigned long) (c2 * c2);
        if (ULONG_MAX - t < c) {
            puzzle_err_bug(__FILE__, __LINE__);
        }
        t += c;
    } while (remaining > (size_t) 0U);
    
    return sqrt((double) t);
}

double puzzle_vector_normalized_distance(PuzzleContext * const context,
                                         const PuzzleCvec * const cvec1,
                                         const PuzzleCvec * const cvec2,
                                         const int fix_for_texts)
{
    PuzzleCvec cvecr;
    double dt, dr;

    puzzle_init_cvec(context, &cvecr);
    puzzle_vector_sub(context, &cvecr, cvec1, cvec2, fix_for_texts);
    dt = puzzle_vector_euclidean_length(context, &cvecr);
    puzzle_free_cvec(context, &cvecr);
    dr = puzzle_vector_euclidean_length(context, cvec1)
        + puzzle_vector_euclidean_length(context, cvec2);
    if (dr == 0.0) {
        return 0.0;
    }
    return dt / dr;
}