Skip to content

Commit

Permalink
lib improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
joe-hauns committed Jun 28, 2023
1 parent 64bf49f commit 874d443
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ cmake-build
# vim swap files
**/.*.swp
**/.*.swo
.ccls-cache
.cache/
2 changes: 2 additions & 0 deletions Lib/DArray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ class DArray
} // ensure


void reset() { ensure(0); }

/**
* Set array's size to @b s and that its capacity is at least @b s.
* If the capacity is smaller, the array will expand, and all old
Expand Down
22 changes: 22 additions & 0 deletions Lib/Map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ class Map
value().~Val();
}
}
void reset() {
if (occupied()) {
key().~Key();
value().~Val();
}
code = 0;
}

private:
/** Create a new entry */
Expand Down Expand Up @@ -515,6 +522,7 @@ class Map
return true;
}


void clear()
{
if (_entries) {
Expand All @@ -528,6 +536,20 @@ class Map
_maxEntries = 0;
}

/**
* resets every entry in the map keeping the memory of _entries allocated
*/
void reset()
{
CALL("Map::deleteAll");

for (int i = _capacity-1;i >= 0;i--) {
_entries[i].reset();
}
_noOfEntries = 0;
} // reset


/**
* Delete all entries.
* @since 07/08/2005 Redmond
Expand Down
12 changes: 6 additions & 6 deletions Lib/Metaiterators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2125,16 +2125,16 @@ auto dropElementType(Iterator iter)
{ return iterTraits(std::move(iter)).map([](auto _) { return make_tuple(); }); }

template<class Array>
auto arrayIter(Array const& x)
{ return iterTraits(getArrayishObjectIterator<const_ref_t>(x)); }
auto arrayIter(Array const& a)
{ return range(0, a.size()).map([&](auto i) -> decltype(auto) { return a[i]; }); }

template<class Array>
auto arrayIter(Array & x)
{ return iterTraits(getArrayishObjectIterator<mut_ref_t>(x)); }
auto arrayIter(Array & a)
{ return range(0, a.size()).map([&](auto i) -> decltype(auto) { return a[i]; }); }

template<class Array>
auto arrayIter(Array && x)
{ return iterTraits(ownedArrayishIterator(std::move(x))); }
auto arrayIter(Array && a)
{ return range(0, a.size()).map([a = std::move(a)](auto i) { return std::move(a[i]); }); }

template<class Iter>
class BoxedIter {
Expand Down
17 changes: 17 additions & 0 deletions Lib/Recycled.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ class Recycled
T& operator* () { return self(); }
T* operator->() { return &self(); }

auto size() const { return self().size(); }
template<class Idx> auto operator[](Idx idx) const { return self()[idx]; }
template<class Idx> auto operator[](Idx idx) { return self()[idx]; }

friend std::ostream& operator<<(std::ostream& out, Recycled const& self)
{ if (self.alive())return out << self.self(); else return out << "Recycled(NULL)"; }
};
Expand All @@ -202,4 +206,17 @@ bool Recycled<T, Reset, Keep>::memAlive = true;

};

template<class T>
Recycled<Stack<T>> recycledStack()
{ return Recycled<Stack<T>>(); }


template<class T, class... Ts>
Recycled<Stack<T>> recycledStack(T t, Ts... ts)
{
Recycled<Stack<T>> out;
out->pushMany(std::move(t), std::move(ts)...);
return out;
}

#endif /*__Recycled__*/

0 comments on commit 874d443

Please sign in to comment.