Commit | Line | Data |
---|---|---|
866e5b51 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are made | |
5 | * available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: Matthew Khouzam - Initial API and implementation | |
10 | * Contributors: Simon Marchi - Initial API and implementation | |
d68f46c2 EB |
11 | * Contributors: Etienne Bergeron <etienne.bergeron@gmail.com> |
12 | * Contributors: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
866e5b51 FC |
13 | *******************************************************************************/ |
14 | ||
ce2388e0 | 15 | package org.eclipse.linuxtools.internal.ctf.core.trace; |
866e5b51 | 16 | |
866e5b51 FC |
17 | import java.util.ListIterator; |
18 | import java.util.Vector; | |
19 | ||
ce2388e0 FC |
20 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; |
21 | ||
866e5b51 FC |
22 | /** |
23 | * <b><u>StreamInputPacketIndex</u></b> | |
24 | * <p> | |
25 | * TODO Implement me. Please. | |
26 | */ | |
27 | public class StreamInputPacketIndex { | |
28 | ||
29 | // ------------------------------------------------------------------------ | |
30 | // Attributes | |
31 | // ------------------------------------------------------------------------ | |
32 | ||
33 | /** | |
34 | * Entries of the index. They are sorted by increasing begin timestamp. | |
35 | * index builder. | |
36 | */ | |
37 | private final Vector<StreamInputPacketIndexEntry> entries = new Vector<StreamInputPacketIndexEntry>(); | |
38 | ||
39 | // ------------------------------------------------------------------------ | |
40 | // Getters/Setters/Predicates | |
41 | // ------------------------------------------------------------------------ | |
42 | ||
9ac2eb62 MK |
43 | /** |
44 | * Gets the entries | |
b73145e2 | 45 | * |
9ac2eb62 MK |
46 | * @return the entries |
47 | */ | |
ce2388e0 | 48 | public Vector<StreamInputPacketIndexEntry> getEntries() { |
866e5b51 FC |
49 | return this.entries; |
50 | } | |
51 | ||
9ac2eb62 MK |
52 | /** |
53 | * Gets an iterator to the entries | |
b73145e2 | 54 | * |
9ac2eb62 MK |
55 | * @return an iterator to the entries |
56 | */ | |
866e5b51 FC |
57 | public ListIterator<StreamInputPacketIndexEntry> listIterator() { |
58 | return this.entries.listIterator(); | |
59 | } | |
60 | ||
9ac2eb62 MK |
61 | /** |
62 | * Gets an iterator to the entries at a given position | |
b73145e2 JCK |
63 | * |
64 | * @param n | |
65 | * the position to get | |
9ac2eb62 MK |
66 | * @return the iterator |
67 | */ | |
866e5b51 FC |
68 | public ListIterator<StreamInputPacketIndexEntry> listIterator(int n) { |
69 | return this.entries.listIterator(n); | |
70 | } | |
71 | ||
72 | // ------------------------------------------------------------------------ | |
73 | // Operations | |
74 | // ------------------------------------------------------------------------ | |
75 | ||
76 | /** | |
77 | * Adds an entry to the index. | |
78 | * | |
79 | * @param entry | |
80 | * The entry to add | |
81 | * @throws CTFReaderException | |
be6df2d8 | 82 | * If there was a problem reading the entry |
866e5b51 FC |
83 | */ |
84 | public void addEntry(StreamInputPacketIndexEntry entry) | |
85 | throws CTFReaderException { | |
ce2388e0 FC |
86 | assert (entry.getContentSizeBits() != 0); |
87 | assert (entry.getContentSizeBits() != 0); | |
866e5b51 | 88 | |
ce2388e0 | 89 | if (entry.getTimestampBegin() > entry.getTimestampEnd()) { |
b73145e2 | 90 | throw new CTFReaderException("Packet begin timestamp is after end timestamp"); //$NON-NLS-1$ |
866e5b51 FC |
91 | } |
92 | ||
93 | if (!this.entries.isEmpty()) { | |
bfe038ff MK |
94 | if (entry.getTimestampBegin() < this.entries.lastElement() |
95 | .getTimestampBegin()) { | |
b73145e2 | 96 | throw new CTFReaderException("Packets begin timestamp decreasing"); //$NON-NLS-1$ |
866e5b51 FC |
97 | } |
98 | } | |
99 | ||
100 | this.entries.add(entry); | |
101 | } | |
102 | ||
103 | /** | |
d68f46c2 EB |
104 | * This method returns the first packet with the end timestamp greater |
105 | * or equal to the given timestamp. The returned packet is the first one | |
106 | * that could include the timestamp. | |
866e5b51 FC |
107 | * |
108 | * @param timestamp | |
109 | * The timestamp to look for. | |
110 | * @return The StreamInputPacketEntry that corresponds to the packet that | |
111 | * includes the given timestamp. | |
112 | */ | |
113 | public ListIterator<StreamInputPacketIndexEntry> search(final long timestamp) { | |
114 | /* | |
115 | * Start with min and max covering all the elements. | |
116 | */ | |
117 | int max = this.entries.size() - 1; | |
118 | int min = 0; | |
119 | ||
120 | int guessI; | |
121 | StreamInputPacketIndexEntry guessEntry = null; | |
122 | ||
4a110860 AM |
123 | /* |
124 | * If the index is empty, return the iterator at the very beginning. | |
125 | */ | |
b73145e2 | 126 | if (this.getEntries().isEmpty()) { |
4a110860 AM |
127 | return this.getEntries().listIterator(); |
128 | } | |
129 | ||
866e5b51 FC |
130 | if (timestamp < 0) { |
131 | throw new IllegalArgumentException("timestamp is negative"); //$NON-NLS-1$ | |
132 | } | |
133 | ||
d68f46c2 | 134 | /* Binary search */ |
866e5b51 FC |
135 | for (;;) { |
136 | /* | |
d68f46c2 | 137 | * Guess in the middle of min and max. |
866e5b51 | 138 | */ |
d68f46c2 | 139 | guessI = min + ((max - min) / 2); |
866e5b51 FC |
140 | guessEntry = this.entries.get(guessI); |
141 | ||
142 | /* | |
143 | * If we reached the point where we focus on a single packet, our | |
144 | * search is done. | |
145 | */ | |
146 | if (min == max) { | |
147 | break; | |
148 | } | |
149 | ||
d68f46c2 | 150 | if (timestamp <= guessEntry.getTimestampEnd()) { |
866e5b51 | 151 | /* |
d68f46c2 EB |
152 | * If the timestamp is lower or equal to the end of the guess packet, |
153 | * then the guess packet becomes the new inclusive max. | |
866e5b51 | 154 | */ |
d68f46c2 EB |
155 | max = guessI; |
156 | } else { | |
866e5b51 | 157 | /* |
d68f46c2 EB |
158 | * If the timestamp is greater than the end of the guess packet, then |
159 | * the new inclusive min is the packet after the guess packet. | |
866e5b51 | 160 | */ |
d68f46c2 | 161 | min = guessI + 1; |
866e5b51 FC |
162 | } |
163 | } | |
164 | ||
ce2388e0 FC |
165 | return this.entries.listIterator(guessI); |
166 | } | |
866e5b51 | 167 | } |