NPL
Neurological Programs and Libraries
graph.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2014 Micah C Chambers (micahc.vt@gmail.com)
3  *
4  * NPL is free software: you can redistribute it and/or modify it under the
5  * terms of the BSD 2-Clause License available in LICENSE or at
6  * http://opensource.org/licenses/BSD-2-Clause
7  *
8  * @file graph.h This file contains the definition for Graph and its derived types.
9  ******************************************************************************/
10 
11 #ifndef GRAPH_H
12 #define GRAPH_H
13 
14 #include "npltypes.h"
15 #include "zlib.h"
16 
17 #include <algorithm>
18 #include <string>
19 #include <vector>
20 
21 namespace npl {
22 
23 // Data Types:
24 // bit 7, 1 if floating, 0 if integer
25 // bit 6, 1 if complex, 0 if real
26 // bit 5, 1 if signed, 0 if unsigned
27 // bit 0-4 size in bytes
29 {
31  G_DT_UCHAR = 1, // 0000 0001
32  G_DT_CHAR = 33, // 0010 0001
33  G_DT_USHORT = 2,// 0000 0010
34  G_DT_SHORT = 34,// 0010 0010
35  G_DT_ULONG = 4, // 0000 0100
36  G_DT_SLONG = 36,// 0010 0100
37  G_DT_ULONGLONG = 8, // 0000 1000
38  G_DT_SLONGLONG =40, // 0010 1000
39  G_DT_FLOAT = 132, // 1000 0100
40  G_DT_DOUBLE = 136, // 1000 1000
41  G_DT_QUAD = 144, // 1001 0000
42  G_DT_COMPLEX_FLOAT = 228, // 1110 0100
43  G_DT_COMPLEX_DOUBLE = 232, // 1110 1000
44  G_DT_COMPLEX_QUAD = 255 // 1111 1111
45 };
46 
48 {
53 };
54 
55 template <typename T>
57 
58 std::string describeType(GraphDataT type);
59 
60 template <typename T>
61 class Graph
62 {
63 public:
64  Graph();
65  Graph(std::string filename, bool typefail = true);
66  Graph(size_t nodes);
67  Graph(Graph&& other);
68  Graph(const Graph& other);
69  Graph(size_t nodes, void* data,
70  std::function<void(void*)> deleter=[](void*){});
72  {
73  m_freefunc(m_data);
74  };
75 
76  void init(size_t nodes);
77  void init(size_t nodes, void* data,
78  std::function<void(void*)> deleter=[](void*){});
79 
80  Graph& operator=(Graph<T>&& other);
81 
82  T& operator()(size_t from, size_t to)
83  {
84  return m_data[m_size*from + to];
85  };
86 
87  const T& operator()(size_t from, size_t to) const
88  {
89  return m_data[m_size*from + to];
90  };
91 
92  size_t nodes() const { return m_size; };
93 
94  const std::string& name(size_t ii) const {return m_names[ii]; };
95  std::string& name(size_t ii) { return m_names[ii]; };
96 
97  void load(std::string filename, bool typefail = true);
98  void save(std::string filename, GraphStoreT store = G_STORE_FULLMAT);
99 
100  static GraphDataT type() { return getType<T>(); };
101  static std::string typestr() { return typeid(T).name(); };
102 
103  void normalize();
104 
105  /* Famouse Graphs */
106  static Graph<T> Coxeter();
107  static Graph<T> PreRandom();
108 
109  /* Statistics */
110  double assortativity() const;
111  double assortativity_wei() const;
112 
113  T strength() const;
114  std::vector<T> strengths() const;
115  std::vector<T> strengths(std::vector<T>& is, std::vector<T>& os) const;
116 
117  int degree() const;
118  std::vector<int> degrees() const;
119  std::vector<int> degrees(std::vector<int>& is, std::vector<int>& os) const;
120 
121  std::vector<int> betweenness_centrality() const;
122  std::vector<int> betweenness_centrality_next() const;
123  void shortest(Graph<T>& sdist) const;
124  void shortest(Graph<int>& next, Graph<T>& sdist) const;
125 
126 private:
127  size_t m_size;
128  T* m_data;
129  std::function<void(T*)> m_freefunc;
130  std::vector<std::string> m_names;
131 
132  void writeCSV(gzFile gz, GraphStoreT store);
133  void writeNPL(gzFile gz, GraphStoreT store);
134 };
135 
136 } // npl
137 #endif
138 
Definition: accessors.h:29
int degree() const
const T & operator()(size_t from, size_t to) const
Definition: graph.h:87
void normalize()
T & operator()(size_t from, size_t to)
Definition: graph.h:82
GraphStoreT
Definition: graph.h:47
Graph & operator=(Graph< T > &&other)
void load(std::string filename, bool typefail=true)
void init(size_t nodes)
~Graph()
Definition: graph.h:71
double assortativity() const
static std::string typestr()
Definition: graph.h:101
void shortest(Graph< T > &sdist) const
static Graph< T > PreRandom()
size_t nodes() const
Definition: graph.h:92
std::vector< int > betweenness_centrality() const
static GraphDataT type()
Definition: graph.h:100
std::string & name(size_t ii)
Definition: graph.h:95
const std::string & name(size_t ii) const
Definition: graph.h:94
GraphDataT
Definition: graph.h:28
std::vector< int > betweenness_centrality_next() const
GraphDataT getType()
std::string describeType(GraphDataT type)
T strength() const
static Graph< T > Coxeter()
std::vector< T > strengths() const
void save(std::string filename, GraphStoreT store=G_STORE_FULLMAT)
std::vector< int > degrees() const
double assortativity_wei() const