Blame view

morfeusz/ResultsIteratorImpl.cpp 1.48 KB
Michał Lenart authored
1
2
3
4
5
6
7
8
9
/* 
 * File:   ResultsIteratorImpl.cpp
 * Author: mlenart
 * 
 * Created on 26 czerwiec 2014, 14:37
 */

#include "ResultsIteratorImpl.hpp"
Michał Lenart authored
10
#include <cstring>
Michał Lenart authored
11
#include <stdexcept>
Michał Lenart authored
12
Michał Lenart authored
13
14
namespace morfeusz {
Michał Lenart authored
15
    ResultsIteratorImpl::ResultsIteratorImpl(const MorfeuszImpl& morfeusz, const char* text, const char* textEnd, bool isOwnerOfText)
Michał Lenart authored
16
17
18
19
20
21
22
    :
    morfeusz(morfeusz),
    text(text),
    isOwnerOfText(isOwnerOfText),
    reader(text, textEnd, morfeusz.analyzerEnv),
    buffer(),
    bufferIterator(buffer.begin()) {
Michał Lenart authored
23
    }
Michał Lenart authored
24
Michał Lenart authored
25
    ResultsIteratorImpl::~ResultsIteratorImpl() {
Michał Lenart authored
26
27
28
        if (isOwnerOfText) {
            delete text;
        }
Michał Lenart authored
29
30
    }
Michał Lenart authored
31
32
    bool ResultsIteratorImpl::hasNext() {
        return bufferIterator != buffer.end() || tryToReadIntoBuffer();
Michał Lenart authored
33
34
    }
Michał Lenart authored
35
36
37
    const MorphInterpretation& ResultsIteratorImpl::peek() {
        ensureHasNext();
        return *bufferIterator;
Michał Lenart authored
38
39
40
    }

    MorphInterpretation ResultsIteratorImpl::next() {
Michał Lenart authored
41
42
43
44
45
46
        ensureHasNext();
        return *bufferIterator++;
    }

    void ResultsIteratorImpl::ensureHasNext() {
        if (!hasNext()) {
Michał Lenart authored
47
            throw std::out_of_range("No more interpretations available to ResultsIterator");
Michał Lenart authored
48
49
50
51
52
53
54
        }
    }

    bool ResultsIteratorImpl::tryToReadIntoBuffer() {
        assert(bufferIterator == buffer.end());
        buffer.resize(0);
        if (!reader.isAtEnd()) {
Michał Lenart authored
55
            morfeusz.analyseOneWord(reader, buffer);
Michał Lenart authored
56
57
58
        }
        bufferIterator = buffer.begin();
        return bufferIterator != buffer.end();
Michał Lenart authored
59
60
61
    }
}