solverforge-macros 0.8.10

Derive macros for SolverForge constraint solver
Documentation
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
use proc_macro2::TokenStream;
use quote::{format_ident, quote};

use crate::attr_parse::has_attribute;

use super::type_helpers::extract_collection_inner_type;

pub(super) fn generate_list_operations(
    fields: &syn::punctuated::Punctuated<syn::Field, syn::token::Comma>,
) -> TokenStream {
    let entity_collections: Vec<_> = fields
        .iter()
        .filter(|field| has_attribute(&field.attrs, "planning_entity_collection"))
        .enumerate()
        .filter_map(|(descriptor_index, field)| {
            let field_ident = field.ident.as_ref()?;
            let entity_type = extract_collection_inner_type(&field.ty)?;
            Some((descriptor_index, field_ident, entity_type))
        })
        .collect();

    if entity_collections.is_empty() {
        return TokenStream::new();
    }

    let source_len_arms: Vec<_> = fields
        .iter()
        .filter(|field| {
            has_attribute(&field.attrs, "problem_fact_collection")
                || has_attribute(&field.attrs, "planning_entity_collection")
                || has_attribute(&field.attrs, "planning_list_element_collection")
        })
        .filter_map(|field| {
            let field_ident = field.ident.as_ref()?;
            let field_name = field_ident.to_string();
            Some(quote! { ::core::option::Option::Some(#field_name) => s.#field_ident.len(), })
        })
        .collect();

    let source_element_arms: Vec<_> = fields
        .iter()
        .filter(|field| {
            has_attribute(&field.attrs, "problem_fact_collection")
                || has_attribute(&field.attrs, "planning_entity_collection")
                || has_attribute(&field.attrs, "planning_list_element_collection")
        })
        .filter_map(|field| {
            let field_ident = field.ident.as_ref()?;
            let field_name = field_ident.to_string();
            let value_expr = if has_attribute(&field.attrs, "planning_list_element_collection") {
                quote! { s.#field_ident[idx] }
            } else {
                quote! { idx }
            };
            Some(quote! { ::core::option::Option::Some(#field_name) => { #value_expr } })
        })
        .collect();

    let owner_helpers: Vec<_> = entity_collections
        .iter()
        .map(|(_, field_ident, entity_type)| {
            let field_name = field_ident.to_string();
            let list_trait =
                quote! { <#entity_type as ::solverforge::__internal::ListVariableEntity<Self>> };
            let list_len_ident = format_ident!("__solverforge_list_len_{}", field_name);
            let list_remove_ident = format_ident!("__solverforge_list_remove_{}", field_name);
            let list_insert_ident = format_ident!("__solverforge_list_insert_{}", field_name);
            let list_get_ident = format_ident!("__solverforge_list_get_{}", field_name);
            let list_set_ident = format_ident!("__solverforge_list_set_{}", field_name);
            let list_reverse_ident = format_ident!("__solverforge_list_reverse_{}", field_name);
            let sublist_remove_ident =
                format_ident!("__solverforge_sublist_remove_{}", field_name);
            let sublist_insert_ident =
                format_ident!("__solverforge_sublist_insert_{}", field_name);
            let ruin_remove_ident = format_ident!("__solverforge_ruin_remove_{}", field_name);
            let ruin_insert_ident = format_ident!("__solverforge_ruin_insert_{}", field_name);
            let list_remove_for_construction_ident =
                format_ident!("__solverforge_list_remove_for_construction_{}", field_name);
            let index_to_element_ident =
                format_ident!("__solverforge_index_to_element_{}", field_name);
            let element_count_ident = format_ident!("__solverforge_element_count_{}", field_name);
            let assigned_elements_ident =
                format_ident!("__solverforge_assigned_elements_{}", field_name);
            let n_entities_ident = format_ident!("__solverforge_n_entities_{}", field_name);
            let assign_element_ident =
                format_ident!("__solverforge_assign_element_{}", field_name);

            quote! {
                #[inline]
                fn #list_len_ident(s: &Self, entity_idx: usize) -> usize {
                    s.#field_ident
                        .get(entity_idx)
                        .map_or(0, |entity| #list_trait::list_field(entity).len())
                }

                #[inline]
                fn #list_remove_ident(
                    s: &mut Self,
                    entity_idx: usize,
                    pos: usize,
                ) -> ::core::option::Option<usize> {
                    s.#field_ident
                        .get_mut(entity_idx)
                        .map(|entity| #list_trait::list_field_mut(entity).remove(pos))
                }

                #[inline]
                fn #list_insert_ident(s: &mut Self, entity_idx: usize, pos: usize, val: usize) {
                    if let Some(entity) = s.#field_ident.get_mut(entity_idx) {
                        #list_trait::list_field_mut(entity).insert(pos, val);
                    }
                }

                #[inline]
                fn #list_get_ident(
                    s: &Self,
                    entity_idx: usize,
                    pos: usize,
                ) -> ::core::option::Option<usize> {
                    s.#field_ident
                        .get(entity_idx)
                        .and_then(|entity| #list_trait::list_field(entity).get(pos).copied())
                }

                #[inline]
                fn #list_set_ident(s: &mut Self, entity_idx: usize, pos: usize, val: usize) {
                    if let Some(entity) = s.#field_ident.get_mut(entity_idx) {
                        let list = #list_trait::list_field_mut(entity);
                        if pos < list.len() {
                            list[pos] = val;
                        }
                    }
                }

                #[inline]
                fn #list_reverse_ident(
                    s: &mut Self,
                    entity_idx: usize,
                    start: usize,
                    end: usize,
                ) {
                    if let Some(entity) = s.#field_ident.get_mut(entity_idx) {
                        #list_trait::list_field_mut(entity)[start..end].reverse();
                    }
                }

                #[inline]
                fn #sublist_remove_ident(
                    s: &mut Self,
                    entity_idx: usize,
                    start: usize,
                    end: usize,
                ) -> Vec<usize> {
                    s.#field_ident
                        .get_mut(entity_idx)
                        .map(|entity| #list_trait::list_field_mut(entity).drain(start..end).collect())
                        .unwrap_or_default()
                }

                #[inline]
                fn #sublist_insert_ident(
                    s: &mut Self,
                    entity_idx: usize,
                    pos: usize,
                    items: Vec<usize>,
                ) {
                    if let Some(entity) = s.#field_ident.get_mut(entity_idx) {
                        let list = #list_trait::list_field_mut(entity);
                        for (offset, item) in items.into_iter().enumerate() {
                            list.insert(pos + offset, item);
                        }
                    }
                }

                #[inline]
                fn #ruin_remove_ident(s: &mut Self, entity_idx: usize, pos: usize) -> usize {
                    #list_trait::list_field_mut(&mut s.#field_ident[entity_idx]).remove(pos)
                }

                #[inline]
                fn #ruin_insert_ident(s: &mut Self, entity_idx: usize, pos: usize, val: usize) {
                    #list_trait::list_field_mut(&mut s.#field_ident[entity_idx]).insert(pos, val);
                }

                #[inline]
                fn #list_remove_for_construction_ident(
                    s: &mut Self,
                    entity_idx: usize,
                    pos: usize,
                ) -> usize {
                    #list_trait::list_field_mut(&mut s.#field_ident[entity_idx]).remove(pos)
                }

                #[inline]
                fn #index_to_element_ident(s: &Self, idx: usize) -> usize {
                    let element_count = Self::#element_count_ident(s);
                    if idx >= element_count {
                        panic!(
                            "list element index {} is out of bounds for {} elements",
                            idx,
                            element_count
                        );
                    }

                    match #list_trait::LIST_ELEMENT_SOURCE {
                        #(#source_element_arms)*
                        ::core::option::Option::Some(source) => {
                            panic!(
                                "list source field `{}` was not found on the planning solution",
                                source
                            );
                        }
                        ::core::option::Option::None => idx,
                    }
                }

                #[inline]
                fn #element_count_ident(s: &Self) -> usize {
                    match #list_trait::LIST_ELEMENT_SOURCE {
                        #(#source_len_arms)*
                        ::core::option::Option::Some(source) => {
                            panic!(
                                "list source field `{}` was not found on the planning solution",
                                source
                            );
                        }
                        ::core::option::Option::None => 0,
                    }
                }

                #[inline]
                fn #assigned_elements_ident(s: &Self) -> Vec<usize> {
                    s.#field_ident
                        .iter()
                        .flat_map(|entity| #list_trait::list_field(entity).iter().copied())
                        .collect()
                }

                #[inline]
                fn #n_entities_ident(s: &Self) -> usize {
                    s.#field_ident.len()
                }

                #[inline]
                fn #assign_element_ident(s: &mut Self, entity_idx: usize, elem: usize) {
                    if let Some(entity) = s.#field_ident.get_mut(entity_idx) {
                        #list_trait::list_field_mut(entity).push(elem);
                    }
                }
            }
        })
        .collect();

    let owner_public_methods: Vec<_> = entity_collections
        .iter()
        .map(|(descriptor_index, field_ident, entity_type)| {
            let field_name = field_ident.to_string();
            let list_trait =
                quote! { <#entity_type as ::solverforge::__internal::ListVariableEntity<Self>> };
            let owner_guard = quote! {
                if !#list_trait::HAS_LIST_VARIABLE {
                    panic!(
                        "`{}` is not a planning list owner on this solution",
                        stringify!(#field_ident)
                    );
                }
            };
            let list_len_ident = format_ident!("__solverforge_list_len_{}", field_name);
            let list_remove_ident = format_ident!("__solverforge_list_remove_{}", field_name);
            let list_insert_ident = format_ident!("__solverforge_list_insert_{}", field_name);
            let list_get_ident = format_ident!("__solverforge_list_get_{}", field_name);
            let list_set_ident = format_ident!("__solverforge_list_set_{}", field_name);
            let list_reverse_ident = format_ident!("__solverforge_list_reverse_{}", field_name);
            let sublist_remove_ident = format_ident!("__solverforge_sublist_remove_{}", field_name);
            let sublist_insert_ident = format_ident!("__solverforge_sublist_insert_{}", field_name);
            let ruin_remove_ident = format_ident!("__solverforge_ruin_remove_{}", field_name);
            let ruin_insert_ident = format_ident!("__solverforge_ruin_insert_{}", field_name);
            let list_remove_for_construction_ident =
                format_ident!("__solverforge_list_remove_for_construction_{}", field_name);
            let index_to_element_ident =
                format_ident!("__solverforge_index_to_element_{}", field_name);
            let element_count_ident = format_ident!("__solverforge_element_count_{}", field_name);
            let assigned_elements_ident =
                format_ident!("__solverforge_assigned_elements_{}", field_name);
            let n_entities_ident = format_ident!("__solverforge_n_entities_{}", field_name);
            let assign_element_ident = format_ident!("__solverforge_assign_element_{}", field_name);

            let owner_list_len_method = format_ident!("{}_list_len", field_name);
            let owner_list_len_static_method = format_ident!("{}_list_len_static", field_name);
            let owner_list_remove_method = format_ident!("{}_list_remove", field_name);
            let owner_list_insert_method = format_ident!("{}_list_insert", field_name);
            let owner_list_get_method = format_ident!("{}_list_get", field_name);
            let owner_list_set_method = format_ident!("{}_list_set", field_name);
            let owner_list_reverse_method = format_ident!("{}_list_reverse", field_name);
            let owner_sublist_remove_method = format_ident!("{}_sublist_remove", field_name);
            let owner_sublist_insert_method = format_ident!("{}_sublist_insert", field_name);
            let owner_ruin_remove_method = format_ident!("{}_ruin_remove", field_name);
            let owner_ruin_insert_method = format_ident!("{}_ruin_insert", field_name);
            let owner_list_remove_for_construction_method =
                format_ident!("{}_list_remove_for_construction", field_name);
            let owner_index_to_element_method =
                format_ident!("{}_index_to_element_static", field_name);
            let owner_descriptor_index_method =
                format_ident!("{}_list_variable_descriptor_index", field_name);
            let owner_element_count_method = format_ident!("{}_element_count", field_name);
            let owner_assigned_elements_method = format_ident!("{}_assigned_elements", field_name);
            let owner_n_entities_method = format_ident!("{}_n_entities", field_name);
            let owner_assign_element_method = format_ident!("{}_assign_element", field_name);

            let descriptor_index_lit = syn::LitInt::new(
                &descriptor_index.to_string(),
                proc_macro2::Span::call_site(),
            );

            quote! {
                #[inline]
                pub fn #owner_list_len_method(&self, entity_idx: usize) -> usize {
                    #owner_guard
                    Self::#list_len_ident(self, entity_idx)
                }

                #[inline]
                pub fn #owner_list_len_static_method(s: &Self, entity_idx: usize) -> usize {
                    #owner_guard
                    Self::#list_len_ident(s, entity_idx)
                }

                #[inline]
                pub fn #owner_list_remove_method(
                    s: &mut Self,
                    entity_idx: usize,
                    pos: usize,
                ) -> ::core::option::Option<usize> {
                    #owner_guard
                    Self::#list_remove_ident(s, entity_idx, pos)
                }

                #[inline]
                pub fn #owner_list_insert_method(
                    s: &mut Self,
                    entity_idx: usize,
                    pos: usize,
                    val: usize,
                ) {
                    #owner_guard
                    Self::#list_insert_ident(s, entity_idx, pos, val)
                }

                #[inline]
                pub fn #owner_list_get_method(
                    s: &Self,
                    entity_idx: usize,
                    pos: usize,
                ) -> ::core::option::Option<usize> {
                    #owner_guard
                    Self::#list_get_ident(s, entity_idx, pos)
                }

                #[inline]
                pub fn #owner_list_set_method(
                    s: &mut Self,
                    entity_idx: usize,
                    pos: usize,
                    val: usize,
                ) {
                    #owner_guard
                    Self::#list_set_ident(s, entity_idx, pos, val)
                }

                #[inline]
                pub fn #owner_list_reverse_method(
                    s: &mut Self,
                    entity_idx: usize,
                    start: usize,
                    end: usize,
                ) {
                    #owner_guard
                    Self::#list_reverse_ident(s, entity_idx, start, end)
                }

                #[inline]
                pub fn #owner_sublist_remove_method(
                    s: &mut Self,
                    entity_idx: usize,
                    start: usize,
                    end: usize,
                ) -> Vec<usize> {
                    #owner_guard
                    Self::#sublist_remove_ident(s, entity_idx, start, end)
                }

                #[inline]
                pub fn #owner_sublist_insert_method(
                    s: &mut Self,
                    entity_idx: usize,
                    pos: usize,
                    items: Vec<usize>,
                ) {
                    #owner_guard
                    Self::#sublist_insert_ident(s, entity_idx, pos, items)
                }

                #[inline]
                pub fn #owner_ruin_remove_method(
                    s: &mut Self,
                    entity_idx: usize,
                    pos: usize,
                ) -> usize {
                    #owner_guard
                    Self::#ruin_remove_ident(s, entity_idx, pos)
                }

                #[inline]
                pub fn #owner_ruin_insert_method(
                    s: &mut Self,
                    entity_idx: usize,
                    pos: usize,
                    val: usize,
                ) {
                    #owner_guard
                    Self::#ruin_insert_ident(s, entity_idx, pos, val)
                }

                #[inline]
                pub fn #owner_list_remove_for_construction_method(
                    s: &mut Self,
                    entity_idx: usize,
                    pos: usize,
                ) -> usize {
                    #owner_guard
                    Self::#list_remove_for_construction_ident(s, entity_idx, pos)
                }

                #[inline]
                pub fn #owner_index_to_element_method(s: &Self, idx: usize) -> usize {
                    #owner_guard
                    Self::#index_to_element_ident(s, idx)
                }

                #[inline]
                pub fn #owner_descriptor_index_method() -> usize {
                    #owner_guard
                    #descriptor_index_lit
                }

                #[inline]
                pub fn #owner_element_count_method(s: &Self) -> usize {
                    #owner_guard
                    Self::#element_count_ident(s)
                }

                #[inline]
                pub fn #owner_assigned_elements_method(s: &Self) -> Vec<usize> {
                    #owner_guard
                    Self::#assigned_elements_ident(s)
                }

                #[inline]
                pub fn #owner_n_entities_method(s: &Self) -> usize {
                    #owner_guard
                    Self::#n_entities_ident(s)
                }

                #[inline]
                pub fn #owner_assign_element_method(
                    s: &mut Self,
                    entity_idx: usize,
                    elem: usize,
                ) {
                    #owner_guard
                    Self::#assign_element_ident(s, entity_idx, elem)
                }
            }
        })
        .collect();

    let list_owner_count_terms: Vec<_> = entity_collections
        .iter()
        .map(|(_, _, entity_type)| quote! { #entity_type::__SOLVERFORGE_LIST_VARIABLE_COUNT })
        .collect();

    let single_owner_list_len_branches: Vec<_> = entity_collections
        .iter()
        .map(|(_, field_ident, entity_type)| {
            let field_name = field_ident.to_string();
            let list_trait =
                quote! { <#entity_type as ::solverforge::__internal::ListVariableEntity<Self>> };
            let list_len_ident = format_ident!("__solverforge_list_len_{}", field_name);
            quote! {
                if #list_trait::HAS_LIST_VARIABLE {
                    return Self::#list_len_ident(s, entity_idx);
                }
            }
        })
        .collect();

    let single_owner_list_remove_branches: Vec<_> = entity_collections
        .iter()
        .map(|(_, field_ident, entity_type)| {
            let field_name = field_ident.to_string();
            let list_trait =
                quote! { <#entity_type as ::solverforge::__internal::ListVariableEntity<Self>> };
            let list_remove_ident = format_ident!("__solverforge_list_remove_{}", field_name);
            quote! {
                if #list_trait::HAS_LIST_VARIABLE {
                    return Self::#list_remove_ident(s, entity_idx, pos);
                }
            }
        })
        .collect();

    let single_owner_list_insert_branches: Vec<_> = entity_collections
        .iter()
        .map(|(_, field_ident, entity_type)| {
            let field_name = field_ident.to_string();
            let list_trait =
                quote! { <#entity_type as ::solverforge::__internal::ListVariableEntity<Self>> };
            let list_insert_ident = format_ident!("__solverforge_list_insert_{}", field_name);
            quote! {
                if #list_trait::HAS_LIST_VARIABLE {
                    Self::#list_insert_ident(s, entity_idx, pos, val);
                    return;
                }
            }
        })
        .collect();

    let single_owner_list_get_branches: Vec<_> = entity_collections
        .iter()
        .map(|(_, field_ident, entity_type)| {
            let field_name = field_ident.to_string();
            let list_trait =
                quote! { <#entity_type as ::solverforge::__internal::ListVariableEntity<Self>> };
            let list_get_ident = format_ident!("__solverforge_list_get_{}", field_name);
            quote! {
                if #list_trait::HAS_LIST_VARIABLE {
                    return Self::#list_get_ident(s, entity_idx, pos);
                }
            }
        })
        .collect();

    let single_owner_list_set_branches: Vec<_> = entity_collections
        .iter()
        .map(|(_, field_ident, entity_type)| {
            let field_name = field_ident.to_string();
            let list_trait =
                quote! { <#entity_type as ::solverforge::__internal::ListVariableEntity<Self>> };
            let list_set_ident = format_ident!("__solverforge_list_set_{}", field_name);
            quote! {
                if #list_trait::HAS_LIST_VARIABLE {
                    Self::#list_set_ident(s, entity_idx, pos, val);
                    return;
                }
            }
        })
        .collect();

    let single_owner_list_reverse_branches: Vec<_> = entity_collections
        .iter()
        .map(|(_, field_ident, entity_type)| {
            let field_name = field_ident.to_string();
            let list_trait =
                quote! { <#entity_type as ::solverforge::__internal::ListVariableEntity<Self>> };
            let list_reverse_ident = format_ident!("__solverforge_list_reverse_{}", field_name);
            quote! {
                if #list_trait::HAS_LIST_VARIABLE {
                    Self::#list_reverse_ident(s, entity_idx, start, end);
                    return;
                }
            }
        })
        .collect();

    let single_owner_sublist_remove_branches: Vec<_> = entity_collections
        .iter()
        .map(|(_, field_ident, entity_type)| {
            let field_name = field_ident.to_string();
            let list_trait =
                quote! { <#entity_type as ::solverforge::__internal::ListVariableEntity<Self>> };
            let sublist_remove_ident = format_ident!("__solverforge_sublist_remove_{}", field_name);
            quote! {
                if #list_trait::HAS_LIST_VARIABLE {
                    return Self::#sublist_remove_ident(s, entity_idx, start, end);
                }
            }
        })
        .collect();

    let single_owner_sublist_insert_branches: Vec<_> = entity_collections
        .iter()
        .map(|(_, field_ident, entity_type)| {
            let field_name = field_ident.to_string();
            let list_trait =
                quote! { <#entity_type as ::solverforge::__internal::ListVariableEntity<Self>> };
            let sublist_insert_ident = format_ident!("__solverforge_sublist_insert_{}", field_name);
            quote! {
                if #list_trait::HAS_LIST_VARIABLE {
                    Self::#sublist_insert_ident(s, entity_idx, pos, items);
                    return;
                }
            }
        })
        .collect();

    let single_owner_ruin_remove_branches: Vec<_> = entity_collections
        .iter()
        .map(|(_, field_ident, entity_type)| {
            let field_name = field_ident.to_string();
            let list_trait =
                quote! { <#entity_type as ::solverforge::__internal::ListVariableEntity<Self>> };
            let ruin_remove_ident = format_ident!("__solverforge_ruin_remove_{}", field_name);
            quote! {
                if #list_trait::HAS_LIST_VARIABLE {
                    return Self::#ruin_remove_ident(s, entity_idx, pos);
                }
            }
        })
        .collect();

    let single_owner_ruin_insert_branches: Vec<_> = entity_collections
        .iter()
        .map(|(_, field_ident, entity_type)| {
            let field_name = field_ident.to_string();
            let list_trait =
                quote! { <#entity_type as ::solverforge::__internal::ListVariableEntity<Self>> };
            let ruin_insert_ident = format_ident!("__solverforge_ruin_insert_{}", field_name);
            quote! {
                if #list_trait::HAS_LIST_VARIABLE {
                    Self::#ruin_insert_ident(s, entity_idx, pos, val);
                    return;
                }
            }
        })
        .collect();

    let single_owner_remove_for_construction_branches: Vec<_> = entity_collections
        .iter()
        .map(|(_, field_ident, entity_type)| {
            let field_name = field_ident.to_string();
            let list_trait =
                quote! { <#entity_type as ::solverforge::__internal::ListVariableEntity<Self>> };
            let list_remove_for_construction_ident =
                format_ident!("__solverforge_list_remove_for_construction_{}", field_name);
            quote! {
                if #list_trait::HAS_LIST_VARIABLE {
                    return Self::#list_remove_for_construction_ident(s, entity_idx, pos);
                }
            }
        })
        .collect();

    let single_owner_index_to_element_branches: Vec<_> = entity_collections
        .iter()
        .map(|(_, field_ident, entity_type)| {
            let field_name = field_ident.to_string();
            let list_trait =
                quote! { <#entity_type as ::solverforge::__internal::ListVariableEntity<Self>> };
            let index_to_element_ident =
                format_ident!("__solverforge_index_to_element_{}", field_name);
            quote! {
                if #list_trait::HAS_LIST_VARIABLE {
                    return Self::#index_to_element_ident(s, idx);
                }
            }
        })
        .collect();

    let single_owner_descriptor_index_branches: Vec<_> = entity_collections
        .iter()
        .map(|(descriptor_index, _, entity_type)| {
            let descriptor_index_lit = syn::LitInt::new(
                &descriptor_index.to_string(),
                proc_macro2::Span::call_site(),
            );
            let list_trait =
                quote! { <#entity_type as ::solverforge::__internal::ListVariableEntity<Self>> };
            quote! {
                if #list_trait::HAS_LIST_VARIABLE {
                    return #descriptor_index_lit;
                }
            }
        })
        .collect();

    let single_owner_element_count_branches: Vec<_> = entity_collections
        .iter()
        .map(|(_, field_ident, entity_type)| {
            let field_name = field_ident.to_string();
            let list_trait =
                quote! { <#entity_type as ::solverforge::__internal::ListVariableEntity<Self>> };
            let element_count_ident = format_ident!("__solverforge_element_count_{}", field_name);
            quote! {
                if #list_trait::HAS_LIST_VARIABLE {
                    return Self::#element_count_ident(s);
                }
            }
        })
        .collect();

    let single_owner_assigned_elements_branches: Vec<_> = entity_collections
        .iter()
        .map(|(_, field_ident, entity_type)| {
            let field_name = field_ident.to_string();
            let list_trait =
                quote! { <#entity_type as ::solverforge::__internal::ListVariableEntity<Self>> };
            let assigned_elements_ident =
                format_ident!("__solverforge_assigned_elements_{}", field_name);
            quote! {
                if #list_trait::HAS_LIST_VARIABLE {
                    return Self::#assigned_elements_ident(s);
                }
            }
        })
        .collect();

    let single_owner_n_entities_branches: Vec<_> = entity_collections
        .iter()
        .map(|(_, field_ident, entity_type)| {
            let field_name = field_ident.to_string();
            let list_trait =
                quote! { <#entity_type as ::solverforge::__internal::ListVariableEntity<Self>> };
            let n_entities_ident = format_ident!("__solverforge_n_entities_{}", field_name);
            quote! {
                if #list_trait::HAS_LIST_VARIABLE {
                    return Self::#n_entities_ident(s);
                }
            }
        })
        .collect();

    let single_owner_assign_element_branches: Vec<_> = entity_collections
        .iter()
        .map(|(_, field_ident, entity_type)| {
            let field_name = field_ident.to_string();
            let list_trait =
                quote! { <#entity_type as ::solverforge::__internal::ListVariableEntity<Self>> };
            let assign_element_ident = format_ident!("__solverforge_assign_element_{}", field_name);
            quote! {
                if #list_trait::HAS_LIST_VARIABLE {
                    Self::#assign_element_ident(s, entity_idx, elem);
                    return;
                }
            }
        })
        .collect();

    let total_list_entities_terms: Vec<_> = entity_collections
        .iter()
        .map(|(_, field_ident, entity_type)| {
            let field_name = field_ident.to_string();
            let list_trait =
                quote! { <#entity_type as ::solverforge::__internal::ListVariableEntity<Self>> };
            let n_entities_ident = format_ident!("__solverforge_n_entities_{}", field_name);
            quote! {
                if #list_trait::HAS_LIST_VARIABLE {
                    Self::#n_entities_ident(s)
                } else {
                    0
                }
            }
        })
        .collect();

    let total_list_elements_terms: Vec<_> = entity_collections
        .iter()
        .map(|(_, field_ident, entity_type)| {
            let field_name = field_ident.to_string();
            let list_trait =
                quote! { <#entity_type as ::solverforge::__internal::ListVariableEntity<Self>> };
            let element_count_ident = format_ident!("__solverforge_element_count_{}", field_name);
            quote! {
                if #list_trait::HAS_LIST_VARIABLE {
                    Self::#element_count_ident(s)
                } else {
                    0
                }
            }
        })
        .collect();

    quote! {
        #(#owner_helpers)*

        const __SOLVERFORGE_LIST_OWNER_COUNT: usize = 0 #(+ #list_owner_count_terms)*;

        #[inline]
        fn __solverforge_assert_single_list_owner() {
            assert!(
                Self::__SOLVERFORGE_LIST_OWNER_COUNT == 1,
                "single-owner list helper called on a solution with {} list owners",
                Self::__SOLVERFORGE_LIST_OWNER_COUNT,
            );
        }

        #(#owner_public_methods)*

        #[inline]
        pub fn list_len(&self, entity_idx: usize) -> usize {
            Self::list_len_static(self, entity_idx)
        }

        #[inline]
        pub fn list_len_static(s: &Self, entity_idx: usize) -> usize {
            Self::__solverforge_assert_single_list_owner();
            #(#single_owner_list_len_branches)*
            unreachable!("single-owner list helper called without a canonical list owner");
        }

        #[inline]
        pub fn list_remove(s: &mut Self, entity_idx: usize, pos: usize) -> ::core::option::Option<usize> {
            Self::__solverforge_assert_single_list_owner();
            #(#single_owner_list_remove_branches)*
            unreachable!("single-owner list helper called without a canonical list owner");
        }

        #[inline]
        pub fn list_insert(s: &mut Self, entity_idx: usize, pos: usize, val: usize) {
            Self::__solverforge_assert_single_list_owner();
            #(#single_owner_list_insert_branches)*
            unreachable!("single-owner list helper called without a canonical list owner");
        }

        #[inline]
        pub fn list_get(s: &Self, entity_idx: usize, pos: usize) -> ::core::option::Option<usize> {
            Self::__solverforge_assert_single_list_owner();
            #(#single_owner_list_get_branches)*
            unreachable!("single-owner list helper called without a canonical list owner");
        }

        #[inline]
        pub fn list_set(s: &mut Self, entity_idx: usize, pos: usize, val: usize) {
            Self::__solverforge_assert_single_list_owner();
            #(#single_owner_list_set_branches)*
            unreachable!("single-owner list helper called without a canonical list owner");
        }

        #[inline]
        pub fn list_reverse(s: &mut Self, entity_idx: usize, start: usize, end: usize) {
            Self::__solverforge_assert_single_list_owner();
            #(#single_owner_list_reverse_branches)*
            unreachable!("single-owner list helper called without a canonical list owner");
        }

        #[inline]
        pub fn sublist_remove(
            s: &mut Self,
            entity_idx: usize,
            start: usize,
            end: usize,
        ) -> Vec<usize> {
            Self::__solverforge_assert_single_list_owner();
            #(#single_owner_sublist_remove_branches)*
            unreachable!("single-owner list helper called without a canonical list owner");
        }

        #[inline]
        pub fn sublist_insert(
            s: &mut Self,
            entity_idx: usize,
            pos: usize,
            items: Vec<usize>,
        ) {
            Self::__solverforge_assert_single_list_owner();
            #(#single_owner_sublist_insert_branches)*
            unreachable!("single-owner list helper called without a canonical list owner");
        }

        #[inline]
        pub fn ruin_remove(s: &mut Self, entity_idx: usize, pos: usize) -> usize {
            Self::__solverforge_assert_single_list_owner();
            #(#single_owner_ruin_remove_branches)*
            unreachable!("single-owner list helper called without a canonical list owner");
        }

        #[inline]
        pub fn ruin_insert(s: &mut Self, entity_idx: usize, pos: usize, val: usize) {
            Self::__solverforge_assert_single_list_owner();
            #(#single_owner_ruin_insert_branches)*
            unreachable!("single-owner list helper called without a canonical list owner");
        }

        #[inline]
        pub fn list_remove_for_construction(s: &mut Self, entity_idx: usize, pos: usize) -> usize {
            Self::__solverforge_assert_single_list_owner();
            #(#single_owner_remove_for_construction_branches)*
            unreachable!("single-owner list helper called without a canonical list owner");
        }

        #[inline]
        pub fn index_to_element_static(s: &Self, idx: usize) -> usize {
            Self::__solverforge_assert_single_list_owner();
            #(#single_owner_index_to_element_branches)*
            unreachable!("single-owner list helper called without a canonical list owner");
        }

        #[inline]
        pub fn list_variable_descriptor_index() -> usize {
            Self::__solverforge_assert_single_list_owner();
            #(#single_owner_descriptor_index_branches)*
            unreachable!("single-owner list helper called without a canonical list owner");
        }

        #[inline]
        pub fn element_count(s: &Self) -> usize {
            Self::__solverforge_assert_single_list_owner();
            #(#single_owner_element_count_branches)*
            unreachable!("single-owner list helper called without a canonical list owner");
        }

        #[inline]
        pub fn assigned_elements(s: &Self) -> Vec<usize> {
            Self::__solverforge_assert_single_list_owner();
            #(#single_owner_assigned_elements_branches)*
            unreachable!("single-owner list helper called without a canonical list owner");
        }

        #[inline]
        pub fn n_entities(s: &Self) -> usize {
            Self::__solverforge_assert_single_list_owner();
            #(#single_owner_n_entities_branches)*
            unreachable!("single-owner list helper called without a canonical list owner");
        }

        #[inline]
        pub fn assign_element(s: &mut Self, entity_idx: usize, elem: usize) {
            Self::__solverforge_assert_single_list_owner();
            #(#single_owner_assign_element_branches)*
            unreachable!("single-owner list helper called without a canonical list owner");
        }

        #[inline]
        fn __solverforge_total_list_entities(s: &Self) -> usize {
            0 #(+ #total_list_entities_terms)*
        }

        #[inline]
        fn __solverforge_total_list_elements(s: &Self) -> usize {
            0 #(+ #total_list_elements_terms)*
        }
    }
}