summaryrefslogtreecommitdiff
path: root/modules/overloading/macros/%c_dsearch.sci
blob: 9261ddb6e8967529862199c096c26b3f72aa7684 (plain)
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
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) - 2013 - Samuel GOUGEON
//
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution.  The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt

function [i_bin, counts, outside] = %c_dsearch(T, bins, discrete)

    // CHECKING PARAMETERS
    // -------------------
    bins2 = unique(bins)
    if size(bins2,"*")~=size(bins,"*")
        msg = _("%c: input argument #%d has duplicates\n")
        error(msprintf(msg, "dsearch", 2))
    end

    // DISCRETE CASE
    // -------------
    if argn(2)>2 & discrete=="d" then
        [nb, i_bin] = members(T, bins)
        outside = sum(bool2s(nb==0))
        counts = zeros(bins)
        inds = [ 0 ; gsort(i_bin(:),"g","i") ; size(bins,"*")+1]
        tmp = find(inds(2:$)~=inds(1:$-1))+1
        counts(inds(tmp(1:$-1))) = tmp(2:$)-tmp(1:$-1)
        counts($) = sum(T==bins($))

        // SORTED CASE
        // -----------
    else
        if or(bins2~=bins)
            msg = _("%c: input argument #%d must be sorted in lexicographic order\n")
            error(msprintf(msg, "dsearch", 2))
        end
        T0 = T
        [T, k] = gsort(T0,"g","i")

        // index of the first T(i) >= bins(1)
        i0 = min(find(strcmp(T(:), bins(1))>=0))
        if i0==[] then
            i0 = 1
        end
        // Initializations
        i_bin = zeros(T)
        counts = zeros(bins(2:$))

        // Loop over the bins
        nT = size(T,"*")
        for i = 1:size(bins,"*")-1
            j = find(strcmp(T(i0:$), bins(i+1))<=0)
            if j==[] then
                counts(i) = 0
            else
                counts(i) = max(j)
                i_bin(i0+j-1) = i
                i0 = i0 + max(j)
            end
            if i0>nT then
                break
            end
        end

        // Taking into account the original T order
        i_bin = i_bin(:)
        i_bin(k(:)) = i_bin
        i_bin = matrix(i_bin, size(k))
        //
        outside = sum(bool2s(i_bin==0))
    end
endfunction