Project

General

Profile

Download (2.88 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / objects / RubikObjectCategoriesYear.java @ master

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2024 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.objects;
11

    
12
import java.util.Arrays;
13

    
14
///////////////////////////////////////////////////////////////////////////////////////////////////
15

    
16
public class RubikObjectCategoriesYear extends RubikObjectCategories
17
{
18
  private void buildSort(int[] data)
19
    {
20
    int numObjects = data.length;
21
    int[] sorted = new int[numObjects];
22
    System.arraycopy(data, 0, sorted, 0, numObjects);
23

    
24
    Arrays.sort(sorted);
25

    
26
    int[] categories = new int[numObjects];
27
    int numCategories = 0;
28

    
29
    for(int o=0; o<numObjects; o++)
30
      if( o==0 || sorted[o-1]!=sorted[o] )
31
        {
32
        categories[numCategories] = sorted[o];
33
        numCategories++;
34
        }
35

    
36
    int lastChange = -1;
37
    int curr = 0;
38
    int[] numInCategory = new int[numCategories];
39

    
40
    for(int o=0; o<numObjects; o++)
41
      if( o==numObjects-1 || sorted[o]!=sorted[o+1] )
42
        {
43
        numInCategory[curr] = o-lastChange;
44
        curr++;
45
        lastChange = o;
46
        }
47

    
48
    mObjectIndices = new int[numCategories][];
49

    
50
    for(int c=0; c<numCategories; c++)
51
      {
52
      mObjectIndices[c] = new int[numInCategory[c]];
53

    
54
      int cat = categories[c];
55
      int index = 0;
56

    
57
      for(int o=0; o<numObjects; o++)
58
        if( data[o]==cat ) mObjectIndices[c][index++] = o;
59
      }
60
    }
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
  void buildIndices()
65
    {
66
    int numObjects = RubikObjectList.getNumObjects();
67
    int[] years = new int[numObjects];
68

    
69
    for(int o=0; o<numObjects; o++)
70
      {
71
      RubikObject obj = RubikObjectList.getObject(o);
72
      years[o] = obj==null ? 0 : obj.getYearOfInvention();
73
      }
74

    
75
    buildSort(years);
76
    }
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

    
80
  void buildTitle()
81
    {
82
    mTitles = new String[mNumCategories];
83

    
84
    for(int t=0; t<mNumCategories; t++)
85
      {
86
      int obj = mObjectIndices[t][0];
87
      RubikObject object = RubikObjectList.getObject(obj);
88
      int year = object==null ? 0 : object.getYearOfInvention();
89
      mTitles[t] = year==0 ? "Unknown" : String.valueOf(year);
90
      }
91
    }
92
}
(6-6/7)