<commit_before>/*
Copyright 2005 <NAME>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package org.gavrog.joss.geometry;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.gavrog.box.simple.Strings;
import org.gavrog.joss.pgraphs.io.DataFormatException;

/**
 * This class handles catalogues of known space groups. For the time being, everything
 * here is static and the input files are hardwired.
 * 
 * @author <NAME>
 * @version $Id: SpaceGroupCatalogue.java,v 1.4 2005/10/02 23:26:46 odf Exp $
 */
public class SpaceGroupCatalogue {
    /**
     * Making the constructor private prevents instantiation (I hope).
     */
    private SpaceGroupCatalogue() {
    }
    
    /**
     * This class is used to represent a table of space group settings of a
     * given dimension.
     */
    private static class Table {
        final public int dimension;
        final public Map nameToOps = new HashMap();
        final public Map nameToTransform = new HashMap();
        
        public Table(final int dimension) {
            this.dimension = dimension;
        }
    }
    
    static private Table groupTables[] = new Table[5];
    static private Map aliases = new HashMap();
    
    /**
     * Represents lookup information for groups, as used by {@link SpaceGroupFinder}.
     */
    static class Lookup {
        final public String name;
        final public int system;
        final public String centering;
        final public BasisChange fromStd;
        
        public Lookup(final String name, final int system, final String centering,
                final BasisChange fromStd) {
            this.name = name;
            this.system = system;
            this.centering = centering;
            this.fromStd = fromStd;
        }
    }
    
    static private Map lookup = new HashMap();
    
    /**
     * Parses space group settings from a file and stores them statically. Each setting is
     * identified by a name and the transformation used to derive it from the canonical
     * setting of the group, both given in the first input line. The following lines list
     * the operators for the group.
     * 
     * CAVEAT: currently, due to the way the constructors are implemented, a full list of
     * operators must be given. Just a set of generators is not sufficient.
     * 
     * TODO make this accept generator lists
     * 
     * @param filename
     */
    private static void parseGroups(final String filename) {
        final InputStream inStream = ClassLoader.getSystemResourceAsStream(filename);
        final BufferedReader reader = new BufferedReader(new InputStreamReader(inStream));
    
        Table table = null;
        String currentName = null;
        
        while (true) {
            final String line;
            try {
                line = reader.readLine();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            if (line == null) {
                break;
            }
            if (line.length() == 0 || line.trim().charAt(0) == '#') {
                continue;
            }
            final int i = line.indexOf(' ');
            if (i > 0) {
                if (currentName != null) {
                    final Map map = table.nameToOps;
                    final String key = currentName;
                    map.put(key, Collections.unmodifiableList((List) map.get(key)));
                }

                final String fields[] = line.trim().split("\\s+");
                if (fields[0].equalsIgnoreCase("alias")) {
                    aliases.put(fields[1], fields[2]);
                } else if (fields[0].equalsIgnoreCase("lookup")) {
                    final String name = fields[1];
                    final int system;
                    if (fields[2].equals("monoclinic")) {
                        system = SpaceGroupFinder.MONOCLINIC_SYSTEM;
                    } else if (fields[2].equals("triclinic")) {
                        system = SpaceGroupFinder.TRICLINIC_SYSTEM;
                    } else if (fields[2].equals("orthorhombic")) {
                        system = SpaceGroupFinder.ORTHORHOMBIC_SYSTEM;
                    } else if (fields[2].equals("trigonal")) {
                        system = SpaceGroupFinder.TRIGONAL_SYSTEM;
                    } else if (fields[2].equals("tetragonal")) {
                        system = SpaceGroupFinder.TETRAGONAL_SYSTEM;
                    } else if (fields[2].equals("hexagonal")) {
                        system = SpaceGroupFinder.HEXAGONAL_SYSTEM;
                    } else if (fields[2].equals("cubic")) {
                        system = SpaceGroupFinder.CUBIC_SYSTEM;
                    } else {
                        throw new RuntimeException(fields[2] + " system unknown");
                    }
                    final String centering = fields[3];
                    final BasisChange fromStd = new BasisChange(new Operator(fields[4]));
                    lookup.put(name, new Lookup(name, system, centering, fromStd));
                } else {
                    currentName = fields[0];
                    final Operator T = new Operator(line.substring(i + 1));
                    final int d = T.getDimension();
                    if (groupTables[d] == null) {
                        groupTables[d] = new Table(d);
                    }
                    table = groupTables[d];
                    table.nameToOps.put(currentName, new LinkedList());
                    table.nameToTransform.put(currentName, T);
                }
            } else if (currentName != null) {
                final Operator op = new Operator(line).modZ();
                ((List) table.nameToOps.get(currentName)).add(op);
            } else {
                throw new DataFormatException("error in space group table file");
            }
        }
    }

    /**
     * The name of the file to read space group settings from.
     */
    final private static Package pkg = SpaceGroupCatalogue.class.getPackage();
    final private static String packagePath = pkg.getName().replaceAll("\\.", "/");
    final private static String tablePath = packagePath + "/sgtable.data";
    
    /**
     * Retrieves the list of all known names for group settings for a given dimension.
     * 
     * CAVEAT: a group may have multiple settings, so this method may return more than one
     * name for each individual group.
     * 
     * @param dimension the common dimension of the space groups.
     * @return an iterator over the names of space group settings.
     */
    public static Iterator settingNames(final int dimension) {
        if (groupTables[dimension] == null) {
            parseGroups(tablePath);
        }
    
        return groupTables[dimension].nameToOps.keySet().iterator();
    }

    /**
     * Retrieves information about a given space group setting. The setting is identified<commit_msg>Added a method to return an iterator over the keys in the lookup table.<commit_after>/*
Copyright 2005 <NAME>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package org.gavrog.joss.geometry;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.gavrog.box.simple.Strings;
import org.gavrog.joss.pgraphs.io.DataFormatException;

/**
 * This class handles catalogues of known space groups. For the time being, everything
 * here is static and the input files are hardwired.
 * 
 * @author <NAME>
 * @version $Id: SpaceGroupCatalogue.java,v 1.5 2005/10/02 23:29:52 odf Exp $
 */
public class SpaceGroupCatalogue {
    /**
     * Making the constructor private prevents instantiation (I hope).
     */
    private SpaceGroupCatalogue() {
    }
    
    /**
     * This class is used to represent a table of space group settings of a
     * given dimension.
     */
    private static class Table {
        final public int dimension;
        final public Map nameToOps = new HashMap();
        final public Map nameToTransform = new HashMap();
        
        public Table(final int dimension) {
            this.dimension = dimension;
        }
    }
    
    static private Table groupTables[] = new Table[5];
    static private Map aliases = new HashMap();
    
    /**
     * Represents lookup information for groups, as used by {@link SpaceGroupFinder}.
     */
    static class Lookup {
        final public String name;
        final public int system;
        final public String centering;
        final public BasisChange fromStd;
        
        public Lookup(final String name, final int system, final String centering,
                final BasisChange fromStd) {
            this.name = name;
            this.system = system;
            this.centering = centering;
            this.fromStd = fromStd;
        }
    }
    
    static private Map lookup = new HashMap();
    
    /**
     * Parses space group settings from a file and stores them statically. Each setting is
     * identified by a name and the transformation used to derive it from the canonical
     * setting of the group, both given in the first input line. The following lines list
     * the operators for the group.
     * 
     * CAVEAT: currently, due to the way the constructors are implemented, a full list of
     * operators must be given. Just a set of generators is not sufficient.
     * 
     * TODO make this accept generator lists
     * 
     * @param filename
     */
    private static void parseGroups(final String filename) {
        final InputStream inStream = ClassLoader.getSystemResourceAsStream(filename);
        final BufferedReader reader = new BufferedReader(new InputStreamReader(inStream));
    
        Table table = null;
        String currentName = null;
        
        while (true) {
            final String line;
            try {
                line = reader.readLine();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            if (line == null) {
                break;
            }
            if (line.length() == 0 || line.trim().charAt(0) == '#') {
                continue;
            }
            final int i = line.indexOf(' ');
            if (i > 0) {
                if (currentName != null) {
                    final Map map = table.nameToOps;
                    final String key = currentName;
                    map.put(key, Collections.unmodifiableList((List) map.get(key)));
                }

                final String fields[] = line.trim().split("\\s+");
                if (fields[0].equalsIgnoreCase("alias")) {
                    aliases.put(fields[1], fields[2]);
                } else if (fields[0].equalsIgnoreCase("lookup")) {
                    final String name = fields[1];
                    final int system;
                    if (fields[2].equals("monoclinic")) {
                        system = SpaceGroupFinder.MONOCLINIC_SYSTEM;
                    } else if (fields[2].equals("triclinic")) {
                        system = SpaceGroupFinder.TRICLINIC_SYSTEM;
                    } else if (fields[2].equals("orthorhombic")) {
                        system = SpaceGroupFinder.ORTHORHOMBIC_SYSTEM;
                    } else if (fields[2].equals("trigonal")) {
                        system = SpaceGroupFinder.TRIGONAL_SYSTEM;
                    } else if (fields[2].equals("tetragonal")) {
                        system = SpaceGroupFinder.TETRAGONAL_SYSTEM;
                    } else if (fields[2].equals("hexagonal")) {
                        system = SpaceGroupFinder.HEXAGONAL_SYSTEM;
                    } else if (fields[2].equals("cubic")) {
                        system = SpaceGroupFinder.CUBIC_SYSTEM;
                    } else {
                        throw new RuntimeException(fields[2] + " system unknown");
                    }
                    final String centering = fields[3];
                    final BasisChange fromStd = new BasisChange(new Operator(fields[4]));
                    lookup.put(name, new Lookup(name, system, centering, fromStd));
                } else {
                    currentName = fields[0];
                    final Operator T = new Operator(line.substring(i + 1));
                    final int d = T.getDimension();
                    if (groupTables[d] == null) {
                        groupTables[d] = new Table(d);
                    }
                    table = groupTables[d];
                    table.nameToOps.put(currentName, new LinkedList());
                    table.nameToTransform.put(currentName, T);
                }
            } else if (currentName != null) {
                final Operator op = new Operator(line).modZ();
                ((List) table.nameToOps.get(currentName)).add(op);
            } else {
                throw new DataFormatException("error in space group table file");
            }
        }
    }

    /**
     * The name of the file to read space group settings from.
     */
    final private static Package pkg = SpaceGroupCatalogue.class.getPackage();
    final private static String packagePath = pkg.getName().replaceAll("\\.", "/");
    final private static String tablePath = packagePath + "/sgtable.data";
    
    /**
     * Retrieves the list of all known names for group settings for a given dimension.
     * 
     * CAVEAT: a group may have multiple settings, so this method may return more than one
     * name for each individual group.
     * 
     * @param dimension the common dimension of the space groups.
     * @return an iterator over the names of space group settings.
     */
    public static Iterator settingNames(final int dimension) {
        if (groupTables[dimension] == null) {
            parseGroups(tablePath);
        }
    
        return groupTables[dimension].nameToOps.keySet().iterator();
    }

    /**
     * Retrieves the list of all names of groups in the lookup table.
     *
     * @return an iterator over the names of space groups.
     */
    static Iterator groupsInLookup() {
        return lookup.keySet().iterator();
    }

    /**
     * Retrieves information about a given space group setting. The setting is identified
{'fraction_non_alphanumeric': 0.08819190558655635, 'fraction_numerical': 0.005772561092938233, 'mean_word_length': 1.9051611701136575, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2887006', 'n_tokens_mistral': 4015, 'n_tokens_neox': 3849, 'n_words': 1314}