Basic Dictionary Class!

May 21, 2011 at 4:59pm
closed account (GzwXoG1T)
Introduction:
Hello, this my first post on this forum. So here is my introduction. I have been programming for two years in .NET (Basic/C#), one year in Java and now a couple of months in C++. I'm seventeen turning eighteen in four months. I will be attempting a CS major this upcoming Fall.

General:

I have posted this on another forum but the general C++ population is far lower there. I am a C++ beginner so please help me learn by pointing out my errors, bad habits and anything I can do to improve.

Purpose:
I made a command interpreter about one month ago. It was able to print text, files and system variables. I decided to add my own string type variables and doing so I learn of the map class and how it seemed painful to use (for me at least). I had used the Dictionary class in the .NET Framework and decided to write my own similar version in C++.

Preview Image:
http://gyazo.com/e91204f404834efa13b12c090e0fe343.png

Source:
http://pastebin.com/LtakpxPK
Last edited on May 22, 2011 at 1:07am
May 21, 2011 at 7:17pm
1
2
3
4
5
6
7
8
int Dictionary::getIndex(std::string key)
{
        for (int index = 0; index < this->count; index++)
        {
                if (entries[index].key == key)
                        return index;
        } return -1;
}
I've seen enough. You wrote an algorithmically inefficient version of a class that's already provided by the standard library. If all you wanted was to avoid std::map's interface, you could have written a thin wrapper class. That would have been much better and faster and would have taken you a lot less time to write.
Scrap this and never use it again.

Also, this doesn't belong on this part of the forum.
May 21, 2011 at 8:17pm
Terribly blunt, but true unfortunately.
May 21, 2011 at 10:42pm
I don't think he knew about the standard library alternative.
May 21, 2011 at 11:36pm
closed account (GzwXoG1T)
I wouldn't say this is inefficient however I wanted to make my own Dictionary and not use an existing class. This was my experimentation with pointers and classes alike.
May 21, 2011 at 11:44pm
I wouldn't say this is inefficient
std::map takes O(log n) time for look up. Yours takes O(n) time.
std::map takes O(log n) time for insertion. Yours takes O(n2) time if the key doesn't already exist.
If that's not inefficient, then I don't know what is.
Last edited on May 21, 2011 at 11:45pm
May 22, 2011 at 12:43am
If you were just experimenting, why did you post it as an article? Posting something in the articles section implies that it is intended for actual use...
May 22, 2011 at 12:48am
Veltas wrote:
I don't think he knew about the standard library alternative.

Well...

tylerflo wrote:
I learn of the map class and how it seemed painful to use (for me at least).

I wouldn't say it's bad as a first attempt to write an associative container.
It does provide the basic functionality one would want in such a class.
It's not as efficient as std::map, true, but the OP's target wasn't efficiency.

tylerflo wrote:
I wanted to make my own Dictionary and not use an existing class.
This was my experimentation with pointers and classes alike.

@ OP:

Consider writing a hashmap as the next step. There is no hashmap
in the current standard and most of the ones provided by libraries
don't allow control on the collision resolution mechanism.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <map>

template <class KeyType, class ValueType, class HF, class SF>
struct StdMapCR
{
    std::vector<std::map<KeyType,ValueType, typename SF::Less> > data;

    bool Get(KeyType key, ValueType & val) { /*...*/ }
    void Put(KeyType key, ValueType val) { /*...*/ }
    void Resize(unsigned long new_size) { /*...*/ }

    //...
};

template <class KeyType, class ValueType, class HF, class SF>
struct StdListCR
{
    std::vector<std::list<std::pair<KeyType, ValueType> > > data;

    bool Get(KeyType key, ValueType & val) { /*...*/ }
    void Put(KeyType key, ValueType val) { /*...*/ }
    void Resize(unsigned long new_size) { /*...*/ }

    //...
};

template <class KeyType> struct HashFunction;

template<>
struct HashFunction<std::string>
{
    struct Hash
    {
        unsigned long operator()(const std::string & key, unsigned long size) { /*...*/ }
    };
};

template <class KeyType>
struct SearchFunctions
{
    struct Equal { bool operator()(KeyTypev1, KeyTypev2) { return v1==v2; } };
    struct Less { bool operator()(KeyTypev1, KeyTypev2) { return v1<v2; } };
};

template <
    class KeyType, class ValueType,
    template <class, class, class, class> class CRPolicy = StdListCR,
    template <class> class HF = HashFunction,
    template <class> class SF = SearchFunctions
    >
class HashMap : public CRPolicy<KeyType, ValueType, HF<KeyType>, SF<KeyType> >
{
    //...
};

//====================================================================================

struct Point3D { int x, y, z; };

template <>
struct HashFunction<Point3D>
{
    struct Hash
    {
        unsigned long operator()(const Point3D & key, unsigned long size) { /*...*/ }
    };
};

template <>
struct SearchFunctions<Point3D>
{
    struct Equal { bool operator()(const Point3D & v1, const Point3D & v2) { /*...*/ } };
    struct Less { bool operator()(const Point3D & v1, const Point3D & v2) { /*...*/ } };
};

//====================================================================================

int main()
{
    HashMap <std::string, int,    StdListCR> hmap1;
    HashMap <Point3D,     double, StdMapCR > hmap2;

    //...

    return 0;
}

EDIT:

chrisname wrote:
If you were just experimenting, why did you post it as an article? Posting
something in the articles section implies that it is intended for actual use...

That's also true. I'm not sure, but I think the OP is able to move
this thread to another section of the forum (the Lounge perhaps).
Last edited on May 23, 2011 at 12:14am
May 22, 2011 at 1:08am
closed account (GzwXoG1T)

Sorry I am new to the forum, moved to the general C++ section.

@chrisname - Because I was experimenting with classes and pointers in my personal project and it worked perfectly for me.

@m4ster r0shi - Thanks for posting something positive. I will consider writing that once I progress further into the language.

Update, saw a thread that inspired me to add operator overloading to allow you to get a value by specifying an index.

Preview:
http://gyazo.com/393549230de499dceb3ccb04f5a7148b.png
Last edited on May 22, 2011 at 1:41am
May 22, 2011 at 12:17pm
Whoops, thought he was talking about a .NET or Java map class.

Yeah, General C++ is a much more appropriate section.

EDIT:
I actually think this attempt is a sign of a promising programmer. Trying out ideas like this is a great way to learn C++, or anything really. Hopefully tylerflo will learn from the harsh criticism, but I think you guys were coming on too hard and it's easier to take criticism that isn't all in your face.

Don't chuck your code away tylerflo, but if you're going to use it later on in your C++ development then you'll probably see the faults in it straight away for yourself anyway, so you'll know what to improve (or rewrite, I've rewritten countless old programs when I've come to borrow them for other things).
Last edited on May 22, 2011 at 12:26pm
May 22, 2011 at 4:26pm
closed account (GzwXoG1T)
Thanks @Veltas, like I said I wanted to accomplish three things in this project.

1. Create a simple, user-friendly, Dictionary.
2. Experiment with pointers correctly (I think I used them properly).
3. Experiment with classes (and I guess this was also my first time using structs).

I plan on sticking around here as I go through my CS courses in university, so hopefully you guys can see me progress into the language!
Last edited on May 22, 2011 at 4:29pm
Topic archived. No new replies allowed.