https://github.com/o-rumiantsev/array
Compromise between the convenient and fast implementation of arrays in C++
https://github.com/o-rumiantsev/array
array cpp features functionality list speed
Last synced: 3 months ago
JSON representation
Compromise between the convenient and fast implementation of arrays in C++
- Host: GitHub
- URL: https://github.com/o-rumiantsev/array
- Owner: o-rumiantsev
- License: mit
- Created: 2018-04-15T13:24:24.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-04-17T09:10:20.000Z (about 8 years ago)
- Last Synced: 2025-04-06T11:50:01.319Z (about 1 year ago)
- Topics: array, cpp, features, functionality, list, speed
- Language: C++
- Homepage:
- Size: 16.6 KB
- Stars: 2
- Watchers: 0
- Forks: 1
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Array
This is an attempt to improve arrays in C++. Not only make it look like JS arrays, but partly save its advantage - direct access to its elements.
# Mechanism
`Array` class wraps a singly linked list, which nodes has two fields: `slice` and `next`. `slice` is a C/C++ array.
The constructor of an `Array` takes one argument - `size`. This is the size of one slice, which contains in one list node.
When user want to use more memory, there will be allocated one more array and push it to the list.
```
Array class
List o —---------------> o —-----------> o
| | |
Memory [ ... | |x|x|x|x| | | ... | |x|x|x|x| | ... |x|x|x|x| | ... ]
slice slice slice
```
So here is an example of how it works:
```javascript
Array arr (10); // Initialize an array with size 10
// Internal list has one node.
// do something
...
// 10 elements became not enough
arr[10] = some_value; // There has allocated one more array on 10 elements inside
// Internal list has two nodes now.
```
# Features
### `push()`
```javascript
// arr [1, 2, 3, 4, 5, 6, 7, 8, 9]
arr.push(10);
// arr [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
```
### `pop()`
```javascript
// arr [1, 2, 3, 4]
int last = arr.pop();
// last = 4, arr [1, 2, 3]
```
### `map()`
```javascript
// arr [1, 2, 3, 4]
Array double_arr = arr.map([](int item) { return item * 2; });
// double_arr [2, 4, 6, 8], arr [1, 2, 3, 4]
```
### `filter()`
```javascript
// arr [1, 2, 3, 4]
Array filtered = arr.filter([](int item) { return item % 2 == 0; });
// filtered [2, 4], arr [1, 2, 3, 4]
```