My game server class library-configuration table and its search algorithm

Source: Internet
Author: User
Configuration table

Presumably, all game servers will be used.Configuration tableConfiguration tables may come from files (Excel, XML, JSON, etc.) or databases. However, to avoid frequent access to disk files or databasesMemory. A configuration table generally consists of multiple rows with a unique ID for each row. In the game logic, you can find a configuration based on the ID. Since every game server needs to write similar logic, I abstracted them and added them to my GitHub project.

Configuration item

No more assumptions are made for the configuration table item. Only one of them is required.Unique ID:

public interface Config {    public int getId();}
ConfigbaseIs the abstract Implementation of the config interface to facilitate subclass inheritance:

public abstract class ConfigBase implements Config {    private final int id;    public ConfigBase(int id) {        this.id = id;    }        @Override    public int getId() {        return id;    }    }

Search Algorithm

Sometimes the configuration table may be relatively large (for example, thousands of rows), so it is important to quickly find a configuration by ID.SearchalgorithmAbstract The configuration table search algorithm:

public abstract class SearchAlgorithm {    public abstract <T extends Config> T search(List<T> cfgs, int id);}

Currently, I have implemented three search algorithms: search by index, binary search, and traversal. If the configuration table ID isContinuous (incremental)So the fastest search algorithm isBy IndexSearch:

public final class IndexedSearch extends SearchAlgorithm {    @Override    public <T extends Config> T search(List<T> cfgs, int id) {        int firstId = cfgs.get(0).getId();        int index = id - firstId;        return cfgs.get(index);    }    }
If the ID is not consecutive, but the configuration table Relatively small(For example, if the number is less than 8 ), TraversalThe entire table may be the fastest search algorithm:

public final class TraverseSearch extends SearchAlgorithm {    @Override    public <T extends Config> T search(List<T> cfgs, int id) {        for (T cfg : cfgs) {            if (cfg.getId() == id) {                return cfg;            }        }                throw new ConfigNotFoundException(cfgs, id);    }    }
Otherwise (the configuration table ID must be Increment), Use BipartiteSearch:

Public final class binarysearch extends searchalgorithm {@ override public <t extends config> T search (list <t> cfgs, int ID) {int Index = indexedbinarysearch (cfgs, ID ); if (index> = 0) {return cfgs. get (INDEX);} else {Throw new confignotfoundexception (cfgs, ID);} // The code is from Java. util. collections. copied from indexedbinarysearch () and modified Private Static <t extends config> int indexedbinarysearch (list <t> list, int ID) {int low = 0; int high = List. size ()-1; while (low <= high) {int mid = (low + high) >>> 1; t midval = List. get (MID); int CMP = integer. compare (midval. GETID (), ID); If (CMP <0) Low = Mid + 1; else if (CMP> 0) High = mid-1; else return mid; // key found} return-(low + 1); // key not found }}

Static Method of searchalgorithmSelectalgorithm ()Select the optimal search algorithm based on the above features of the configuration table, and the specific code will not be pasted.

Configlist

ConfiglistEncapsulate the configuration table and search algorithm together:

Public class configlist <t extends config> {private final list <t> cfgs; // size> 0 private final searchalgorithm; Public configlist (list <t> cfgs) {If (cfgs. isempty () {Throw new illegalargumentexception ("Empty List");} cfgs = new arraylist <> (cfgs); sortcfgs (cfgs); checksameid (cfgs ); // ID cannot be repeated this. cfgs = collections. unmodifiablelist (cfgs); searchalgorithm = searchalgorithm. selectalgorithm (this. cfgs );}...}
Getconfigs ()Method returns all configurations, Getconfig (INT)Search for configuration by ID:

    public List<T> getConfigs() {        return cfgs;    }        public T getConfig(int id) {        // todo check id        return searchAlgorithm.search(cfgs, id);    }



My game server class library-configuration table and its search algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.