1 /*
2  * This file is part of ELKI:
3  * Environment for Developing KDD-Applications Supported by Index-Structures
4  *
5  * Copyright (C) 2018
6  * ELKI Development Team
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Affero General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Affero General Public License for more details.
17  *
18  * You should have received a copy of the GNU Affero General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 package de.lmu.ifi.dbs.elki.database.relation;
22 
23 import de.lmu.ifi.dbs.elki.data.projection.Projection;
24 import de.lmu.ifi.dbs.elki.data.type.SimpleTypeInformation;
25 import de.lmu.ifi.dbs.elki.database.ids.DBIDIter;
26 import de.lmu.ifi.dbs.elki.database.ids.DBIDRef;
27 import de.lmu.ifi.dbs.elki.database.ids.DBIDs;
28 import de.lmu.ifi.dbs.elki.logging.Logging;
29 
30 /**
31  * Projected relation view (non-materialized)
32  *
33  * @author Erich Schubert
34  * @since 0.5.0
35  *
36  * @param <IN> Vector type
37  * @param <OUT> Vector type
38  */
39 public class ProjectedView<IN, OUT> extends AbstractRelation<OUT> {
40   /**
41    * Class logger
42    */
43   private static final Logging LOG = Logging.getLogger(ProjectedView.class);
44 
45   /**
46    * The wrapped representation where we get the IDs from.
47    */
48   private final Relation<? extends IN> inner;
49 
50   /**
51    * The projection we use.
52    */
53   private Projection<IN, OUT> projection;
54 
55   /**
56    * Constructor.
57    *
58    * @param inner Inner relation
59    * @param projection Projection function
60    */
ProjectedView(Relation<? extends IN> inner, Projection<IN, OUT> projection)61   public ProjectedView(Relation<? extends IN> inner, Projection<IN, OUT> projection) {
62     super();
63     this.inner = inner;
64     this.projection = projection;
65     projection.initialize(inner.getDataTypeInformation());
66   }
67 
68   @Override
getLongName()69   public String getLongName() {
70     return "projection";
71   }
72 
73   @Override
getShortName()74   public String getShortName() {
75     return "projection";
76   }
77 
78   @Override
get(DBIDRef id)79   public OUT get(DBIDRef id) {
80     return projection.project(inner.get(id));
81   }
82 
83   @Override
getDataTypeInformation()84   public SimpleTypeInformation<OUT> getDataTypeInformation() {
85     return projection.getOutputDataTypeInformation();
86   }
87 
88   @Override
getDBIDs()89   public DBIDs getDBIDs() {
90     return inner.getDBIDs();
91   }
92 
93   @Override
iterDBIDs()94   public DBIDIter iterDBIDs() {
95     return inner.iterDBIDs();
96   }
97 
98   @Override
size()99   public int size() {
100     return inner.size();
101   }
102 
103   @Override
getLogger()104   protected Logging getLogger() {
105     return LOG;
106   }
107 }