ctf: Plug the unsigned utils comparator into Stream comparator.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / internal / ctf / core / trace / StreamInputPacketIndex.java
... / ...
CommitLineData
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
11 * Contributors: Etienne Bergeron <etienne.bergeron@gmail.com>
12 * Contributors: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
13 *******************************************************************************/
14
15package org.eclipse.linuxtools.internal.ctf.core.trace;
16
17import java.util.ListIterator;
18import java.util.Vector;
19
20import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
21
22/**
23 * <b><u>StreamInputPacketIndex</u></b>
24 * <p>
25 * TODO Implement me. Please.
26 */
27public 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
43 /**
44 * Gets the entries
45 *
46 * @return the entries
47 */
48 public Vector<StreamInputPacketIndexEntry> getEntries() {
49 return this.entries;
50 }
51
52 /**
53 * Gets an iterator to the entries
54 *
55 * @return an iterator to the entries
56 */
57 public ListIterator<StreamInputPacketIndexEntry> listIterator() {
58 return this.entries.listIterator();
59 }
60
61 /**
62 * Gets an iterator to the entries at a given position
63 *
64 * @param n
65 * the position to get
66 * @return the iterator
67 */
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
82 * If there was a problem reading the entry
83 */
84 public void addEntry(StreamInputPacketIndexEntry entry)
85 throws CTFReaderException {
86 assert (entry.getContentSizeBits() != 0);
87 assert (entry.getContentSizeBits() != 0);
88
89 if (entry.getTimestampBegin() > entry.getTimestampEnd()) {
90 throw new CTFReaderException("Packet begin timestamp is after end timestamp"); //$NON-NLS-1$
91 }
92
93 if (!this.entries.isEmpty()) {
94 if (entry.getTimestampBegin() < this.entries.lastElement()
95 .getTimestampBegin()) {
96 throw new CTFReaderException("Packets begin timestamp decreasing"); //$NON-NLS-1$
97 }
98 }
99
100 this.entries.add(entry);
101 }
102
103 /**
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.
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
123 /*
124 * If the index is empty, return the iterator at the very beginning.
125 */
126 if (this.getEntries().isEmpty()) {
127 return this.getEntries().listIterator();
128 }
129
130 if (timestamp < 0) {
131 throw new IllegalArgumentException("timestamp is negative"); //$NON-NLS-1$
132 }
133
134 /* Binary search */
135 for (;;) {
136 /*
137 * Guess in the middle of min and max.
138 */
139 guessI = min + ((max - min) / 2);
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
150 if (timestamp <= guessEntry.getTimestampEnd()) {
151 /*
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.
154 */
155 max = guessI;
156 } else {
157 /*
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.
160 */
161 min = guessI + 1;
162 }
163 }
164
165 return this.entries.listIterator(guessI);
166 }
167}
This page took 0.026295 seconds and 5 git commands to generate.