HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Solution to challenge of making a list

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
listsolutionmakingchallenge

Problem

This is the answer to this challenge:

Description

Thanks for that list you made me, my thoughts are way more organised!
I've got a few problems though that I thought you might be able to
help with? Sometimes I put the wrong information in a list item. Maybe
to prevent this I'd be able to modify/update the list item? That's not
the only problem though, when there are 50+ items it gets kind of hard
to work my way through. Do you think you could maybe add the ability
to categorise my items? Obviously, if I have that, I'd also like to be
able to view by category! Oh and finally, a few of you were really
great and did this last time but is there a way you can somehow make
my list retain state so that I don't have to re-type it everytime I
turn my computer on again? The newest To-do list should be capable of
the following functionality: Modifying an existing list item Be able
to give a list item a category. The list item should be able to take
an arbitrary amount of categorys View by category - All list items
should be able to be sorted and output by category to make it easier
to wade through submissions Retain state Thanks!
Formal Inputs & Outputs
Output description

Any output that is created should be user-friendly. When I'm viewing
my to-do list, I should be able to easily discern one list item from
another. Examples

(don't take this too literally, do it how you would like to do it)
Categorisation
Input:

addItem('Go to work','Programming'); //Item belongs to the Programming     Category
addItem('Create Sine Waves in C', 'Music', 'Programming); //Belongs to 2  categories, 'Programming' and 'Music');


Category Output
Input:

viewList('programming');
viewList('music');
viewList('music', 'programming');


Output:

` ----PROGRAMMING----
- A pixel is not a pixel is not a pixel
- The Scheme Programming Language
- Memory in C
- Haskell's School of Music
- Algorithmic Symphonies from one line of code

----MUSIC----
  • Modes in Folk Music
  • The use

Solution

Overall performance isn't great. Consider using some hashing or trees. E.g., map categories' names to Category objects either by std::map or std::unordered_map.

Also, it's a good idea to create a separate Item class for holding items and store pointers (e.g. std::shared_ptr) to them in category. In this case you'll have to find it only once during the update. In that case you can maintain circular references (item-to-category) via std::weak_ptr.

In your current implementation find (for finding a value) and find_if (for finding by predicate) from ` can be used instead of manual for-ing. It'll benefit your code a lot, as it's being used in most of your methods.

Minor things:

const auto SIZE = categories.size();
for (auto i = 0; i < SIZE; ++i){


maybe replaced with

for (auto i = 0, size = categories.size(); i < size; ++i) {


upd:

Okay, you've updated your code, but there are some issues left.

-
You can use same names for constructor's arguments and class field names.

Item(std::string dat) : data(dat) {}


Can be replaced with

Item(std::string data) : data(data) {}


-
Return const references, not a copy.

std::string getData() const {
std::string const & getData() const {  // ok


-
Don't use
std::string for items, you have Item class.

-
Use
make_shared.

std::shared_ptr item(new Item(nitem));
std::shared_ptr item = std::make_shared(nitem);  // ok


-
You can use
set here:

//keep only the ones that are the same and then remove every copy so that at the end there's only one
std::vector intermediate;
// ...


So the code below will be way more simpler.

-
Implement
Item's operator operators such as == and != with both Item and std::string. Then you can just use std::find` without the explicit lambda.

More later.

Code Snippets

const auto SIZE = categories.size();
for (auto i = 0; i < SIZE; ++i){
for (auto i = 0, size = categories.size(); i < size; ++i) {
Item(std::string dat) : data(dat) {}
Item(std::string data) : data(data) {}
std::string getData() const {
std::string const & getData() const {  // ok

Context

StackExchange Code Review Q#101139, answer score: 2

Revisions (0)

No revisions yet.