Browse Source

iterators and vector start

master
narnaud 2 years ago
parent
commit
f7fa758fb1
  1. 192
      .clang-format
  2. 16
      Makefile
  3. 199
      Session.vim
  4. 19
      compile_commands.json
  5. 5
      includes/ft_container.hpp
  6. 197
      includes/iterator.hpp
  7. 125
      includes/vector.hpp
  8. 5
      tester.cpp

192
.clang-format

@ -0,0 +1,192 @@
---
Language: Cpp
# BasedOnStyle: LLVM
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignArrayOfStructures: None
AlignConsecutiveMacros: None
AlignConsecutiveAssignments: None
AlignConsecutiveBitFields: None
AlignConsecutiveDeclarations: Consecutive
AlignEscapedNewlines: Right
AlignOperands: Align
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortEnumsOnASingleLine: true
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: MultiLine
AttributeMacros:
- __capability
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeConceptDeclarations: true
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
QualifierAlignment: Leave
CompactNamespaces: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DeriveLineEnding: true
DerivePointerAlignment: false
DisableFormat: false
EmptyLineAfterAccessModifier: Never
EmptyLineBeforeAccessModifier: LogicalBlock
ExperimentalAutoDetectBinPacking: false
PackConstructorInitializers: BinPack
BasedOnStyle: ''
ConstructorInitializerAllOnOneLineOrOnePerLine: false
AllowAllConstructorInitializersOnNextLine: true
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IfMacros:
- KJ_IF_MAYBE
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
SortPriority: 0
CaseSensitive: false
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
SortPriority: 0
CaseSensitive: false
- Regex: '.*'
Priority: 1
SortPriority: 0
CaseSensitive: false
IncludeIsMainRegex: '(Test)?$'
IncludeIsMainSourceRegex: ''
IndentAccessModifiers: false
IndentCaseLabels: false
IndentCaseBlocks: false
IndentGotoLabels: true
IndentPPDirectives: None
IndentExternBlock: AfterExternBlock
IndentRequires: false
IndentWidth: 2
IndentWrappedFunctionNames: false
InsertTrailingCommas: None
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
LambdaBodyIndentation: Signature
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 2
ObjCBreakBeforeNestedBlockParam: true
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakOpenParenthesis: 0
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PenaltyIndentedWhitespace: 0
PointerAlignment: Right
PPIndentWidth: -1
ReferenceAlignment: Pointer
ReflowComments: true
RemoveBracesLLVM: false
SeparateDefinitionBlocks: Leave
ShortNamespaceLines: 1
SortIncludes: CaseSensitive
SortJavaStaticImport: Before
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeParensOptions:
AfterControlStatements: true
AfterForeachMacros: true
AfterFunctionDefinitionName: false
AfterFunctionDeclarationName: false
AfterIfMacros: true
AfterOverloadedOperator: false
BeforeNonEmptyParentheses: false
SpaceAroundPointerQualifiers: Default
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: true
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInLineCommentPrefix:
Minimum: 1
Maximum: -1
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
BitFieldColonSpacing: Both
Standard: Latest
StatementAttributeLikeMacros:
- Q_EMIT
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 8
UseCRLF: false
UseTab: Never
WhitespaceSensitiveMacros:
- STRINGIZE
- PP_STRINGIZE
- BOOST_PP_STRINGIZE
- NS_SWIFT_NAME
- CF_SWIFT_NAME
...

16
Makefile

@ -0,0 +1,16 @@
CXXFLAGS= -std=c++98 -Werror -Wextra -Wall -Iincludes
all: tester
tester: tester.o
c++ tester.o -o tester
clean:
rm -rf tester.o
fclean: clean
rm -rf tester
re: fclean re
.PHONY: all clean fclean re

199
Session.vim

@ -0,0 +1,199 @@
let SessionLoad = 1
let s:so_save = &g:so | let s:siso_save = &g:siso | setg so=0 siso=0 | setl so=-1 siso=-1
let v:this_session=expand("<sfile>:p")
silent only
silent tabonly
cd ~/Development/ft_container
if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''
let s:wipebuf = bufnr('%')
endif
let s:shortmess_save = &shortmess
if &shortmess =~ 'A'
set shortmess=aoOA
else
set shortmess=aoO
endif
badd +1 Makefile
badd +1 tester.cpp
badd +121 includes/vector.hpp
badd +1 main.cpp
badd +1 includes/ft_container.hpp
badd +193 includes/iterator.hpp
argglobal
%argdel
$argadd main.cpp
tabnew +setlocal\ bufhidden=wipe
tabrewind
edit Makefile
let s:save_splitbelow = &splitbelow
let s:save_splitright = &splitright
set splitbelow splitright
wincmd _ | wincmd |
vsplit
wincmd _ | wincmd |
vsplit
2wincmd h
wincmd w
wincmd w
let &splitbelow = s:save_splitbelow
let &splitright = s:save_splitright
wincmd t
let s:save_winminheight = &winminheight
let s:save_winminwidth = &winminwidth
set winminheight=0
set winheight=1
set winminwidth=0
set winwidth=1
exe 'vert 1resize ' . ((&columns * 84 + 127) / 254)
exe 'vert 2resize ' . ((&columns * 84 + 127) / 254)
exe 'vert 3resize ' . ((&columns * 84 + 127) / 254)
argglobal
balt includes/ft_container.hpp
setlocal fdm=manual
setlocal fde=
setlocal fmr={{{,}}}
setlocal fdi=#
setlocal fdl=0
setlocal fml=1
setlocal fdn=20
setlocal fen
silent! normal! zE
let &fdl = &fdl
let s:l = 1 - ((0 * winheight(0) + 32) / 65)
if s:l < 1 | let s:l = 1 | endif
keepjumps exe s:l
normal! zt
keepjumps 1
normal! 0
wincmd w
argglobal
if bufexists(fnamemodify("includes/ft_container.hpp", ":p")) | buffer includes/ft_container.hpp | else | edit includes/ft_container.hpp | endif
if &buftype ==# 'terminal'
silent file includes/ft_container.hpp
endif
balt Makefile
setlocal fdm=manual
setlocal fde=
setlocal fmr={{{,}}}
setlocal fdi=#
setlocal fdl=0
setlocal fml=1
setlocal fdn=20
setlocal fen
silent! normal! zE
let &fdl = &fdl
let s:l = 4 - ((3 * winheight(0) + 32) / 65)
if s:l < 1 | let s:l = 1 | endif
keepjumps exe s:l
normal! zt
keepjumps 4
normal! 022|
wincmd w
argglobal
if bufexists(fnamemodify("tester.cpp", ":p")) | buffer tester.cpp | else | edit tester.cpp | endif
if &buftype ==# 'terminal'
silent file tester.cpp
endif
balt Makefile
setlocal fdm=manual
setlocal fde=
setlocal fmr={{{,}}}
setlocal fdi=#
setlocal fdl=0
setlocal fml=1
setlocal fdn=20
setlocal fen
silent! normal! zE
let &fdl = &fdl
let s:l = 3 - ((2 * winheight(0) + 32) / 65)
if s:l < 1 | let s:l = 1 | endif
keepjumps exe s:l
normal! zt
keepjumps 3
normal! 0
wincmd w
exe 'vert 1resize ' . ((&columns * 84 + 127) / 254)
exe 'vert 2resize ' . ((&columns * 84 + 127) / 254)
exe 'vert 3resize ' . ((&columns * 84 + 127) / 254)
tabnext
edit includes/iterator.hpp
let s:save_splitbelow = &splitbelow
let s:save_splitright = &splitright
set splitbelow splitright
wincmd _ | wincmd |
vsplit
1wincmd h
wincmd w
let &splitbelow = s:save_splitbelow
let &splitright = s:save_splitright
wincmd t
let s:save_winminheight = &winminheight
let s:save_winminwidth = &winminwidth
set winminheight=0
set winheight=1
set winminwidth=0
set winwidth=1
exe 'vert 1resize ' . ((&columns * 126 + 127) / 254)
exe 'vert 2resize ' . ((&columns * 127 + 127) / 254)
argglobal
balt includes/vector.hpp
setlocal fdm=manual
setlocal fde=
setlocal fmr={{{,}}}
setlocal fdi=#
setlocal fdl=0
setlocal fml=1
setlocal fdn=20
setlocal fen
silent! normal! zE
let &fdl = &fdl
let s:l = 67 - ((8 * winheight(0) + 32) / 65)
if s:l < 1 | let s:l = 1 | endif
keepjumps exe s:l
normal! zt
keepjumps 67
normal! 03|
wincmd w
argglobal
if bufexists(fnamemodify("includes/vector.hpp", ":p")) | buffer includes/vector.hpp | else | edit includes/vector.hpp | endif
if &buftype ==# 'terminal'
silent file includes/vector.hpp
endif
setlocal fdm=manual
setlocal fde=
setlocal fmr={{{,}}}
setlocal fdi=#
setlocal fdl=0
setlocal fml=1
setlocal fdn=20
setlocal fen
silent! normal! zE
let &fdl = &fdl
let s:l = 7 - ((6 * winheight(0) + 32) / 65)
if s:l < 1 | let s:l = 1 | endif
keepjumps exe s:l
normal! zt
keepjumps 7
normal! 02|
wincmd w
2wincmd w
exe 'vert 1resize ' . ((&columns * 126 + 127) / 254)
exe 'vert 2resize ' . ((&columns * 127 + 127) / 254)
tabnext 2
if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0 && getbufvar(s:wipebuf, '&buftype') isnot# 'terminal'
silent exe 'bwipe ' . s:wipebuf
endif
unlet! s:wipebuf
set winheight=1 winwidth=20
let &shortmess = s:shortmess_save
let &winminheight = s:save_winminheight
let &winminwidth = s:save_winminwidth
let s:sx = expand("<sfile>:p:r")."x.vim"
if filereadable(s:sx)
exe "source " . fnameescape(s:sx)
endif
let &g:so = s:so_save | let &g:siso = s:siso_save
set hlsearch
doautoall SessionLoadPost
unlet SessionLoad
" vim: set ft=vim :

19
compile_commands.json

@ -0,0 +1,19 @@
[
{
"arguments": [
"/usr/bin/g++",
"-std=c++98",
"-Werror",
"-Wextra",
"-Wall",
"-Iincludes",
"-c",
"-o",
"tester.o",
"tester.cpp"
],
"directory": "/home/narnaud/Development/ft_container",
"file": "/home/narnaud/Development/ft_container/tester.cpp",
"output": "/home/narnaud/Development/ft_container/tester.o"
}
]

5
includes/ft_container.hpp

@ -0,0 +1,5 @@
#pragma once
#include "vector.hpp"
#include "iterator.hpp"

197
includes/iterator.hpp

@ -0,0 +1,197 @@
#pragma once
#include <cstddef>
namespace ft {
struct input_iterator_tag {};
struct output_iterator_tag {};
struct forward_iterator_tag : public input_iterator_tag {};
struct bidirectional_iterator_tag : public forward_iterator_tag {};
struct random_access_iterator_tag : public bidirectional_iterator_tag {};
template < class Iter > struct iterator_traits {
typedef class Iter::iterator_category iterator_category;
typedef class Iter::value_type value_type;
typedef class Iter::difference_type difference_type;
typedef class Iter::pointer pointer;
typedef class Iter::reference reference;
};
template < class T > struct iterator_traits< T * > {
public:
typedef random_access_iterator_tag iterator_category;
typedef T value_type;
typedef std::ptrdiff_t difference_type;
typedef T *pointer;
typedef T &reference;
};
template < class T > struct iterator_traits< const T * > {
typedef random_access_iterator_tag iterator_category;
typedef T value_type;
typedef std::ptrdiff_t difference_type;
typedef const T *pointer;
typedef const T &reference;
};
// Iterator classes:
template < class T > class ra_iterator {
typedef random_access_iterator_tag iterator_category;
typedef T value_type;
typedef std::ptrdiff_t difference_type;
typedef T *pointer;
typedef T &reference;
T current;
public:
ra_iterator(T value = NULL) : current(value) {}
ra_iterator(const ra_iterator &iter) : current(iter.base()) {}
ra_iterator &operator=(const ra_iterator< T > &iter) {
current = iter.base();
return (*this);
}
T base(void) const { return current; }
reference operator*(void) const { return *current; }
pointer operator->(void) const { return current; }
reference operator[](const difference_type n) const { return *(current + n); }
ra_iterator operator+(const difference_type n) const {
return (iterator(current + n));
}
ra_iterator &operator++() {
++current;
return *this;
}
ra_iterator operator++(int) {
ra_iterator tmp(*this);
++(*this);
return tmp;
}
ra_iterator &operator+=(const difference_type other) {
current += other;
return *this;
}
ra_iterator operator-(const difference_type n) const {
return (iterator(current - n));
}
ra_iterator &operator--() {
--current;
return *this;
}
ra_iterator operator--(int) {
ra_iterator tmp(*this);
--(*this);
return tmp;
}
ra_iterator &operator-=(const difference_type other) {
current -= other;
return *this;
}
bool operator==(const ra_iterator &right) const {
return current == right.base();
}
bool operator!=(const ra_iterator &right) const {
return current != right.base();
}
bool operator<(const ra_iterator &right) const {
return current < right.base();
}
bool operator>(const ra_iterator &right) const {
return current > right.base();
}
bool operator<=(const ra_iterator &right) const {
return current <= right.base();
}
bool operator>=(const ra_iterator &right) const {
return current >= right.base();
}
};
template < class T > class rev_iterator {
T current;
typedef random_access_iterator_tag iterator_category;
typedef T value_type;
typedef std::ptrdiff_t difference_type;
typedef T *pointer;
typedef T &reference;
public:
rev_iterator(T value = NULL) : current(value) {}
rev_iterator(const rev_iterator &iter) : current(iter.base()) {}
rev_iterator &operator=(const rev_iterator< T > &iter) {
current = iter.base();
return (*this);
}
T base(void) const { return current; }
reference operator*(void) const { return *(current - 1); }
pointer operator->(void) const { return current - 1; }
reference operator[](const difference_type n) const { return *(current - n); }
rev_iterator operator+(const difference_type n) const {
return (rev_iterator(current - n));
}
rev_iterator &operator++() {
--current;
return *this;
}
rev_iterator operator++(int) const {
rev_iterator tmp(*this);
--(*this);
return tmp;
}
rev_iterator &operator+=(const difference_type n) {
current -= n;
return *this;
}
rev_iterator operator-(const difference_type n) const {
return (rev_iterator(current + n));
}
rev_iterator &operator--() {
++current;
return *this;
}
rev_iterator operator--(int) {
rev_iterator tmp(*this);
++(*this);
return tmp;
}
rev_iterator &operator-=(const difference_type n) {
current += n;
return *this;
}
bool operator==(const rev_iterator< T > &right) const {
return current == right.base();
}
bool operator!=(const rev_iterator< T > &right) const {
return current != right.base();
}
bool operator<(const rev_iterator< T > &right) const {
return current >= right.base();
}
bool operator>(const rev_iterator< T > &right) const {
return current <= right.base();
}
bool operator<=(const rev_iterator< T > &right) const {
return current > right.base();
}
bool operator>=(const rev_iterator< T > &right) const {
return current < right.base();
}
};
template < class It > std::size_t distance(It first, It last) {
size_t ret = 0;
while (first != last && ret++)
first++;
return ret;
}
} // namespace ft

125
includes/vector.hpp

@ -0,0 +1,125 @@
#pragma once
#include "iterator.hpp"
#include <memory>
namespace ft {
template < class T, class Allocator = std::allocator< T > > class vector {
public:
typedef T value_type;
typedef Allocator allocator_type;
typedef class Allocator::pointer pointer;
typedef class Allocator::const_pointer cst_pointer;
typedef value_type &reference;
typedef const value_type &cst_reference;
typedef ft::ra_iterator< pointer > iterator;
typedef ft::ra_iterator< cst_pointer > cst_iterator;
typedef ft::rev_iterator< iterator > reverse_iterator;
typedef ft::rev_iterator< cst_iterator > cst_reverse_iterator;
typedef std::ptrdiff_t difference_type;
typedef std::size_t size_type;
private:
Allocator _alloc;
pointer _begin;
size_type _size;
size_type _capacity;
public:
vector(void) : _alloc(Allocator()), _begin(NULL), _size(0), _capacity(0) {}
explicit vector(const Allocator &alloc)
: _alloc(alloc), _begin(NULL), _size(0), _capacity(0){};
explicit vector(size_type count, const T &value = T(),
const Allocator &alloc = Allocator())
: _alloc(alloc), _size(count), _capacity(count) {
_begin = _alloc.allocate(count);
for (size_type i = 0; i < count; i++)
alloc.construct(_begin + i, value);
}
template < class It >
vector(It first, It last, const Allocator &alloc = Allocator())
: _alloc(alloc) {
_size = distance(first, last);
_capacity = _size;
_begin = _alloc.allocate(_size);
for (size_type i = 0; i < _size; i++)
_alloc.construct(_begin + i, *(first + i));
}
vector(const vector &other)
: _alloc(other._alloc), _size(other._size), _capacity(other._capacity) {
_begin = _alloc.allocate(_capacity);
for (size_type i = 0; i < _size; i++)
_alloc.construct(_begin + i, *(other._begin + i));
}
~vector(void) {
clear();
if (_capacity)
_alloc.deallocate(_begin, _capacity);
}
vector &operator=(const vector &other);
void assign(size_type count, const T &value);
template < class It > void assign(It first, It last);
allocator_type get_allocator(void) const { return _alloc; }
// ACCESS:
reference at(size_type pos);
cst_reference at(size_type pos) const;
reference operator[](size_type pos);
cst_reference operator[](size_type pos) const;
reference front(void);
cst_reference front(void) const;
reference back(void);
cst_reference back(void) const;
T *data(void);
const T *data(void) const;
// ITERATORS:
ft::ra_iterator< reference > begin(void);
ft::ra_iterator< cst_reference > begin(void) const;
ft::ra_iterator< reference > end(void);
ft::ra_iterator< cst_reference > end(void) const;
ft::rev_iterator< iterator > rbegin(void);
ft::rev_iterator< cst_iterator > rbegin(void) const;
ft::rev_iterator< iterator > rend(void);
ft::rev_iterator< cst_iterator > rend(void) const;
// CAPACITY:
bool empty(void) const;
size_type size(void) const;
size_type max_size(void) const;
void reserve(size_type new_cap);
size_type capacity(void) const;
// MODIFIERS:
void clear(void) {
for (size_type i = 0; i < _size; i++)
_alloc.destroy(_begin + i);
_size = 0;
}
iterator insert(cst_iterator pos, const T &value);
iterator insert(cst_iterator pos, size_type count, const T &value);
template < class It > iterator insert(cst_iterator pos, It first, It last);
iterator erase(iterator pos);
iterator erase(iterator first, iterator last);
void push_back(const T& value);
void pop_back(void);
void resize(size_type count);
void resize(size_type count, T value = T());
void swap( vector& other);
};
} // namespace ft

5
tester.cpp

@ -0,0 +1,5 @@
#include "ft_container.hpp"
int main(void) {
}
Loading…
Cancel
Save