summaryrefslogtreecommitdiffstats
path: root/lookup/BTSLut.java
blob: 6abfcb86ba66be2979b8d35f40002ac5d9f8ac69 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package lookup;

import helper.ListGPS;

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

import DataStructure.GPScoordinate;
import DataStructure.GSMMap;
import DataStructure.SingleBTS;

/**
 * No longer used!
 * 
 * @author richy
 * 
 */
public class BTSLut implements ILut {
	private LinkedList<GPScoordinate>[] possible_coords_DL = new LinkedList[69];
	private LinkedList<GPScoordinate>[] possible_coords_UL = new LinkedList[69];
	public int bts; // which bts does this Object store
	public int threshold_dB;
	public int threshold_m;

	/**
	 * create empty LUT
	 * 
	 * @param ARFCN
	 *            set BTS-ARFCN this object will take measurements and requests
	 */
	public BTSLut(int ARFCN, int threshold_dB, int threshold_m) {
		this.bts = ARFCN;
		this.threshold_dB = threshold_dB;
		this.threshold_m = threshold_m;
		// initialize strength array: -47 ... -115
		for (int i = 0; i < 69; i++) {
			possible_coords_DL[i] = new LinkedList<GPScoordinate>();
			possible_coords_UL[i] = new LinkedList<GPScoordinate>();
		}

	}

	public BTSLut(int ARFCN, GSMMap map, int threshold, int threshold_m) {
		this.bts = ARFCN;
		this.threshold_dB = threshold;
		this.threshold_m = threshold_m;
		for (int i = 0; i < 69; i++) {
			possible_coords_DL[i] = new LinkedList<GPScoordinate>();
			possible_coords_UL[i] = new LinkedList<GPScoordinate>();
		}
		// fill the LUT
		for (int x = 0; x < map.Xcoords.length; x++) {
			for (int y = 0; y < map.Ycoords.length; y++) {
				fill(map.getCoord(x, y), map.map[x][y]);
			}
		}

	}

	@Override
	public LinkedList<GPScoordinate> get(SingleBTS MR) {
		// hier muss noch das threshold_dB berücksichtigt werden!
		if (MR == null)
			return null;
		LinkedList<GPScoordinate> result = new LinkedList<GPScoordinate>();
		for (int i = threshold_dB * (-1); i <= threshold_dB; i++) {
			try {
				if (MR.fullBTS && MR.getDLcount() > 0) {
					// get coords for DL and UL and do a intersection
					// fix: do no intersection
					LinkedList<GPScoordinate> listUL = new LinkedList<GPScoordinate>();
					LinkedList<GPScoordinate> listDL = new LinkedList<GPScoordinate>();
					listUL = possible_coords_UL[getIndex(MR.getUldB()
							+ threshold_dB)];
					listDL = possible_coords_DL[getIndex(MR.getDldB()
							+ threshold_dB)];
					listUL = ListGPS.removeNullElements(listUL);
					listDL = ListGPS.removeNullElements(listDL);
					// result.addAll(helper.ListGPS.intersect(listDL,
					// listUL,threshold_m));
					result.addAll(listUL);
					result.addAll(listDL);
				} else {
					result.addAll(possible_coords_DL[(getIndex(MR.getDldB()
							+ threshold_dB))]);
				}
			} catch (Exception e) {
				// only index out of bound... ignore

			}
		}
		return result;

	}

	/**
	 * Takes list. Returns the lookup. Result is the intersection between all
	 * SingleBTSs in MR. That is, only Coordinates that are in common are
	 * returned
	 */

	@SuppressWarnings("unused")
	public LinkedList<GPScoordinate> get(List<SingleBTS> MR) {
		// create result list for each function call
		LinkedList<GPScoordinate> list = new LinkedList<GPScoordinate>();
		// create a nested-linked-list in order to compute the intersection
		LinkedList<LinkedList<GPScoordinate>> nestedlist = new LinkedList<LinkedList<GPScoordinate>>();

		// one list for all
		LinkedList<GPScoordinate> resultset = new LinkedList<GPScoordinate>();

		ListIterator<SingleBTS> itr = MR.listIterator();
		while (itr.hasNext()) {
			SingleBTS current = itr.next();
			resultset.addAll(get(current));
		}
		if (true)
			return resultset;

		for (SingleBTS current : MR) {
			// clear list
			list = new LinkedList<GPScoordinate>();
			// save results in that list. do that list into the list-list
			list.addAll(get(current));
			list = ListGPS.removeNullElements(list);
			if (list != null && !list.isEmpty())
				nestedlist.addLast(list);
		}

		// -------
		// from here on only calculating intersect
		// -------

		// reuse list. take it to store final result
		nestedlist = ListGPS.removeAllNullElements(nestedlist);
		if (list != null)
			list.clear();
		if (nestedlist == null || nestedlist.isEmpty())
			return null;
		if (nestedlist.size() == 1)
			return nestedlist.getFirst();
		// nestedlist has at least 2 elements. do intersect with both. Store in
		// list.
		list.addAll(ListGPS.intersect(nestedlist.removeFirst(),
				nestedlist.removeLast(), threshold_m));
		// now do intersect with the rest. This could get a speedup by using
		// pairwise intersection. would be O(log n) then.
		while (!nestedlist.isEmpty()) {
			list = ListGPS.intersect(list, nestedlist.removeFirst(),
					threshold_m);
		}
		// TODO: only null elements are here!
		return list;

	}

	/**
	 * Returns index where the strength (dBm) is stored in the array
	 * 
	 * @param strength
	 *            . Gets rounded
	 * @return
	 */
	private int getIndex(double strength) {
		int rounded = (int) Math.round(strength);
		// this could be done with math.abs
		if (strength < 0) {
			return -47 - rounded;
		} else {
			return rounded - 47;
		}

	}

	@Override
	public void fill(GPScoordinate where, SingleBTS what) {
		if (what == null || what.ARFCN != bts)
			return;
		int hit = 0;
		if (what.getDldB() != 0) {
			hit = getIndex(what.getDldB());
			possible_coords_DL[hit].add(where);
		}
		if (what.getUldB() != 0) {
			hit = getIndex(what.getUldB());
			possible_coords_UL[hit].add(where);
		}

	}

	public void fill(GPScoordinate where, List<SingleBTS> what) {
		if (what == null)
			return;
		for (SingleBTS current : what) {
			fill(where, current);
		}
	}

}