summaryrefslogtreecommitdiff
path: root/src/T2DLL/T2CustomerTableIterator.cpp
blob: 0d952cc239930d387b2ad879c3674a178463bec3 (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
#include "StdAfx.h"
#include "T2CustomerTableIterator.h"
#include "T2TenantMemberDef.h"
#include "T2TenantMemberTableDef.h"
#include "UT2Utils.h"

T2CustomerTableIterator::T2CustomerTableIterator(const T2TenantMemberTableDef* inTable) {
    mTable = inTable;

    if (mTable) {
        mLastNum = mTable->GetNumOfElem();
        mBound = UT2Utils::Randomize(mLastNum);
        mNextIndex = -1;
    } else {
        mNextIndex = -2;
    }
}

T2CustomerTableIterator::~T2CustomerTableIterator() {
}

BOOL T2CustomerTableIterator::Next(T2TenantMemberDef*& outMemberDef, unsigned int& outRate) {
    BOOL result = false;
    BOOL done = false;

    while (!done) {
        CalcNextIndex();
        if (mNextIndex != -2) {
            T2TenantMemberDef *theMember = mTable->GetElem(mNextIndex);
            if (theMember) {
                unsigned int rate = UT2Utils::Float2Int(theMember->GetRate());
                if (rate > 0) {
                    outMemberDef = theMember;
                    outRate = rate;
                    done = true;
                    result = true;
                }
            } else {
                done = true;
            }
        } else {
            done = true;
        }
    }

    return result;
}

void T2CustomerTableIterator::CalcNextIndex() {
    if (mNextIndex == -1) {
        mNextIndex = mBound;
    } else if (mNextIndex != -2) {
        mNextIndex++;
        if (mNextIndex >= mLastNum)
            mNextIndex = 0;
        if (mNextIndex == mBound)
            mNextIndex = -2;
    }
}