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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//! A typed list of key-value pairs.

mod macros;
pub mod marker;

use crate::{
    counter::{Counter, Current, Next},
    functional::{ApplyFunctor, Functor},
    list::{
        LAppend, LAppendFunctor, LConcatOp, LConcatOpOutput, LCons, LIndexOfManyOp,
        LIndexOfManyOpOutput, LIndexOfOp, LIndexOfOpOutput, LLengthOp, LLengthOpOutput, LNil,
        LPrepend, LReverse, LReverseFunctor, LSetEqualOp, LSetEqualOpOutput, LUnzipOp,
        LUnzipOpFormerOutput, TList,
    },
    tuple::{SecondOf, SecondOfFunctor},
};
use std::marker::PhantomData;
use typenum::Unsigned;

// list

/// The trait represents a list of key-value pairs.
pub trait KVList
where
    Self: TList,
{
}

/// A node of [KVList].
pub type KVCons<Key, Value, Tail> = LCons<(Key, Value), Tail>;

impl<Key, Value, Tail> KVList for KVCons<Key, Value, Tail> where Tail: KVList {}

/// The ending node of [KVList].
pub type KVNil = LNil;

impl KVList for LNil {}

// length of list

/// A functor that gets length of [KVList].
pub struct KVLengthFunctor;

impl<List> Functor<List> for KVLengthFunctor
where
    List: KVList + LLengthOp,
    LLengthOpOutput<List>: Unsigned,
{
    type Output = LLengthOpOutput<List>;
}

pub type KVLength<List> = ApplyFunctor<KVLengthFunctor, List>;

// prepend

/// A functor that prepends a key-value pair to [KVList].
pub struct KVPrependFunctor<Key, Value> {
    _phantom: PhantomData<(Key, Value)>,
}

impl<List, Key, Value> Functor<List> for KVPrependFunctor<Key, Value>
where
    List: KVList,
    LAppendFunctor<(Key, Value)>: Functor<List>,
{
    type Output = LPrepend<List, (Key, Value)>;
}

pub type KVPrepend<List, Key, Value> = ApplyFunctor<KVPrependFunctor<Key, Value>, List>;

// append

/// A functor that appends a key-value pair to [KVList].
pub struct KVAppendFunctor<Key, Value> {
    _phantom: PhantomData<(Key, Value)>,
}

impl<List, Key, Value> Functor<List> for KVAppendFunctor<Key, Value>
where
    List: KVList,
    LAppendFunctor<(Key, Value)>: Functor<List>,
{
    type Output = LAppend<List, (Key, Value)>;
}

pub type KVAppend<List, Key, Value> = ApplyFunctor<KVAppendFunctor<Key, Value>, List>;

// insert at

/// A type operator that inserts `Key`-`Value` pair into [KVList] at `Target`.
pub trait KVInsertAtOp<Key, Value, Target, Index>
where
    Index: Counter,
    Self: KVList,
    Self::Output: KVList,
{
    type Output;
}

pub type KVInsertAtOpOutput<List, Key, Value, Target, Index> =
    <List as KVInsertAtOp<Key, Value, Target, Index>>::Output;

impl<Key, Value, Target, TargetValue, Tail> KVInsertAtOp<Key, Value, Target, Current>
    for KVCons<Target, TargetValue, Tail>
where
    Tail: KVList,
{
    type Output = KVCons<Key, Value, KVCons<Target, TargetValue, Tail>>;
}
impl<Key, Value, Target, Index, NonTarget, NonTargetValue, Tail>
    KVInsertAtOp<Key, Value, Target, Next<Index>> for KVCons<NonTarget, NonTargetValue, Tail>
where
    Tail: KVList + KVInsertAtOp<Key, Value, Target, Index>,
    Index: Counter,
{
    type Output =
        KVCons<NonTarget, NonTargetValue, KVInsertAtOpOutput<Tail, Key, Value, Target, Index>>;
}

/// A functor that inserts `Key`-`Value` pair into [KVList] at `Target`.
pub struct KVInsertAtFunctor<Key, Value, Target, Index> {
    _phantom: PhantomData<(Key, Value, Target, Index)>,
}

pub type KVInsertAt<List, Key, Value, Target, Index> =
    ApplyFunctor<KVInsertAtFunctor<Key, Value, Target, Index>, List>;

impl<List, Key, Value, Target, Index> Functor<List> for KVInsertAtFunctor<Key, Value, Target, Index>
where
    List: KVList + KVInsertAtOp<Key, Value, Target, Index>,
    Index: Counter,
{
    type Output = KVInsertAtOpOutput<List, Key, Value, Target, Index>;
}

// remove

/// A type operator that removes `Target` from [KVList].
pub trait KVRemoveAtOp<Target, Index>
where
    Index: Counter,
    Self: KVList,
    Self::Output: KVList,
{
    type Output;
}

impl<Target, Value, Tail> KVRemoveAtOp<Target, Current> for KVCons<Target, Value, Tail>
where
    Tail: KVList,
{
    type Output = Tail;
}

impl<Target, Index, NonTarget, Value, Tail> KVRemoveAtOp<Target, Next<Index>>
    for KVCons<NonTarget, Value, Tail>
where
    Index: Counter,
    Tail: KVList + KVRemoveAtOp<Target, Index>,
{
    type Output = KVCons<NonTarget, Value, KVRemoveAtOpOutput<Tail, Target, Index>>;
}

pub type KVRemoveAtOpOutput<KVist, Target, Index> = <KVist as KVRemoveAtOp<Target, Index>>::Output;

/// A functor that removes `Target` from `KVList`.
pub struct KVRemoveAtFunctor<Target, Index> {
    _phantom: PhantomData<(Target, Index)>,
}

pub type KVRemoveAt<List, Target, Index> = ApplyFunctor<KVRemoveAtFunctor<Target, Index>, List>;

impl<List, Target, Index> Functor<List> for KVRemoveAtFunctor<Target, Index>
where
    List: KVList + KVRemoveAtOp<Target, Index>,
    Index: Counter,
{
    type Output = KVRemoveAtOpOutput<List, Target, Index>;
}

// remove multiple items

/// A type operator that removes multiple `Targets` from [KVList].
pub trait KVRemoveManyOp<Targets, Indexes>
where
    Targets: TList,
    Indexes: TList,
    Self: KVList,
    Self::Output: KVList,
{
    type Output;
}

impl<List> KVRemoveManyOp<LNil, LNil> for List
where
    List: KVList,
{
    type Output = List;
}

impl<Index, IRemain, Target, TRemain, Key, Value, Tail>
    KVRemoveManyOp<LCons<Target, TRemain>, LCons<Index, IRemain>> for KVCons<Key, Value, Tail>
where
    Index: Counter,
    IRemain: TList,
    TRemain: TList,
    Tail: KVList,
    Self: KVRemoveAtOp<Target, Index>,
    KVRemoveAtOpOutput<Self, Target, Index>: KVRemoveManyOp<TRemain, IRemain>,
{
    type Output = KVRemoveManyOpOutput<KVRemoveAtOpOutput<Self, Target, Index>, TRemain, IRemain>;
}

pub type KVRemoveManyOpOutput<KVist, Targets, Indexes> =
    <KVist as KVRemoveManyOp<Targets, Indexes>>::Output;

/// A functor that removes multiple `Targets` from [KVList].
pub struct KVRemoveManyFunctor<Targets, Indexes>
where
    Targets: TList,
    Indexes: TList,
{
    _phantom: PhantomData<(Targets, Indexes)>,
}

pub type KVRemoveMany<List, Targets, Indexes> =
    ApplyFunctor<KVRemoveManyFunctor<Targets, Indexes>, List>;

impl<List, Targets, Indexes> Functor<List> for KVRemoveManyFunctor<Targets, Indexes>
where
    List: KVList + KVRemoveManyOp<Targets, Indexes>,
    Targets: TList,
    Indexes: TList,
{
    type Output = KVRemoveManyOpOutput<List, Targets, Indexes>;
}

// index of item

/// A functor that gets index of `Target` in [KVList].
pub struct KVIndexOfFunctor<Target, Index>
where
    Index: Counter,
{
    _phantom: PhantomData<(Target, Index)>,
}

pub type KVIndexOf<List, Target, Index> = ApplyFunctor<KVIndexOfFunctor<Target, Index>, List>;

impl<List, Target, Index> Functor<List> for KVIndexOfFunctor<Target, Index>
where
    List: KVList + LUnzipOp,
    Index: Counter,
    LUnzipOpFormerOutput<List>: LIndexOfOp<Target, Index>,
    LIndexOfOpOutput<LUnzipOpFormerOutput<List>, Target, Index>: Unsigned,
{
    type Output = LIndexOfOpOutput<LUnzipOpFormerOutput<List>, Target, Index>;
}

// index of many

/// A functor that gets multiple indexes of `Targets` in [KVList].
pub struct KVIndexOfManyFunctor<Targets, Indexes> {
    _phantom: PhantomData<(Targets, Indexes)>,
}

impl<List, Targets, Indexes> Functor<List> for KVIndexOfManyFunctor<Targets, Indexes>
where
    List: KVList + LUnzipOp,
    Targets: TList,
    Indexes: TList,
    LUnzipOpFormerOutput<List>: LIndexOfManyOp<Targets, Indexes>,
    LIndexOfManyOpOutput<LUnzipOpFormerOutput<List>, Targets, Indexes>: TList,
{
    type Output = LIndexOfManyOpOutput<LUnzipOpFormerOutput<List>, Targets, Indexes>;
}

pub type KVIndexOfMany<List, Targets, Indexes> =
    ApplyFunctor<KVIndexOfManyFunctor<Targets, Indexes>, List>;

// reverse

/// A [Functor] that reverses a [KVList].
pub struct KVReverseFuntor {}

pub type KVReverse<List> = ApplyFunctor<KVReverseFuntor, List>;

impl<List> Functor<List> for KVReverseFuntor
where
    List: KVList,
    LReverse<List>: TList,
    LReverseFunctor: Functor<List>,
{
    type Output = LReverse<List>;
}

// set equal

/// A functor that compares if two [KVList]s have same set of keys.
pub struct KVSetEqualFuntor<Rhs, Indexes>
where
    Rhs: KVList,
    Indexes: TList,
{
    _phantom: PhantomData<(Rhs, Indexes)>,
}

pub type KVSetEqual<Lhs, Rhs, Indexes> = ApplyFunctor<KVSetEqualFuntor<Rhs, Indexes>, Lhs>;

impl<Lhs, Rhs, Indexes> Functor<Lhs> for KVSetEqualFuntor<Rhs, Indexes>
where
    Lhs: KVList + LUnzipOp,
    Rhs: KVList + LUnzipOp,
    Indexes: TList,
    LUnzipOpFormerOutput<Lhs>: LSetEqualOp<LUnzipOpFormerOutput<Rhs>, Indexes>,
{
    type Output = LSetEqualOpOutput<LUnzipOpFormerOutput<Lhs>, LUnzipOpFormerOutput<Rhs>, Indexes>;
}

// concatenate

/// A [Functor] that concatenates input and `Rhs` [KVList]s.
pub struct KVConcatFunctor<Rhs>
where
    Rhs: KVList,
{
    _phantom: PhantomData<Rhs>,
}

pub type KVConcat<Lhs, Rhs> = ApplyFunctor<KVConcatFunctor<Rhs>, Lhs>;

impl<Lhs, Rhs> Functor<Lhs> for KVConcatFunctor<Rhs>
where
    Lhs: KVList + LConcatOp<Rhs>,
    Rhs: KVList,
    LConcatOpOutput<Lhs, Rhs>: KVList,
{
    type Output = LConcatOpOutput<Lhs, Rhs>;
}

// get key-value pair

/// A functor that gets key-value pair from [KVList].
pub struct KVKeyValueAtFunctor<Target, Index> {
    _phantom: PhantomData<(Target, Index)>,
}

pub type KVKeyValueAt<List, Target, Index> = ApplyFunctor<KVKeyValueAtFunctor<Target, Index>, List>;

impl<Target, Value, Tail> Functor<KVCons<Target, Value, Tail>>
    for KVKeyValueAtFunctor<Target, Current>
where
    Tail: KVList,
{
    type Output = (Target, Value);
}

impl<NonTarget, Value, Tail, Target, Index> Functor<KVCons<NonTarget, Value, Tail>>
    for KVKeyValueAtFunctor<Target, Next<Index>>
where
    Tail: KVList,
    Index: Counter,
    KVKeyValueAtFunctor<Target, Index>: Functor<Tail>,
{
    type Output = KVKeyValueAt<Tail, Target, Index>;
}

// get value of key

/// A functor that gets the value at `Target` in [KVList].
pub struct KVValueAtFunctor<Target, Index>
where
    Index: Counter,
{
    _phantom: PhantomData<(Target, Index)>,
}

pub type KVValueAt<List, Target, Index> = ApplyFunctor<KVValueAtFunctor<Target, Index>, List>;

impl<List, Target, Index> Functor<List> for KVValueAtFunctor<Target, Index>
where
    List: KVList,
    Index: Counter,
    KVKeyValueAtFunctor<Target, Index>: Functor<List>,
    SecondOfFunctor: Functor<KVKeyValueAt<List, Target, Index>>,
{
    type Output = SecondOf<KVKeyValueAt<List, Target, Index>>;
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{control::IfSameOutput, KVListType, TListType};
    use typenum::consts::*;

    type AssertEqual<Lhs, Rhs> = IfSameOutput<(), Lhs, Rhs>;

    struct A;
    struct B;
    struct C;
    struct D;
    struct E;

    struct Va;
    struct Vb;
    struct Vc;
    struct Vd;
    struct Ve;

    type EmptyList = KVListType![];
    type SomeList = KVListType![(A, Va), (B, Vb), (C, Vc)];
    type AnotherList = KVListType![(D, Vd), (E, Ve)];

    // prepend empty list
    type Assert1 = AssertEqual<KVPrepend<EmptyList, A, Va>, KVListType![(A, Va)]>;

    // append empty list
    type Assert2 = AssertEqual<KVAppend<EmptyList, D, Vd>, KVListType![(D, Vd)]>;

    // prepend non-empty list
    type Assert3 =
        AssertEqual<KVPrepend<SomeList, D, Vd>, KVListType![(D, Vd), (A, Va), (B, Vb), (C, Vc)]>;

    // append non-empty list
    type Assert4 =
        AssertEqual<KVAppend<SomeList, D, Vd>, KVListType![(A, Va), (B, Vb), (C, Vc), (D, Vd)]>;

    // insert in middle
    type Assert5<Idx> = AssertEqual<
        KVInsertAt<SomeList, D, Vd, B, Idx>,
        KVListType![(A, Va), (D, Vd), (B, Vb), (C, Vc)],
    >;

    // insert at end
    type Assert6<Idx> = AssertEqual<
        KVInsertAt<SomeList, D, Vd, C, Idx>,
        KVListType![(A, Va), (B, Vb), (D, Vd), (C, Vc)],
    >;

    // remove
    type Assert7<Idx> = AssertEqual<KVRemoveAt<SomeList, B, Idx>, KVListType![(A, Va), (C, Vc)]>;

    // remove multiple items
    type Assert8<Idx> =
        AssertEqual<KVRemoveMany<SomeList, TListType![A, C], Idx>, KVListType![(B, Vb)]>;

    // remove until empty
    type Assert9<Idx> =
        AssertEqual<KVRemoveMany<SomeList, TListType![B, A, C], Idx>, KVListType![]>;

    // reverse list
    type Assert10 = AssertEqual<KVReverse<SomeList>, KVListType![(C, Vc), (B, Vb), (A, Va)]>;

    // assert identical set of items
    type Assert11<Idx> = KVSetEqual<SomeList, KVListType![(C, Vc), (A, Va), (B, Vb)], Idx>;

    // concat
    type Assert12 = AssertEqual<
        KVConcat<SomeList, AnotherList>,
        KVListType![(A, Va), (B, Vb), (C, Vc), (D, Vd), (E, Ve)],
    >;

    // concat
    type Assert13<Idx> =
        AssertEqual<KVIndexOfMany<SomeList, TListType![C, A, B], Idx>, TListType![U2, U0, U1]>;

    // index of
    type Assert14<Idx> = AssertEqual<KVIndexOf<SomeList, A, Idx>, U0>;
    type Assert15<Idx> = AssertEqual<KVIndexOf<SomeList, B, Idx>, U1>;
    type Assert16<Idx> = AssertEqual<KVIndexOf<SomeList, C, Idx>, U2>;

    // get key-value pair
    type Assert17<Idx> = AssertEqual<KVKeyValueAt<SomeList, B, Idx>, (B, Vb)>;

    // get value pair
    type Assert18<Idx> = AssertEqual<KVValueAt<SomeList, B, Idx>, Vb>;

    #[test]
    fn tlist_test() {
        let _: Assert1 = ();
        let _: Assert2 = ();
        let _: Assert3 = ();
        let _: Assert4 = ();
        let _: Assert5<_> = ();
        let _: Assert6<_> = ();
        let _: Assert7<_> = ();
        let _: Assert8<_> = ();
        let _: Assert9<_> = ();
        let _: Assert10 = ();
        let _: Assert11<_> = ();
        let _: Assert12 = ();
        let _: Assert13<_> = ();
        let _: Assert14<_> = ();
        let _: Assert15<_> = ();
        let _: Assert16<_> = ();
        let _: Assert17<_> = ();
        let _: Assert18<_> = ();
    }
}