1+ /* ---------------------------------------------------------------------
2+ * Numenta Platform for Intelligent Computing (NuPIC)
3+ * Copyright (C) 2014, Numenta, Inc. Unless you have an agreement
4+ * with Numenta, Inc., for a separate license for this software code, the
5+ * following terms and conditions apply:
6+ *
7+ * This program is free software: you can redistribute it and/or modify
8+ * it under the terms of the GNU Affero Public License version 3 as
9+ * published by the Free Software Foundation.
10+ *
11+ * This program is distributed in the hope that it will be useful,
12+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14+ * See the GNU Affero Public License for more details.
15+ *
16+ * You should have received a copy of the GNU Affero Public License
17+ * along with this program. If not, see http://www.gnu.org/licenses.
18+ *
19+ * http://numenta.org/licenses/
20+ * ---------------------------------------------------------------------
21+ */
22+ package org .numenta .nupic ;
23+
24+ import java .util .Arrays ;
25+ import java .util .Collection ;
26+ import java .util .List ;
27+ import java .util .Set ;
28+ import java .util .stream .IntStream ;
29+
30+ import org .numenta .nupic .model .Cell ;
31+ import org .numenta .nupic .model .Column ;
32+
33+ /**
34+ * <p>
35+ * For now, a utility class for convenience operations
36+ * on integer arrays understood to be algorithmic inputs
37+ * and outputs; and conversions to and from canonical objects.
38+ * </p><p>
39+ * Later, this may become the encapsulation of the vectors
40+ * representing SDRs and previously treated as integer arrays.
41+ * </p><p>
42+ * <b>NOTE:</b> <em>Eclipse is not up to date with its leakable resource inspection.
43+ * Streams not derived from channels (i.e. from arrays or lists) do not
44+ * need explicit closing.</em>
45+ * </p>
46+ * <p>
47+ * see here: http://stackoverflow.com/questions/25796118/java-8-streams-and-try-with-resources
48+ * </p>
49+ * @author cogmission
50+ */
51+ public class SDR {
52+
53+ /**
54+ * Converts a vector of {@link Cell} indexes to {@link Column} indexes.
55+ *
56+ * @param cells the indexes of the cells to convert
57+ * @param cellsPerColumn the defined number of cells per column
58+ * false if not.
59+ * @return the column indexes of the specified cells.
60+ */
61+ public static int [] asColumnIndices (int [] cells , int cellsPerColumn ) {
62+ IntStream op = Arrays .stream (cells );
63+ return op .map (cell -> cell / cellsPerColumn ).distinct ().toArray ();
64+ }
65+
66+ /**
67+ * Converts a vector of {@link Cell} indexes to {@link Column} indexes.
68+ *
69+ * @param cells the indexes of the cells to convert
70+ * @param cellsPerColumn the defined number of cells per column
71+ * false if not.
72+ * @return the column indexes of the specified cells.
73+ */
74+ public static int [] asColumnIndices (List <Integer > cells , int cellsPerColumn ) {
75+ IntStream op = cells .stream ().mapToInt (c -> c );
76+ return op .map (cellIdx -> cellIdx / cellsPerColumn ).distinct ().toArray ();
77+ }
78+
79+ /**
80+ * Converts a List of {@link Cell}s to {@link Column} indexes.
81+ *
82+ * @param cells the list of cells to convert
83+ * @param cellsPerColumn the defined number of cells per column
84+ * false if not.
85+ * @return the column indexes of the specified cells.
86+ */
87+ public static int [] cellsToColumns (List <Cell > cells , int cellsPerColumn ) {
88+ IntStream op = cells .stream ().mapToInt (c -> c .getIndex ());
89+
90+ return op .map (cellIdx -> cellIdx / cellsPerColumn ).distinct ().toArray ();
91+ }
92+
93+ /**
94+ * Converts a Set of {@link Cell}s to {@link Column} indexes.
95+ *
96+ * @param cells the list of cells to convert
97+ * @param cellsPerColumn the defined number of cells per column
98+ *
99+ * @return the column indexes of the specified cells.
100+ */
101+ public static int [] cellsAsColumnIndices (Set <Cell > cells , int cellsPerColumn ) {
102+ return cells .stream ().mapToInt (c -> c .getIndex ())
103+ .sorted ().map (cellIdx -> cellIdx / cellsPerColumn ).distinct ().toArray ();
104+ }
105+
106+ /**
107+ * Converts a {@link Collection} of {@link Cell}s to a list
108+ * of cell indexes.
109+ *
110+ * @param cells
111+ * @return
112+ */
113+ public static int [] asCellIndices (Collection <Cell > cells ) {
114+ return cells .stream ().mapToInt (cell -> cell .getIndex ()).sorted ().toArray ();
115+ }
116+ }
0 commit comments