freight_car_forwarder 0.1.11

Port of the C++ port of Timothy O'Connor's Freight Car Forwarding system.
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
// -!- rust -!- //////////////////////////////////////////////////////////////
//
//  System        : 
//  Module        : 
//  Object Name   : $RCSfile$
//  Revision      : $Revision$
//  Date          : $Date$
//  Author        : $Author$
//  Created By    : Robert Heller
//  Created       : 2025-09-02 15:13:27
//  Last Modified : <250919.2004>
//
//  Description	
//
//  Notes
//
//  History
//	
/////////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2025  Robert Heller D/B/A Deepwoods Software
//			51 Locke Hill Road
//			Wendell, MA 01379-9728
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program; if not, write to the Free Software
//    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
// 
//
//////////////////////////////////////////////////////////////////////////////
use std::fmt;
use std::ops::{Index, IndexMut};
use std::sync::Arc;
use std::collections::HashMap;
use crate::train::*;

/// Station or Industry Enum used as the drop stop for a Switch List Element
#[derive(Debug, PartialEq, Eq, Clone, Copy)] // Add traits for comparison, copy, print
pub enum StationOrIndustry {
    /// A station stop
    StationStop(u8),
    /// An industry stop  
    IndustryStop(usize),
    /// This should not ever be used 
    None,
}

impl fmt::Display for StationOrIndustry {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            StationOrIndustry::StationStop(station) =>
                    write!(f, "<#Station ({})>", station),
            StationOrIndustry::IndustryStop(industry) =>
                    write!(f, "<#Industry ({})>", industry),
            StationOrIndustry::None =>
                    write!(f, "<#None>"),
        }
    }
}

/// trait to create a StationOrIndustry enum based on the stop type
trait __NewSorI<T> {
    fn new(index: T) -> Self;
}

impl __NewSorI<u8> for StationOrIndustry {
    /// Create a station stop
    /// ## Parameters:
    /// - index the station index
    ///
    /// __Returns__ a StationOrIndustry::StationStop
    fn new(index: u8) -> Self {
        StationOrIndustry::StationStop(index)
    }
}

impl __NewSorI<usize> for StationOrIndustry {
    /// Create an industry stop
    /// ## Parameters:
    /// - index the industry index
    ///
    /// __Returns__ a StationOrIndustry::IndustryStop
    fn new(index: usize) -> Self {
        StationOrIndustry::IndustryStop(index)
    }
}

impl Default for StationOrIndustry {
    fn default() -> Self {
        StationOrIndustry::None
    }
}

/// A switch list element contains where to pick up the car, the car to pick 
/// up, the train to do the pick up, and the last train to pick up this car
/// and where to drop it off.
#[derive(Debug, Clone)]
pub struct SwitchListElement {
    pickLoc: usize,
    pickCar: usize,
    pickTrain: usize,
    lastTrain: usize,
    dropStop: StationOrIndustry,
}

impl fmt::Display for SwitchListElement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "<#SwitchListElement>")
    }
}

impl Default for SwitchListElement {
    fn default() -> Self {
        Self {pickLoc: 0, pickCar: 0, pickTrain: 0, lastTrain: 0, 
              dropStop: StationOrIndustry::None }
    }
}
/// A trait to create a SwitchListElement based on the type of index, u8 for
/// station drops, usize for industry drops. 
trait __NewSWElement<T> {
    fn new(pickLoc: usize, pickCar: usize, pickTrain: usize,         
                          lastTrain: usize, eletype: T) -> SwitchListElement;
}
impl __NewSWElement<u8> for SwitchListElement {
    /// Create a SwitchListElement with a station drop
    /// ## Parameters:
    /// - pickLoc the pickup location
    /// - pickCar the car to pick up
    /// - pickTrain the train to pick the car up
    /// - lastTrain the last train to pick up this car
    /// - station the station to drop the car at
    ///
    /// __Returns__ a freshly initialized SwitchListElement
    fn new(pickLoc: usize, pickCar: usize, pickTrain: usize, 
                      lastTrain: usize, station: u8) -> Self {
        Self {pickLoc: pickLoc, pickCar: pickCar, pickTrain: pickTrain,
              lastTrain: lastTrain, 
              dropStop: StationOrIndustry::StationStop(station) }
    }
}

impl __NewSWElement<usize> for SwitchListElement {
    /// Create a SwitchListElement with an industry drop
    /// ## Parameters:
    /// - pickLoc the pickup location
    /// - pickCar the car to pick up
    /// - pickTrain the train to pick the car up
    /// - lastTrain the last train to pick up this car
    /// - industry the industry to drop the car at
    ///
    /// __Returns__ a freshly initialized SwitchListElement
    fn new(pickLoc: usize, pickCar: usize, pickTrain: usize, 
                      lastTrain: usize, industry: usize) -> Self {
        Self {pickLoc: pickLoc, pickCar: pickCar, pickTrain: pickTrain,
              lastTrain: lastTrain, 
              dropStop: StationOrIndustry::IndustryStop(industry) }
    }
}

impl SwitchListElement {
    /// The pickup location
    /// ## Parameters:
    /// None
    ///
    /// __Returns__ the pickup location
    pub fn PickLocation(&self) -> usize {self.pickLoc}
    /// The pickup car
    /// ## Parameters:
    /// None
    ///
    /// __Returns__ the car to pick up.
    pub fn PickCar(&self) -> usize {self.pickCar}
    /// The pickup train
    /// ## Parameters:
    /// None
    ///
    /// __Returns__ the pickup train
    pub fn PickTrain(&self) -> usize {self.pickTrain}
    /// The last train
    /// ## Parameters:
    /// None
    ///
    /// __Returns__ the last train
    pub fn LastTrain(&self) -> usize {self.lastTrain}
    /// The industry to drop the car at, if any
    /// ## Parameters:
    /// None
    ///
    /// __Returns__ Some(industry) or None
    pub fn DropStopIndustry(&self) -> Option<usize> {
        if self.pickTrain == 0 {return None;}
        match self.dropStop {
            StationOrIndustry::StationStop(station) => None,
            StationOrIndustry::IndustryStop(industry) => Some(industry),
            StationOrIndustry::None => None,
        }
    }
    /// The station to drop the car at
    /// ## Parameters:
    /// None
    ///
    /// __Returns__ Some(station) or None
    pub fn DropStopStation(&self) -> Option<u8> {
        if self.pickTrain == 0 {return None;}
        match self.dropStop {
            StationOrIndustry::StationStop(station) => Some(station),
            StationOrIndustry::IndustryStop(industry) => None,
            StationOrIndustry::None => None, 
        }
    }
    /// Is this a drop stop for this train?
    /// ## Parameters:
    /// - px stop number
    /// - trains the a reference copy of the trains hashmap
    ///
    /// __Returns__ true if this is a train and a stop that matches, false 
    /// otherwise  
    pub fn DropStopEQ(&self, px:usize,trains: &Arc<HashMap<usize, Train>>)
             -> bool {
        if !trains.contains_key(&self.pickTrain) {return false;}
        let train = &trains[&self.pickTrain];
        let theStopOpt = train.Stop(px);
        if theStopOpt.is_none() {return false;}
        let theStop = theStopOpt.unwrap();
        match self.dropStop {
            StationOrIndustry::None => {return false;}
            StationOrIndustry::StationStop(dropStation) =>
                {return *theStop == Stop::StationStop(dropStation);},
            StationOrIndustry::IndustryStop(dropIndustry) =>
                {return *theStop == Stop::IndustryStop(dropIndustry);},
        };
        //false
    }
}

/// Switch list struct.  A specialized vector of switch list elements.
#[derive(Debug, Clone)]
pub struct SwitchList {
    theList: Vec<SwitchListElement>,
    pickIndex: usize,
    limitCars: usize,
    lastIndex: isize,
}

impl fmt::Display for SwitchList {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "<#SwitchList {} elements>", self.theList.len())
    }
}

impl Default for SwitchList {
    /// The default initialized SwitchList
    fn default() -> Self {
        Self {theList: Vec::new(), pickIndex: 0, lastIndex: -1, limitCars: 0}
    }
}

/// A trait to add switch list elements based on the type of stop
pub trait __StopType<T> {
    fn AddSwitchListElement(&mut self,pickloc: usize, pickcar: usize, 
                            picktrain: usize, lasttrain: usize, stop: T);
}

impl __StopType<u8> for SwitchList {
    /// Add a station stop switch list element
    /// ## Parameters:
    /// - pickloc pickup location
    /// - pickcar car to pick up
    /// - picktrain the train to pick up
    /// - lasttrain the last train to pick up this car
    /// - stop the station stop
    ///
    /// __Returns__ nothing
    fn AddSwitchListElement(&mut self,pickloc: usize, pickcar: usize, 
                                picktrain: usize, lasttrain: usize, stop: u8) {
        let newele: SwitchListElement = 
            SwitchListElement::new(pickloc, pickcar, picktrain, lasttrain,
                                   stop);
        if self.pickIndex >= self.theList.len() {
            self.theList.push(newele);
            self.pickIndex = self.theList.len();
        } else {
            self.theList[self.pickIndex] = newele;
            self.pickIndex += 1;
        }
        self.limitCars = self.pickIndex;
    }
}

impl __StopType<usize> for SwitchList {
    /// Add a station stop switch list element
    /// ## Parameters:
    /// - pickloc pickup location
    /// - pickcar car to pick up
    /// - picktrain the train to pick up
    /// - lasttrain the last train to pick up this car
    /// - stop the industry stop
    ///
    /// __Returns__ nothing
    fn AddSwitchListElement(&mut self,pickloc: usize, pickcar: usize,
                                picktrain: usize, lasttrain: usize, 
                                stop: usize) {
        let newele: SwitchListElement = 
            SwitchListElement::new(pickloc, pickcar, picktrain, lasttrain,
                                   stop);
        if self.pickIndex >= self.theList.len() {
            self.theList.push(newele);
            self.pickIndex = self.theList.len();
        } else {
            self.theList[self.pickIndex] = newele;
            self.pickIndex += 1;
        }
        self.limitCars = self.pickIndex;
    }
}

impl SwitchList {
    /// Initialize a SwitchList
    /// ## Parameters:
    /// None
    ///
    /// __Returns__ a freshly initialized SwitchList
    pub fn new() -> Self {
        Self {theList: Vec::new(), pickIndex: 0, lastIndex: -1, limitCars: 0}
    }
    /// Reset the switch list
    /// ## Parameters:
    /// None
    ///
    /// __Returns__ nothing
    pub fn ResetSwitchList(&mut self) {
        self.pickIndex = 0;
        self.lastIndex = -1;
    }
    /// Discard the switch list
    /// ## Parameters:
    /// None
    ///
    /// __Returns__ nothing
    pub fn DiscardSwitchList(&mut self) {
        self.ResetSwitchList();
        self.limitCars = 0;
    }
    /// Find the next switch list element for the car and industry
    /// ## Parameters:
    /// - car the car index
    /// - industry the industry index
    ///
    /// __Returns__ the the switxh list element index or -1
    pub fn NextSwitchListForCarAndIndustry(&mut self, car: usize, 
                                            industry: usize) -> isize {
        let start: usize = (self.lastIndex+1) as usize;
        let end: usize = self.pickIndex;
        for Gx in start..end {
            let igx: usize = Gx;
            if self.theList[igx].PickCar() == car &&
               self.theList[igx].PickLocation() == industry {
                self.lastIndex = Gx as isize;
                return self.lastIndex;
            }
        }
        self.lastIndex = -1;
        self.lastIndex
    }
    /// ## Parameters:
    /// None
    ///
    /// __Returns__ the pick index
    pub fn PickIndex(&self) -> usize {self.pickIndex}
    /// The limit on cars
    /// ## Parameters:
    /// None
    ///
    /// __Returns__ the limit on cars
    pub fn LimitCars(&self) -> usize {self.limitCars}
    /// Reset the last index
    /// None
    ///
    /// __Returns__ nothing
    pub fn ResetLastIndex(&mut self) {self.lastIndex = -1;}
    /// Is the selected SwitchListElement a pickup for the specified location
    /// ## Parameters:
    /// - Gx the switch list element index
    /// - Ix the industry index
    ///
    /// __Returns__ true if the Gx'th switch list element index is for the
    /// specified industry 
    pub fn PickLocationEq(&self, Gx: isize, Ix: usize) -> bool {
        if Gx < 0 || Gx as usize >= self.pickIndex {return false;}
        else {return self.theList[Gx as usize].PickLocation() == Ix;}
    }
    /// Is the selected SwitchListElement a pickup for the specified car
    /// ## Parameters:
    /// - Gx the switch list element index
    /// - Cx the car index
    ///
    /// __Returns__ true if the Gx'th switch list element index is for the
    /// specified car 
    pub fn PickCarEq(&self, Gx: isize, Cx: usize) -> bool {
        if Gx < 0 || Gx as usize >= self.pickIndex {return false;}
        else {return self.theList[Gx as usize].PickCar() == Cx;}
    }
    /// Is the selected SwitchListElement a pickup for the specified train
    /// ## Parameters:
    /// - Gx the switch list element index
    /// - Tx the train index
    ///
    /// __Returns__ true if the Gx'th switch list element index is for the
    /// specified train 
    pub fn PickTrainEq(&self, Gx: isize, Tx: usize) -> bool {
        if Gx < 0 || Gx as usize >= self.pickIndex {return false;}
        else {return self.theList[Gx as usize].PickTrain() == Tx;}
    }
    
}    

/// Add indexing Trait
impl Index<usize> for SwitchList {
    type Output = SwitchListElement;
    fn index(&self, i: usize) -> &Self::Output {
        &self.theList[i]
    }
}

/// Add mutable indexing Trait
impl IndexMut<usize> for SwitchList {
    //type Output = SwitchListElement;
    fn index_mut(&mut self, i: usize) -> &mut Self::Output {
        &mut self.theList[i]
    }
}