当前位置:网站首页>SparseArray details
SparseArray details
2022-06-25 09:51:00 【sunny_ yin8899】
- SparseArray Used for mapping integers To object. But unlike ordinary arrays ,sparseArray There are no useless elements between the elements of . In mapping integers To object In the process of ,SparseArray Due to the adoption of the keys And its data structure does not depend on additional Object to store the implementation of the mapping relationship , So it's better than hashMap More efficient use of memory .
- SparseArray Looking for keys Binary search is used in the process of , This implementation is not suitable for a large amount of data . Because the binary search is used in the search , Adding or deleting involves moving other elements of the array ,
- So usually SparseArray than hashMap slow . When processing hundreds of data , This difference in performance is not particularly obvious , The performance difference does not exceed 50%.
- In order to optimize performance ,SparseArray in the light of remove case Optimized ,remove It does not immediately squash the array space , It's marked as delete. This tagged element is either reused , want
- Many times remove Then pass it once gc Squeezed out during operation .gc It needs to be executed before : The array needs to be expanded ;get map size;get values;
- The core put The code is as follows :
- /**
- * Adds a mapping from the specified key to the specified value,
- * replacing the previous mapping from the specified key if there
- * was one.
- */
- public void put(int key, E value) {
- // Let's look in two , Determine the insertion position , To ensure the key The order of arrays
- int i = ContainerHelpers.binarySearch(mKeys, mSize, key);
- if (i >= 0) {
- // eureka , Direct replacement
- mValues[i] = value;
- } else {
- // A little trick , It is related to the return value of binary search
- // If you don't find it i The meaning is i = -insertPoint -1, such as i=-2, be insertPoint=1
- // and -2 What is stored in memory is the complement , It's just right to reverse him insertPoint, It is the same as the above calculation , But bit operation is more efficient
- i = ~i;
- // if i stay size Within the scope of , And the corresponding position is marked as delete 了 , Put it directly in
- if (i < mSize && mValues[i] == DELETED) {
- mKeys[i] = key;
- mValues[i] = value;
- return;
- }
- // If the front if Don't set up , namely i Beyond the size Range , Or the element of the corresponding position is valid
- if (mGarbage && mSize >= mKeys.length) {
- // Compress space
- gc();
- // Find insertion point again
- // Search again because indices may have changed.
- i = ~ContainerHelpers.binarySearch(mKeys, mSize, key);
- }
- // Insert , If there is not enough space, the array will be reallocated
- mKeys = GrowingArrayUtils.insert(mKeys, mSize, i, key);
- mValues = GrowingArrayUtils.insert(mValues, mSize, i, value);
- mSize++;
- }
- }
- }
边栏推荐
- [Ruby on rails full stack course] course directory
- Chitubox micromake l3+ slicing software configuration correspondence
- Oracle function trigger
- 2台三菱PLC走BCNetTCP协议,能否实现网口无线通讯?
- CYCA少儿形体礼仪 乐清市培训成果考核圆满落幕
- 22 mathematical modeling contest 22 contest C
- neo4jDesktop(neo4j桌面版)配置自动启动(开机自启)
- pmp考试题型需要注意哪些?
- The way that flutter makes the keyboard disappear (forwarding from the dependent window)
- Pytorch_Geometric(PyG)使用DataLoader报错RuntimeError: Sizes of tensors must match except in dimension 0.
猜你喜欢

2021mathorcupc topic optimal design of heat dissipation for submarine data center

Summarize two methods of configuring pytorch GPU environment

C语言刷题随记 —— 猴子吃桃

Vscode attempted to write the procedure to a pipeline that does not exist

Wallys/MULTI-FUNCTION IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL

纳米数据世界杯数据接口,中超数据,体育数据比分,世界杯赛程api,足球比赛实时数据接口

Pytorch_ Geometric (pyg) uses dataloader to report an error runtimeerror: sizes of tenants must match except in dimension 0

neo4jDesktop(neo4j桌面版)配置自动启动(开机自启)

Voiceprint Technology (VI): other applications of voiceprint Technology
![[zufe expense reimbursement] zhecai invoice reimbursement specification (taking Xinmiao reimbursement as an example), which can be passed in one trip at most](/img/28/c5c6b6d03b459745dc3735f8b39ea9.jpg)
[zufe expense reimbursement] zhecai invoice reimbursement specification (taking Xinmiao reimbursement as an example), which can be passed in one trip at most
随机推荐
Online notes on Mathematics for postgraduate entrance examination (8): Kego equations, eigenvalues and eigenvectors, similarity matrix, quadratic series courses
vscode试图过程写入管道不存在
Data-driven anomaly detection and early warning of item C in the May 1st mathematical modeling competition in 2021
neo4jDesktop(neo4j桌面版)配置自动启动(开机自启)
[IOU] intersection over union
How do dating applets make millions a year? What is the profit model?
manhattan_slam环境配置
Analysis on the thinking of 2022 meisai C question
Processing picture class library
Ruiji takeout project (II)
富时A50开户什么地方安全
Title B of the certification cup of the pistar cluster in the Ibagu catalog
[project part - structure and content writing of technical scheme] software system type mass entrepreneurship and innovation project plan and Xinmiao guochuang (Dachuang) application
Why should the terminal retail industry choose the member management system
Tiktok brand goes to sea: both exposure and transformation are required. What are the skills of information flow advertising?
Voiceprint Technology (II): Fundamentals of audio signal processing
Rxjs TakeUntil 操作符的学习笔记
manhattan_ Slam environment configuration
Etcd教程 — 第四章 Etcd集群安全配置
Pytorch_Geometric(PyG)使用DataLoader报错RuntimeError: Sizes of tensors must match except in dimension 0.