Synthia
Generic and flexible data structure generator
Swap.java
Go to the documentation of this file.
1 /*
2  Synthia, a data structure generator
3  Copyright (C) 2019-2021 Laboratoire d'informatique formelle
4  Université du Québec à Chicoutimi, Canada
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 package ca.uqac.lif.synthia.collection;
20 
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 
25 import ca.uqac.lif.synthia.Picker;
26 import ca.uqac.lif.synthia.util.Mutator;
27 
28 /**
29  * Mutator that receives a list and selects two elements to be swapped.
30  *
31  * @param <T> The type of elements in the list
32  * @ingroup API
33  */
34 public class Swap<T> extends Mutator<List<T>>
35 {
36  /**
37  * A picker used to select the position of the first element.
38  */
39  /*@ non_null @*/ protected Picker<Float> m_position1;
40 
41  /**
42  * A picker used to select the position of the second element.
43  */
44  /*@ non_null @*/ protected Picker<Float> m_position2;
45 
46  /**
47  * Creates a new instance of swap.
48  * @param picker The underlying picker producing the lists to transform
49  * @param position1 A picker used to select the position of the first element
50  * @param position2 A picker used to select the position of the second element
51  */
52  public Swap(Picker<? extends List<T>> picker, Picker<Float> position1, Picker<Float> position2)
53  {
54  super(picker);
55  m_position1 = position1;
56  m_position2 = position2;
57  }
58 
59  @Override
60  public List<T> pick()
61  {
62  List<T> original = m_picker.pick();
63  int len = original.size();
64  int position1 = (int) Math.floor(m_position1.pick() * len);
65  int position2 = (int) Math.floor(m_position2.pick() * len);
66  List<T> new_list = new ArrayList<T>(len);
67  new_list.addAll(original);
68  Collections.swap(new_list, position1, position2);
69  return new_list;
70  }
71 
72  @Override
73  public Swap<T> duplicate(boolean with_state)
74  {
75  return new Swap<T>(m_picker.duplicate(with_state), m_position1.duplicate(with_state), m_position2.duplicate(with_state));
76  }
77 }
ca.uqac.lif.synthia.Picker
Picks an object.
Definition: Picker.java:36
ca.uqac.lif.synthia.util.Mutator.m_picker
Picker<? extends T > m_picker
The underlying picker producing the values to transform.
Definition: Mutator.java:38
ca.uqac.lif.synthia.util
Miscellaneous pickers performing various functions.
Definition: ArrayPicker.java:19
ca.uqac.lif.synthia.collection.Swap.Swap
Swap(Picker<? extends List< T >> picker, Picker< Float > position1, Picker< Float > position2)
Creates a new instance of swap.
Definition: Swap.java:52
ca.uqac
ca.uqac.lif.synthia
Definition: Bounded.java:19
ca.uqac.lif
ca.uqac.lif.synthia.collection.Swap.duplicate
Swap< T > duplicate(boolean with_state)
Creates a copy of the picker.
Definition: Swap.java:73
ca
ca.uqac.lif.synthia.Picker.pick
T pick()
Picks an object.
ca.uqac.lif.synthia.collection.Swap.pick
List< T > pick()
Picks an object.
Definition: Swap.java:60
ca.uqac.lif.synthia.util.Mutator
A picker that applies a transformation ("mutation") on the value produced by another picker.
Definition: Mutator.java:33
ca.uqac.lif.synthia.collection.Swap.m_position1
Picker< Float > m_position1
A picker used to select the position of the first element.
Definition: Swap.java:39
ca.uqac.lif.synthia.collection.Swap.m_position2
Picker< Float > m_position2
A picker used to select the position of the second element.
Definition: Swap.java:44
ca.uqac.lif.synthia.collection.Swap
Mutator that receives a list and selects two elements to be swapped.
Definition: Swap.java:34
ca.uqac.lif.synthia.Picker.duplicate
Picker< T > duplicate(boolean with_state)
Creates a copy of the picker.