Work on TmfCheckpoint
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / trace / StreamInputPacketIndex.java
CommitLineData
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
11 *******************************************************************************/
12
13package org.eclipse.linuxtools.ctf.core.trace;
14
15import java.util.Collection;
16import java.util.ListIterator;
17import java.util.Vector;
18
19/**
20 * <b><u>StreamInputPacketIndex</u></b>
21 * <p>
22 * TODO Implement me. Please.
23 */
24public class StreamInputPacketIndex {
25
26 // ------------------------------------------------------------------------
27 // Attributes
28 // ------------------------------------------------------------------------
29
30 /**
31 * Entries of the index. They are sorted by increasing begin timestamp.
32 * index builder.
33 */
34 private final Vector<StreamInputPacketIndexEntry> entries = new Vector<StreamInputPacketIndexEntry>();
35
36 // ------------------------------------------------------------------------
37 // Getters/Setters/Predicates
38 // ------------------------------------------------------------------------
39
40 public Collection<StreamInputPacketIndexEntry> getEntries() {
41 return this.entries;
42 }
43
44 public ListIterator<StreamInputPacketIndexEntry> listIterator() {
45 return this.entries.listIterator();
46 }
47
48 public ListIterator<StreamInputPacketIndexEntry> listIterator(int n) {
49 return this.entries.listIterator(n);
50 }
51
52 // ------------------------------------------------------------------------
53 // Operations
54 // ------------------------------------------------------------------------
55
56 /**
57 * Adds an entry to the index.
58 *
59 * @param entry
60 * The entry to add
61 * @throws CTFReaderException
62 */
63 public void addEntry(StreamInputPacketIndexEntry entry)
64 throws CTFReaderException {
65 assert (entry.packetSizeBits != 0);
66 assert (entry.contentSizeBits != 0);
67
68 if (entry.timestampBegin > entry.timestampEnd) {
69 throw new CTFReaderException(
70 "Packet begin timestamp is after end timestamp"); //$NON-NLS-1$
71 }
72
73 if (!this.entries.isEmpty()) {
74 if (entry.timestampBegin < this.entries.lastElement().timestampBegin) {
75 throw new CTFReaderException(
76 "Packets begin timestamp decreasing"); //$NON-NLS-1$
77 }
78 }
79
80 this.entries.add(entry);
81 }
82
83 /**
84 * Given a timestamp, this methods returns the first PacketIndexEntry that
85 * could include the timestamp, that is the last packet with a begin
86 * timestamp smaller than the given timestamp.
87 *
88 * @param timestamp
89 * The timestamp to look for.
90 * @return The StreamInputPacketEntry that corresponds to the packet that
91 * includes the given timestamp.
92 */
93 public ListIterator<StreamInputPacketIndexEntry> search(final long timestamp) {
94 /*
95 * Start with min and max covering all the elements.
96 */
97 int max = this.entries.size() - 1;
98 int min = 0;
99
100 int guessI;
101 StreamInputPacketIndexEntry guessEntry = null;
102
103 if (timestamp < 0) {
104 throw new IllegalArgumentException("timestamp is negative"); //$NON-NLS-1$
105 }
106
107 for (;;) {
108 /*
109 * Guess in the middle of min and max. The +1 is so that in case
110 * (min + 1 == max), we choose the packet at the subscript "max"
111 * instead of the one at "min". Otherwise, it would give an infinite
112 * loop.
113 */
114 guessI = (max + min + 1) / 2;
115 guessEntry = this.entries.get(guessI);
116
117 /*
118 * If we reached the point where we focus on a single packet, our
119 * search is done.
120 */
121 if (min == max) {
122 break;
123 }
124
125 if (timestamp < guessEntry.timestampBegin) {
126 /*
127 * If the timestamp if before the begin timestamp, we know that
128 * the packet to return is before the guess.
129 */
130 max = guessI - 1;
131 } else if (timestamp >= guessEntry.timestampBegin) {
132 /*
133 * If the timestamp is after the begin timestamp, we know that
134 * the packet to return is after the guess or is the guess.
135 */
136 min = guessI;
137 }
138 }
139
140 return this.entries.listIterator(guessI);
141 }
142
143}
This page took 0.035573 seconds and 5 git commands to generate.