summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/ranking/Normalizer.java
blob: 0d86e1409c395745a92917cb5aa00146a052578b (plain) (blame)
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.ranking;

abstract class Normalizer {

    protected double[] data;
    protected int size = 0;

    private static int initialCapacity(int hint) {
        for (int capacity = 64; capacity < 4096; capacity *= 2) {
            if (hint <= capacity) {
                return capacity;
            }
        }
        return 4096;
    }

    Normalizer(int sizeHint) {
        this.data = new double[initialCapacity(sizeHint)];
    }

    int addInput(double value) {
        if (size == data.length) {
            int newSize = size * 2;
            var tmp = new double[newSize];
            System.arraycopy(data, 0, tmp, 0, size);
            this.data = tmp;
        }
        data[size] = value;
        return size++;
    }

    double getOutput(int index) { return data[index]; }

    abstract void normalize();

    abstract String normalizing();
}