https://github.com/arun11299/slices-in-cpp
This is not a complete library in any way. It exists only to spread out the idea.
https://github.com/arun11299/slices-in-cpp
Last synced: 4 months ago
JSON representation
This is not a complete library in any way. It exists only to spread out the idea.
- Host: GitHub
- URL: https://github.com/arun11299/slices-in-cpp
- Owner: arun11299
- Created: 2015-09-20T05:25:21.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-11-21T19:11:04.000Z (over 9 years ago)
- Last Synced: 2024-12-27T11:14:28.501Z (5 months ago)
- Language: C++
- Size: 33.2 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Slices-in-Cpp
This is not a complete library in any way. It exists only to spread out the idea.Idea ?
-------
From Golang slices. The 'indexing' expression itself be used
to provide the range over which the slice is operating.How this works in Go ?
-----------------------
Go maintains a lightweight header structure which carries some basic information
like the start of the underlying container, its length, its capacity etc.
This information is easier to pass by copy, as the underlying container
does not get copied.This makes it possible to work on 2 different sections of the array/underlying
container or rather makes it easier to work with subsections of the container
at different places at the same time.Why required in C++ ?
-----------------------
Currently, iteration in C++ is supported by 'iterators'.
Iterators abstract the underlying datastructure and provides a unified
interface to walk over the structure.
But with the available set of API's there is more typing involved
with achieving things like slicing of vectors/list/array.Usage
-----
std::vector vec {1,2,3,4,5,6};
auto vec_s = make_slice(vec); // vec_s now knows about the complete range of vec
vec_s = make_slice(vec, 5); // vec_s knows only about 0 to 4 elements
vec_s = make_slice(vec, 2, 5); // vec_s knows about 2nd to 4th element inclusiveauto new_s = make_slice(vec);
auto copy_new_s = new_s(start, 3);
copy_new_s = new_s(4,end);