Skip to content

Commit

Permalink
Add Fenwick tree
Browse files Browse the repository at this point in the history
  • Loading branch information
GauthamBanasandra committed Jan 8, 2018
1 parent 28a377e commit 244fd39
Show file tree
Hide file tree
Showing 9 changed files with 447 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,5 @@
/DSLib/Test/SegmentTree/Debug
/DSLib/Deps
/DSLib/out
/DSLib/Client/FenwickTree/Debug
/DSLib/Lib/FenwickTree/Debug
16 changes: 16 additions & 0 deletions DSLib/Client/FenwickTree/FenwickTree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "FenwickTree.h"

#include <iostream>

int main(int argc, char* argv[])
{
std::vector<std::size_t> data = { 1, 2, 3, 4, 5, 6, 7 };
ds::fen_tree f(data.size());
for (std::size_t i = 0; i < data.size(); ++i)
{
f.update_point(i + 1, data[i]);
}

std::cout << f.sum(data.size()) << std::endl;
return 0;
}
151 changes: 151 additions & 0 deletions DSLib/Client/FenwickTree/FenwickTree.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>15.0</VCProjectVersion>
<ProjectGuid>{37248D29-4153-4C53-9BB8-DD16AB36C257}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>FenwickTree</RootNamespace>
<WindowsTargetPlatformVersion>10.0.16299.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>"$(SolutionDir)Lib\FenwickTree"</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>"$(SolutionDir)Lib\FenwickTree"</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>"$(SolutionDir)Lib\FenwickTree"</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>"$(SolutionDir)Lib\FenwickTree"</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="FenwickTree.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
22 changes: 22 additions & 0 deletions DSLib/Client/FenwickTree/FenwickTree.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="FenwickTree.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
22 changes: 22 additions & 0 deletions DSLib/DSLib.sln
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SegmentTree", "Client\Segme
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SegmentTree", "Test\SegmentTree\SegmentTree.vcxproj", "{A740815A-4E10-4116-A01E-73D71286FBA6}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FenwickTree", "Lib\FenwickTree\FenwickTree.vcxproj", "{26B24EFB-1E93-4AC1-AB9C-D80C9FA76D0F}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FenwickTree", "Client\FenwickTree\FenwickTree.vcxproj", "{37248D29-4153-4C53-9BB8-DD16AB36C257}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Expand Down Expand Up @@ -179,6 +183,22 @@ Global
{A740815A-4E10-4116-A01E-73D71286FBA6}.Release|x64.Build.0 = Release|x64
{A740815A-4E10-4116-A01E-73D71286FBA6}.Release|x86.ActiveCfg = Release|Win32
{A740815A-4E10-4116-A01E-73D71286FBA6}.Release|x86.Build.0 = Release|Win32
{26B24EFB-1E93-4AC1-AB9C-D80C9FA76D0F}.Debug|x64.ActiveCfg = Debug|x64
{26B24EFB-1E93-4AC1-AB9C-D80C9FA76D0F}.Debug|x64.Build.0 = Debug|x64
{26B24EFB-1E93-4AC1-AB9C-D80C9FA76D0F}.Debug|x86.ActiveCfg = Debug|Win32
{26B24EFB-1E93-4AC1-AB9C-D80C9FA76D0F}.Debug|x86.Build.0 = Debug|Win32
{26B24EFB-1E93-4AC1-AB9C-D80C9FA76D0F}.Release|x64.ActiveCfg = Release|x64
{26B24EFB-1E93-4AC1-AB9C-D80C9FA76D0F}.Release|x64.Build.0 = Release|x64
{26B24EFB-1E93-4AC1-AB9C-D80C9FA76D0F}.Release|x86.ActiveCfg = Release|Win32
{26B24EFB-1E93-4AC1-AB9C-D80C9FA76D0F}.Release|x86.Build.0 = Release|Win32
{37248D29-4153-4C53-9BB8-DD16AB36C257}.Debug|x64.ActiveCfg = Debug|x64
{37248D29-4153-4C53-9BB8-DD16AB36C257}.Debug|x64.Build.0 = Debug|x64
{37248D29-4153-4C53-9BB8-DD16AB36C257}.Debug|x86.ActiveCfg = Debug|Win32
{37248D29-4153-4C53-9BB8-DD16AB36C257}.Debug|x86.Build.0 = Debug|Win32
{37248D29-4153-4C53-9BB8-DD16AB36C257}.Release|x64.ActiveCfg = Release|x64
{37248D29-4153-4C53-9BB8-DD16AB36C257}.Release|x64.Build.0 = Release|x64
{37248D29-4153-4C53-9BB8-DD16AB36C257}.Release|x86.ActiveCfg = Release|Win32
{37248D29-4153-4C53-9BB8-DD16AB36C257}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -201,6 +221,8 @@ Global
{EB5FC754-BC16-4B5E-80C3-3D7AC23A63D2} = {6FE9DCA5-B3F2-4593-B71E-3B9BC3A59723}
{4EBF5131-F65F-4F3B-8C78-A9706EE1F495} = {A97B1356-3EAC-40A4-B591-E557CA356147}
{A740815A-4E10-4116-A01E-73D71286FBA6} = {2943D6FC-5010-421B-9250-B7CC6BB2E118}
{26B24EFB-1E93-4AC1-AB9C-D80C9FA76D0F} = {6FE9DCA5-B3F2-4593-B71E-3B9BC3A59723}
{37248D29-4153-4C53-9BB8-DD16AB36C257} = {A97B1356-3EAC-40A4-B591-E557CA356147}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {83EEE417-9039-4161-A830-DDDEECE454CD}
Expand Down
1 change: 1 addition & 0 deletions DSLib/Lib/FenwickTree/FenwickTree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "FenwickTree.h"
48 changes: 48 additions & 0 deletions DSLib/Lib/FenwickTree/FenwickTree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once
#include <vector>

namespace ds
{
class fen_tree
{
public:
explicit fen_tree(const std::size_t size) :size_(size + 1)
{
f_.assign(size + 1, 0);
}

void update_point(std::size_t i, std::size_t value);
std::size_t sum(std::size_t i) const;
std::size_t range_sum(std::size_t i, std::size_t j) const;

private:
std::vector<std::size_t> f_;
std::size_t size_;
};

inline void fen_tree::update_point(std::size_t i, const std::size_t value)
{
while (i < size_)
{
f_[i] += value;
i += i & -i;
}
}

inline std::size_t fen_tree::sum(std::size_t i) const
{
std::size_t s = 0;
while (i > 0)
{
s += f_[i];
i -= i & -i;
}

return s;
}

inline std::size_t fen_tree::range_sum(std::size_t i, std::size_t j) const
{
return sum(j) - (i == 1 ? 0 : sum(i - 1));
}
}
Loading

0 comments on commit 244fd39

Please sign in to comment.