aboutsummaryrefslogtreecommitdiffstats
path: root/src/lesson40_algo84.cpp
blob: ccd8d080f133cadd3ceca58d50ed9d93bb7f0bf3 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//============================================================================
// Author      : Sven Göthel
// Version     : 0.1
// Copyright   : MIT
// Description : C++ Lesson 4.0 A custom interval map using C++
//============================================================================
#include <iostream>
// #include <iterator>
#include <string>
#include <map>

#include <concepts>
#include <functional>

#include <cassert>

namespace test_env {
    /**
     * Test Key value type implementing constraints
     *
     * Properties:
     * - copyable
     * - assignable
     * - comparable via <
     *
     * NOT properties (not implement):
     * - comparable via ==, <=, >, >=
     * - arithmetic operators
     */
    class KeyType {
        private:
            int64_t store;

        public:
            constexpr KeyType() noexcept
            : store(0) { }

            constexpr KeyType(int64_t v) noexcept
            : store(v) { }

            constexpr KeyType(const KeyType& o) noexcept = default;
            constexpr KeyType(KeyType&& o) noexcept = default;
            constexpr KeyType& operator=(const KeyType&) noexcept = default;
            constexpr KeyType& operator=(KeyType&&) noexcept = default;

            constexpr int64_t value() const noexcept { return store; }

            bool operator<(const KeyType& b) const noexcept {
                return store < b.store;
            }

            std::string toString() const noexcept { return std::to_string(store); }
    };

    /**
     * Test Value value type implementing constraints
     *
     * Properties:
     * - copyable
     * - assignable
     * - comparable via ==
     *
     * NOT properties (not implement):
     * - comparable via <, <=, >, >=
     * - arithmetic operators
     */
    class ValueType {
        private:
            int64_t store;

        public:
            constexpr ValueType() noexcept
            : store(0) { }

            constexpr ValueType(int64_t v) noexcept
            : store(v) { }

            constexpr ValueType(const ValueType& o) noexcept = default;
            constexpr ValueType(ValueType&& o) noexcept = default;
            constexpr ValueType& operator=(const ValueType&) noexcept = default;
            constexpr ValueType& operator=(ValueType&&) noexcept = default;

            constexpr int64_t value() const noexcept { return store; }

            constexpr bool operator==(const ValueType& b) const noexcept {
                return store == b.store;
            }

            std::string toString() const noexcept { return std::to_string(store); }
    };
} // namespace test_env

namespace std {
    inline std::string to_string(const test_env::KeyType& v) noexcept { return std::to_string(v.value()); }

    inline std::ostream& operator<<(std::ostream& out, const test_env::KeyType& v) {
        return out << v.toString();
    }

    inline std::string to_string(const test_env::ValueType& v) noexcept { return std::to_string(v.value()); }

    inline std::ostream& operator<<(std::ostream& out, const test_env::ValueType& v) {
        return out << v.toString();
    }
}

void test_interval_map();

namespace feature {

    template<typename T>
    concept IntervalMapKey = requires(T a)
    {
        // { a < a } -> std::convertible_to<bool>;
        { a < a } -> std::same_as<bool>;
    };

    /**
     * Custom interval map
     *
     * - Using a unique (canonical) K -> V m_map, i.e. mapping to a set of values w/o duplicates
     *   - std map, only using std::less<K>, i.e. operator< for key comparison, see below
     * - Using a dedicated interval begin value
     *   - Mapped to all keys not covered by map
     *   - Not contained in first entry of m_map,
     *   - Initially covers whole range of K
     *
     * Each interval [keyBegin, keyEnd) includes keyBegin, but excludes keyEnd.
     *
     * Example:
     *   m_valBegin = 'A', m_map{ (1,'B'), (3,'A') }
     * where value 'B' is mapped to range [1..3)
     *
     * TODO:
     * - Add corner case of no_value, IFF value is contained in map
     * - Inject constraints of K, V via concepts in class template declaration
     *
     * @tparam K only provides operator<
     * @tparam V only provides operator==
     */
    template<typename K, typename V>
    // requires std::equality_comparable_with<V, V>
    class IntervalMap {
      private:
        typedef std::map<K, V, std::less<K> /* default */> map_t;
        typedef typename map_t::iterator map_iterator_t;
        typedef typename map_t::const_iterator const_map_iterator_t;

        map_t m_map;

        V m_valBegin;

        friend void ::test_interval_map();

      public:
        constexpr IntervalMap(const V& valBegin) noexcept
        : m_map(), m_valBegin(valBegin) {}

        constexpr const V& operator[](const K& key) const noexcept {
            const_map_iterator_t it = m_map.upper_bound(key);
            if ( it == m_map.cend() || it == m_map.cbegin() ) {
                return m_valBegin; // includes empty case: it == cend()
            } else {
                return (--it)->second;
            }
        }

        /**
         * Adds an interval to this map
         *
         * Pre-existing intervals are overwritten or split.
         *
         * Invalid given interval keyBegin >= keyEnd is a nop.
         *
         * Complexity O(log(n)) or O(m) with m = period-len (keyEnd-keyBegin)
         *
         * @param keyBegin interval inclusive start
         * @param keyEnd interval exclusive end
         * @param val mapped value to given interval
         * @return true if interval has been successfully added, otherwise false
         */
        bool add( const K& keyBegin, const K& keyEnd, const V& val ) noexcept {
            if( !( keyBegin < keyEnd ) ) {
                return false;
            }
            map_iterator_t it_b = m_map.lower_bound(keyBegin); // O(log(n))
            if( it_b == m_map.end() ) {
                // no key >= b exists, hence [b, e) not included (includes empty + tail)
                m_map.insert(m_map.end(), { keyBegin, val }); // O(1), inserting just before iterator
                m_map.insert(m_map.end(), { keyEnd, m_valBegin }); // O(1), inserting just before iterator
                return true;
            }
            // it_b >= b (keyBegin)

            V end_val = m_valBegin;
            map_iterator_t it;

            if( keyBegin < it_b->first ) {
                // it_b > b (keyBegin)
                it = it_b;
                m_map.insert_or_assign(it, keyBegin, V(val)); // O(1), inserting just before it
            } else {
                // it_b == b (keyBegin)
                end_val = it_b->second;
                it = it_b;
                ++it;
                if( !( it_b->second == val ) ) {
                    end_val = it_b->second;
                    m_map.insert_or_assign(it, keyBegin, V(val)); // O(1), overwrite just before it
                } else {
                    // it_b->second == val
                    // nop, reuse existing start-point
                }
            }

            while( it != m_map.end() && it->first < keyEnd ) { // O(m) w/ m = period-len (keyEnd-keyBegin)
                end_val = it->second;
                it = m_map.erase(it); // O(1)
            }
            if( it != m_map.end() ) {
                // it >= e (keyEnd)
                if( keyEnd < it->first ) {
                    // it > e (keyEnd)
                    m_map.insert_or_assign(it, keyEnd, end_val); // O(1), inserting just before it
                } else {
                    // it == e (keyEnd)
                    // nop, reuse existing end-point
                }
            } else {
                m_map.insert_or_assign(it, keyEnd, end_val); // O(1), inserting just before it
            }
            return true;
        }

        std::string toString() const noexcept {
            std::string s = "size ";
            s.append(std::to_string(m_map.size())).append(": ");
            for (auto const &pair: m_map) {
                s.append( std::to_string( pair.first )).append(" -> ")
                  .append( std::to_string( pair.second ) )
                  .append(", ");
            }
            return s;
        }
    };

} // namespace feature

namespace std {

    template<typename K, typename V>
    std::string to_string(const feature::IntervalMap<K, V>& v) noexcept {
        return std::to_string(v.value());
    }

    template<typename K, typename V>
    std::ostream& operator<<(std::ostream& out, const feature::IntervalMap<K, V>& v) noexcept {
        return out << v.toString();
    }
}

//
// test code
//

typedef feature::IntervalMap<test_env::KeyType, test_env::ValueType> test_interval_map_t;

void dumpMap(const std::string& prefix, const test_interval_map_t& m, int keyBegin, int keyEnd) {
    std::cout << prefix << ": " << m << std::endl;
    std::cout << "  : ";
    for(int k=keyBegin; k<keyEnd; ++k) {
        std::cout << "[" << k << "] " << m[k] << ", ";
    }
    std::cout << std::endl << std::endl;
}

void rangeTest(const test_interval_map_t& m, int keyBegin, int keyEnd, const test_env::ValueType& v, int line) {
    for(int k=keyBegin; k<keyEnd; ++k) {
        test_env::ValueType v_has = m[k];
        if( !(v == v_has) ) {
            std::cerr << "In line " << line << ": failed test: " << v << " == ( m[" << k << "]: " << v_has << " )" << std::endl;
            exit(1);
        }
    }
}
void test_interval_map() {
    // make assert() macro happy, not req if using Catch2
    const test_env::ValueType v_42(42), v_43(43), v_44(44), v_45(45), v_46(46),
                            v_80(80), v_81(81), v_82(82), v_88(88);

    test_interval_map_t im( v_42 ); // empty
    dumpMap("Empty", im, 0, 0);
    assert( v_42 == im.m_valBegin );
    assert( im.m_map.cend() == im.m_map.cbegin() );
    rangeTest(im, 0, 20, v_42, __LINE__);

    // Assign first
    // map entries: [5, 7)=44
    assert( im.add(5, 7, v_44) );
    dumpMap("add-1", im, 0, 22);
    assert( v_42 == im.m_valBegin );
    assert( im.m_map.cend() != im.m_map.cbegin() );
    assert( 2 == im.m_map.size() );
    rangeTest(im, 0,  5, v_42, __LINE__);
    rangeTest(im, 5,  7, v_44, __LINE__);
    rangeTest(im, 7, 22, v_42, __LINE__);

    // Assign end
    // map entries: : [5, 7)=44, [17, 19)=46
    assert( im.add(17, 19, v_46) );
    dumpMap("add-2", im, 0, 22);
    assert( v_42 == im.m_valBegin );
    assert( im.m_map.cend() != im.m_map.cbegin() );
    assert( 4 == im.m_map.size() );
    rangeTest(im, 0,  5, v_42, __LINE__);
    rangeTest(im, 5,  7, v_44, __LINE__);
    rangeTest(im, 7, 17, v_42, __LINE__);
    rangeTest(im,17, 19, v_46, __LINE__);
    rangeTest(im,19, 22, v_42, __LINE__);

    // Assign front
    // map entries: : [1, 3)=43, [5, 7)=44, [17, 19)=46
    assert( im.add(1, 3, v_43) );
    dumpMap("add-3", im, 0, 22);
    assert( v_42 == im.m_valBegin );
    assert( im.m_map.cend() != im.m_map.cbegin() );
    assert( 6 == im.m_map.size() );
    rangeTest(im, 0,  1, v_42, __LINE__);
    rangeTest(im, 1,  3, v_43, __LINE__);
    rangeTest(im, 3,  5, v_42, __LINE__);
    rangeTest(im, 5,  7, v_44, __LINE__);
    rangeTest(im, 7, 17, v_42, __LINE__);
    rangeTest(im,17, 19, v_46, __LINE__);
    rangeTest(im,19, 22, v_42, __LINE__);

    // Assign middle
    // map entries: : [1, 3)=43, [5, 7)=44, [8, 10)=45, [17, 19)=46
    assert( im.add(8, 10, v_45) );
    dumpMap("add-4", im, 0, 22);
    assert( v_42 == im.m_valBegin );
    assert( im.m_map.cend() != im.m_map.cbegin() );
    assert( 8 == im.m_map.size() );
    rangeTest(im, 0,  1, v_42, __LINE__);
    rangeTest(im, 1,  3, v_43, __LINE__);
    rangeTest(im, 3,  5, v_42, __LINE__);
    rangeTest(im, 5,  7, v_44, __LINE__);
    rangeTest(im, 7,  8, v_42, __LINE__);
    rangeTest(im, 8, 10, v_45, __LINE__);
    rangeTest(im,10, 17, v_42, __LINE__);
    rangeTest(im,17, 19, v_46, __LINE__);
    rangeTest(im,19, 22, v_42, __LINE__);

    // Assign: keyEnd dropped for next-interval keyBegin
    // map entries: : [1, 3)=43, [5, 7)=44, [8, 10)=45, [15, 17)=80, [17, 19)=46
    assert( im.add(15, 17, v_80) );
    dumpMap("add-5", im, 0, 22);
    assert( v_42 == im.m_valBegin );
    assert( im.m_map.cend() != im.m_map.cbegin() );
    assert( 9 == im.m_map.size() );
    rangeTest(im, 0,  1, v_42, __LINE__);
    rangeTest(im, 1,  3, v_43, __LINE__);
    rangeTest(im, 3,  5, v_42, __LINE__);
    rangeTest(im, 5,  7, v_44, __LINE__);
    rangeTest(im, 7,  8, v_42, __LINE__);
    rangeTest(im, 8, 10, v_45, __LINE__);
    rangeTest(im,10, 15, v_42, __LINE__);
    rangeTest(im,15, 17, v_80, __LINE__);
    rangeTest(im,17, 19, v_46, __LINE__);
    rangeTest(im,19, 22, v_42, __LINE__);

    // Assign: replace 1 interval
    // map entries: : [1, 3)=43, [5, 7)=81, [8, 10)=45, [15, 17)=80, [17, 19)=46
    assert( im.add(5, 7, v_81) );
    dumpMap("add-6", im, 0, 22);
    assert( v_42 == im.m_valBegin );
    assert( im.m_map.cend() != im.m_map.cbegin() );
    assert( 9 == im.m_map.size() );
    rangeTest(im, 0,  1, v_42, __LINE__);
    rangeTest(im, 1,  3, v_43, __LINE__);
    rangeTest(im, 3,  5, v_42, __LINE__);
    rangeTest(im, 5,  7, v_81, __LINE__);
    rangeTest(im, 7,  8, v_42, __LINE__);
    rangeTest(im, 8, 10, v_45, __LINE__);
    rangeTest(im,10, 15, v_42, __LINE__);
    rangeTest(im,15, 17, v_80, __LINE__);
    rangeTest(im,17, 19, v_46, __LINE__);
    rangeTest(im,19, 22, v_42, __LINE__);

    // Assign: complete overwrite of 2 intervals
    // map entries: : [1, 3)=43, [5, 7)=81, [8, 10)=45, [12, 20)=82
    assert( im.add(12, 20, v_82) );
    dumpMap("add-7", im, 0, 22);
    assert( v_42 == im.m_valBegin );
    assert( im.m_map.cend() != im.m_map.cbegin() );
    assert( 8 == im.m_map.size() );
    rangeTest(im, 0,  1, v_42, __LINE__);
    rangeTest(im, 1,  3, v_43, __LINE__);
    rangeTest(im, 3,  5, v_42, __LINE__);
    rangeTest(im, 5,  7, v_81, __LINE__);
    rangeTest(im, 7,  8, v_42, __LINE__);
    rangeTest(im, 8, 10, v_45, __LINE__);
    rangeTest(im,10, 12, v_42, __LINE__);
    rangeTest(im,12, 20, v_82, __LINE__);
    rangeTest(im,20, 22, v_42, __LINE__);

    // Assign: Splitting pre-existing tail interval
    // map entries: : [1, 3)=43, [5, 7)=81, [8, 10)=45, [11, 17)=88, [17, 20)=82
    assert( im.add(11, 17, v_88) );
    dumpMap("add-8", im, 0, 11);
    assert( v_42 == im.m_valBegin );
    assert( im.m_map.cend() != im.m_map.cbegin() );
    assert( 9 == im.m_map.size() );
    rangeTest(im, 0,  1, v_42, __LINE__);
    rangeTest(im, 1,  3, v_43, __LINE__);
    rangeTest(im, 3,  5, v_42, __LINE__);
    rangeTest(im, 5,  7, v_81, __LINE__);
    rangeTest(im, 7,  8, v_42, __LINE__);
    rangeTest(im, 8, 10, v_45, __LINE__);
    rangeTest(im,10, 11, v_42, __LINE__);
    rangeTest(im,11, 17, v_88, __LINE__);
    rangeTest(im,17, 20, v_82, __LINE__);
    rangeTest(im,20, 22, v_42, __LINE__);

    // Assign: complete overwrite of 2 intervals
    // map entries: : [1, 3)=43, [4, 10)=81, [11, 17)=88, [17, 20)=82
    assert( im.add(4, 10, v_81) );
    dumpMap("add-9", im, 0, 22);
    assert( v_42 == im.m_valBegin );
    assert( im.m_map.cend() != im.m_map.cbegin() );
    assert( 7 == im.m_map.size() );
    rangeTest(im, 0,  1, v_42, __LINE__);
    rangeTest(im, 1,  3, v_43, __LINE__);
    rangeTest(im, 3,  4, v_42, __LINE__);
    rangeTest(im, 4, 10, v_81, __LINE__);
    rangeTest(im,10, 11, v_42, __LINE__);
    rangeTest(im,11, 17, v_88, __LINE__);
    rangeTest(im,17, 20, v_82, __LINE__);
    rangeTest(im,20, 22, v_42, __LINE__);

    // Assign: Splitting pre-existing head and tail interval
    // map entries: : [1, 3)=43, [4, 7)=81, [7, 14)=45, [14, 17)=88, [17, 20)=82
    assert( im.add(7, 14, v_45) );
    dumpMap("add-10", im, 0, 22);
    assert( v_42 == im.m_valBegin );
    assert( im.m_map.cend() != im.m_map.cbegin() );
    assert( 7 == im.m_map.size() );
    rangeTest(im, 0,  1, v_42, __LINE__);
    rangeTest(im, 1,  3, v_43, __LINE__);
    rangeTest(im, 3,  4, v_42, __LINE__);
    rangeTest(im, 4,  7, v_81, __LINE__);
    rangeTest(im, 7, 14, v_45, __LINE__);
    rangeTest(im,14, 17, v_88, __LINE__);
    rangeTest(im,17, 20, v_82, __LINE__);
    rangeTest(im,20, 22, v_42, __LINE__);
}

int main() {
    test_interval_map();
    return 0;
}