Hey everyone!
This is my follow up from the last post (Draft Idea), so I have implemented an example container, taking influence and coding methodology from existing implementations from the Cxx public files in the repo, I think as Egor (@egor.zhdan) suggested, one must keep grinding and try to build and work around in their codebase, I have done that, to get a hang, make my ideas and understanding stronger, and possible correct a bunch of more mistakes.
So keeping it simple, I have chosen std::deque
to demonstrate the extension, as it would be little easier to interpret due to existing std::vector
and correct any possible issues, compared to implementing any other container
just for this example, and maybe also refer for upcoming followups:
public protocol CxxArrayDeque<Element> {
associatedtype Element
// Initially I thought of considering 'Int', later realized after
// some research that 'BinaryInteger' is the universal type
// for all integer types like 'Int', 'Int16', etc
associatedtype Size: BinaryInteger
// In my understanding, this is similar to 'std::iterator' of C++,
// making below functions clearly straightforward
associatedtype RawIterator: UnsafeCxxInputIterator
where RawIterator.Pointee: Element
func __findUnsafe(_ index: BinaryInteger) -> RawIterator
func __endUnsafe() -> RawIterator
/// Below functions would probably use the 'RawIterator' to fetch the
/// 'begin()' and 'rbegin()' (possibly '--end()' using overload) similar to C++
func __getFront() -> Element
func __getBack() -> Element
func __removeFront() -> Element
func __removeBack() -> Element
}
extension CxxArrayDeque {
// To get the element at certain 'index' using '[]'
public subscript(index: any BinaryInteger) -> Element? {
get {
let unsafeIter = __findUnsafe(index)
guard unsafeIter != __endUnsafe() else {
return nil
}
return unsafeIter.pointee
}
}
}
I have put some inline comments for review, suggestions and some next steps!
Request your view point, changes and recommendations @egor.zhdan!