summaryrefslogtreecommitdiff
path: root/src/bundles.adb
blob: a6e1472cdf9db0d645cea842db599175775baef6 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99


--  This source is licensed under Creative Commons CC0 v1.0.
--
--  To read the full text, see license.txt in the main directory of this repository
--  or go to https://creativecommons.org/publicdomain/zero/1.0/legalcode.txt
--
--  For a human readable summary, go to https://creativecommons.org/publicdomain/zero/1.0/


package body Bundles is


    procedure Transfer
           (This     : in out Bundle;
            From, To : in     Candidates.CandidateID;
            Excluded : in     Candidates.Containers.CandidateID_Set;
            Value    : in     Rationals.Fraction;
            Result   :    out Bundle)
    is
        use type Candidates.CandidateID;

        Position : Positive;
    begin
        Result := Empty_Bundle;
        Result.Worth := This.Worth * Value;
        for P of This.Papers loop
            Position := Given_Prefs.Preference_Range'First;

            while Position <= Given_Prefs.Preference_Range'Last and then
                  P (Position) /= From
            loop
                Position := Position + 1;
            end loop;
            Position := Position + 1;

            while Position <= Given_Prefs.Preference_Range'Last and then
                  P (Position) /= Candidates.No_Candidate and then
                  Excluded.Contains (P (Position))
            loop
                Position := Position + 1;
            end loop;

            if Position <= Given_Prefs.Preference_Range'Last and then
               P (Position) = To
            then
                Result.Papers.Append (P);
            end if;
        end loop;
    end Transfer;




    function Count_Votes
           (This : in Bundle)
        return Natural is
    begin
        --  The number of votes under all normal circumstances should never exceed MAXINT
        return Natural (Rationals.Floor (Count_Papers (This) * This.Worth));
    end Count_Votes;




    function Count_Papers
           (This : in Bundle)
        return Natural is
    begin
        return Integer (This.Papers.Length);
    end Count_Papers;




    procedure Count_Both
           (This   : in     Bundle;
            Votes  :    out Natural;
            Papers :    out Natural) is
    begin
        Papers := Count_Papers (This);
        --  The number of votes under all normal circumstances should never exceed MAXINT
        Votes := Natural (Rationals.Floor (Papers * This.Worth));
    end Count_Both;




    function "<"
           (Left, Right : in Bundle)
        return Boolean is
    begin
        return Left.Worth < Right.Worth;
    end "<";


end Bundles;