Synthia
Generic and flexible data structure generator
ComposeShrunkList.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.List;
23 
25 import ca.uqac.lif.synthia.Picker;
28 
29 /**
30  * Given a reference list, picks lists that are smaller.
31  * Given a list <i>L</i>, a list <i>L</i>' is "smaller" list if either:
32  * <ul>
33  * <li><i>L</i>' has strictly fewer elements than <i>L</i>, or</li>
34  * <li><i>L</i>' has the same number of elements <i>n</i>, and there
35  * exists an index <i>i</i> &leq; <i>n</i> such that <i>L</i>[<i>i</i>] &lt;
36  * <i>L</i>'[<i>i</i>] and <i>L</i>[<i>j</i>] = <i>L</i>'[<i>j</i>] for
37  * all <i>j</i> &lt; <i>i</i>
38  * </ul>
39  * @author Sylvain Hallé
40  *
41  * @param <T> The type of the elements in the list
42  */
43 public class ComposeShrunkList<T> implements Picker<List<T>>, Shrinkable<List<T>>
44 {
46 
48 
49  protected List<T> m_reference;
50 
52 
53  protected float m_magnitude;
54 
55  public ComposeShrunkList(Shrinkable<T> element_picker, Shrinkable<Integer> length, List<T> reference, Picker<Float> decision, float magnitude)
56  {
57  super();
58  m_originalPicker = element_picker;
59  m_reference = reference;
60  m_decision = decision;
61  m_length = length;
62  m_magnitude = magnitude;
63  }
64 
65  public ComposeShrunkList(Shrinkable<T> element_picker, Shrinkable<Integer> length, List<T> reference)
66  {
67  this(element_picker, length, reference, RandomFloat.instance, 1);
68  }
69 
70  @Override
71  public Shrinkable<List<T>> shrink(List<T> list, Picker<Float> d, float magnitude)
72  {
73  return new ComposeShrunkList<T>(m_originalPicker, m_length, list, d, magnitude);
74  }
75 
76  @Override
77  public void reset()
78  {
79  // TODO Auto-generated method stub
80 
81  }
82 
83  @Override
84  public List<T> pick()
85  {
86  int ref_size = m_reference.size();
87  if (m_decision.pick() < (1 - m_magnitude))
88  {
89  // Generate a shorter list
90  int new_length = m_length.shrink(ref_size, m_decision, m_magnitude).pick();
91  List<T> new_list = new ComparableList<T>();
92  for (int i = 0; i < new_length; i++)
93  {
94  new_list.add(m_originalPicker.pick());
95  }
96  return new_list;
97  }
98  // Generate a list with smaller elements
99  int prefix_length = (int) (ref_size * m_decision.pick() * (1 - m_magnitude));
100  List<T> new_list = new ComparableList<T>();
101  for (int i = 0; i < ref_size; i++)
102  {
103  if (i < prefix_length)
104  {
105  if (m_decision.pick() < m_magnitude)
106  {
107  new_list.add(m_reference.get(i));
108  }
109  else
110  {
111  Picker<T> shrunk = m_originalPicker.shrink(m_reference.get(i), m_decision, m_magnitude);
112  try
113  {
114  new_list.add(shrunk.pick());
115  }
116  catch (NoMoreElementException e)
117  {
118  // This element can no longer be shrunk, leave it alone
119  new_list.add(m_reference.get(i));
120  }
121  }
122  }
123  else if (i == prefix_length)
124  {
125  Picker<T> shrunk = m_originalPicker.shrink(m_reference.get(i), m_decision, m_magnitude);
126  try
127  {
128  new_list.add(shrunk.pick());
129  }
130  catch (NoMoreElementException e)
131  {
132  // This element can no longer be shrunk, leave it alone
133  new_list.add(m_reference.get(i));
134  }
135  }
136  else
137  {
138  new_list.add(m_originalPicker.pick());
139  }
140  }
141  return new_list;
142  }
143 
144  @Override
145  public Picker<List<T>> duplicate(boolean with_state)
146  {
147  // TODO Auto-generated method stub
148  return null;
149  }
150 
151  @Override
152  public Shrinkable<List<T>> shrink(List<T> o)
153  {
154  return shrink(o, RandomFloat.instance, 1);
155  }
156 }
ca.uqac.lif.synthia.Picker
Picks an object.
Definition: Picker.java:36
ca.uqac.lif.synthia.Shrinkable
Interface signaling that a picker can be shrunk.
Definition: Shrinkable.java:36
ca.uqac.lif.synthia.collection.ComposeShrunkList.pick
List< T > pick()
Picks an object.
Definition: ComposeShrunkList.java:84
ca.uqac.lif.synthia.collection.ComposeShrunkList.reset
void reset()
Puts the picker back into its initial state.
Definition: ComposeShrunkList.java:77
ca.uqac.lif.synthia.Shrinkable.shrink
Shrinkable< T > shrink(T o, Picker< Float > d, float m)
Shrinks a picker.
ca.uqac.lif.synthia.collection.ComparableList
Class that extends Java's ArrayList class to implements the Comparable interface.
Definition: ComparableList.java:31
ca.uqac.lif.synthia.random.RandomFloat.instance
static final transient RandomFloat instance
A public static instance of RandomFloat.
Definition: RandomFloat.java:45
ca.uqac.lif.synthia.collection.ComposeShrunkList.m_decision
Picker< Float > m_decision
Definition: ComposeShrunkList.java:51
ca.uqac
ca.uqac.lif.synthia.collection.ComposeShrunkList.m_magnitude
float m_magnitude
Definition: ComposeShrunkList.java:53
ca.uqac.lif.synthia.random
Pickers that produce pseudo-random objects such as numbers.
Definition: AffineTransform.java:19
ca.uqac.lif.synthia
Definition: Bounded.java:19
ca.uqac.lif.synthia.collection.ComposeShrunkList.m_reference
List< T > m_reference
Definition: ComposeShrunkList.java:49
ca.uqac.lif.synthia.collection.ComposeShrunkList.m_originalPicker
Shrinkable< T > m_originalPicker
Definition: ComposeShrunkList.java:45
ca.uqac.lif
ca
ca.uqac.lif.synthia.Picker.pick
T pick()
Picks an object.
ca.uqac.lif.synthia.collection.ComposeShrunkList.shrink
Shrinkable< List< T > > shrink(List< T > list, Picker< Float > d, float magnitude)
Definition: ComposeShrunkList.java:71
ca.uqac.lif.synthia.collection.ComposeShrunkList.m_length
Shrinkable< Integer > m_length
Definition: ComposeShrunkList.java:47
ca.uqac.lif.synthia.random.RandomFloat
Picks a floating point number uniformly in an interval.
Definition: RandomFloat.java:30
ca.uqac.lif.synthia.NoMoreElementException
An exception to throw when a picker can't pick an other element.
Definition: NoMoreElementException.java:25
ca.uqac.lif.synthia.collection.ComposeShrunkList.shrink
Shrinkable< List< T > > shrink(List< T > o)
Definition: ComposeShrunkList.java:152
ca.uqac.lif.synthia.collection.ComposeShrunkList.ComposeShrunkList
ComposeShrunkList(Shrinkable< T > element_picker, Shrinkable< Integer > length, List< T > reference, Picker< Float > decision, float magnitude)
Definition: ComposeShrunkList.java:55
ca.uqac.lif.synthia.collection.ComposeShrunkList.duplicate
Picker< List< T > > duplicate(boolean with_state)
Creates a copy of the picker.
Definition: ComposeShrunkList.java:145
ca.uqac.lif.synthia.collection.ComposeShrunkList.ComposeShrunkList
ComposeShrunkList(Shrinkable< T > element_picker, Shrinkable< Integer > length, List< T > reference)
Definition: ComposeShrunkList.java:65
ca.uqac.lif.synthia.collection.ComposeShrunkList
Given a reference list, picks lists that are smaller.
Definition: ComposeShrunkList.java:43