C++ Boost Flyweight














































C++ Boost Flyweight



Executable in C++11/14/17

Description:

Flyweights are small-sized handle classes granting constant access to shared common data,
thus allowing for the management of large amounts of entities within reasonable
memory limits i.e.  it saves the space by sharing the same data which is saved in a single container. 


Boost.Flyweight makes it easy to use this common programming idiom by providing the class
template flyweight<T> , which acts as a drop-in replacement for const T. This
is similar to the concept of pointers except for the fact that flyweights are easier
to understand and implement and it saves the memory.


Header:


#include<boost/function.hpp>


Using Boost Flyweight:


#include <boost/flyweight.hpp>
#include<iostream>
#include <string>
#include <vector>
#include <utility>

using namespace boost::flyweights;
//all classes and functions are in boost::flyweights
using namespace std;
struct person
{

int id;
flyweight<
std::string> city;
person(
int id, string city) : id{id}, city{move(city)}{}
//move() moves the element in the range to a new location
};

int main()
{
vector<person> persons;
for (int i = 0; i < 100; i++)
persons.push_back({i,
"New Delhi"});
// moves the city "New Delhi" to 100 different id's
for(int i=0;i<100;i++)
cout<<persons[i].id<<" "<<persons[i].city<<endl;
}

In the above example all the people live in the same city i.e. New Delhi so we
create one string using boost flyweight and set it equal to New Delhi and use
it whenever we want.


Output:


0  New Delhi

1  New Delhi

2  New Delhi

3  New Delhi

4  New Delhi

5  New Delhi

6  New Delhi

7  New Delhi

8  New Delhi

9  New Delhi

10  New Delhi

11  New Delhi

12  New Delhi

13  New Delhi

14  New Delhi

15  New Delhi

16  New Delhi

17  New Delhi

18  New Delhi

19  New Delhi

20  New Delhi

21  New Delhi

22  New Delhi

23  New Delhi

24  New Delhi

25  New Delhi

26  New Delhi

27  New Delhi

28  New Delhi

29  New Delhi

30  New Delhi

31  New Delhi

32  New Delhi

33  New Delhi

34  New Delhi

35  New Delhi

36  New Delhi

37  New Delhi

38  New Delhi

39  New Delhi

40  New Delhi

41  New Delhi

42  New Delhi

43  New Delhi

44  New Delhi

45  New Delhi

46  New Delhi

47  New Delhi

48  New Delhi

49  New Delhi

50  New Delhi

51  New Delhi

52  New Delhi

53  New Delhi

54  New Delhi

55  New Delhi

56  New Delhi

57  New Delhi

58  New Delhi

59  New Delhi

60  New Delhi

61  New Delhi

62  New Delhi

63  New Delhi

64  New Delhi

65  New Delhi

66  New Delhi

67  New Delhi

68  New Delhi

69  New Delhi

70  New Delhi

71  New Delhi

72  New Delhi

73  New Delhi

74  New Delhi

75  New Delhi

76  New Delhi

77  New Delhi

78  New Delhi

79  New Delhi

80  New Delhi

81  New Delhi

82  New Delhi

83  New Delhi

84  New Delhi

85  New Delhi

86  New Delhi

87  New Delhi

88  New Delhi

89  New Delhi

90  New Delhi

91  New Delhi

92  New Delhi

93  New Delhi

94  New Delhi

95  New Delhi

96  New Delhi

97  New Delhi

98  New Delhi

99  New Delhi





Comments