summaryrefslogtreecommitdiff
path: root/src/coord.hh
blob: 6109bf14c3ec9359c2c601a612450dbde9c3a5c3 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#ifndef coord_hh_INCLUDED
#define coord_hh_INCLUDED

#include "units.hh"
#include "hash.hh"

namespace Kakoune
{

template<typename EffectiveType, typename LineType, typename ColumnType>
struct LineAndColumn
{
    LineType   line = 0;
    ColumnType column = 0;

    [[gnu::always_inline]]
    constexpr EffectiveType operator+(EffectiveType other) const
    {
        return {line + other.line, column + other.column};
    }

    [[gnu::always_inline]]
    EffectiveType& operator+=(EffectiveType other)
    {
        line   += other.line;
        column += other.column;
        return *static_cast<EffectiveType*>(this);
    }

    [[gnu::always_inline]]
    constexpr EffectiveType operator-(EffectiveType other) const
    {
        return {line - other.line, column - other.column};
    }

    [[gnu::always_inline]]
    EffectiveType& operator-=(EffectiveType other)
    {
        line   -= other.line;
        column -= other.column;
        return *static_cast<EffectiveType*>(this);
    }

    [[gnu::always_inline]]
    constexpr bool operator< (EffectiveType other) const
    {
        return (line != other.line) ? line < other.line
                                    : column < other.column;
    }

    [[gnu::always_inline]]
    constexpr bool operator<= (EffectiveType other) const
    {
        return (line != other.line) ? line < other.line
                                    : column <= other.column;
    }

    [[gnu::always_inline]]
    constexpr bool operator> (EffectiveType other) const
    {
        return (line != other.line) ? line > other.line
                                    : column > other.column;
    }

    [[gnu::always_inline]]
    constexpr bool operator>= (EffectiveType other) const
    {
        return (line != other.line) ? line > other.line
                                    : column >= other.column;
    }

    [[gnu::always_inline]]
    constexpr bool operator== (EffectiveType other) const
    {
        return line == other.line and column == other.column;
    }

    [[gnu::always_inline]]
    constexpr bool operator!= (EffectiveType other) const
    {
        return line != other.line or column != other.column;
    }

    friend constexpr size_t hash_value(const EffectiveType& val)
    {
        return hash_values(val.line, val.column);
    }
};

struct BufferCoord : LineAndColumn<BufferCoord, LineCount, ByteCount>
{
    [[gnu::always_inline]]
    constexpr BufferCoord(LineCount line = 0, ByteCount column = 0)
        : LineAndColumn{line, column} {}
};

struct DisplayCoord : LineAndColumn<DisplayCoord, LineCount, ColumnCount>
{
    [[gnu::always_inline]]
    constexpr DisplayCoord(LineCount line = 0, ColumnCount column = 0)
        : LineAndColumn{line, column} {}

    static constexpr const char* option_type_name = "coord";
};

struct BufferCoordAndTarget : BufferCoord
{
    [[gnu::always_inline]]
    constexpr BufferCoordAndTarget(LineCount line = 0, ByteCount column = 0, ColumnCount target = -1)
        : BufferCoord{line, column}, target{target} {}

    [[gnu::always_inline]]
    constexpr BufferCoordAndTarget(BufferCoord coord, ColumnCount target = -1)
        : BufferCoord{coord}, target{target} {}

    ColumnCount target;
};

constexpr size_t hash_value(const BufferCoordAndTarget& val)
{
    return hash_values(val.line, val.column, val.target);
}

}

#endif // coord_hh_INCLUDED